Discussion:
embedding FOP in Java
Boris Rousseau
2002-12-09 12:11:44 UTC
Permalink
Hello,

I am using the following file embedding fop:
"import org.apache.fop.apps.Driver;
import org.apache.fop.layout.Page;
import org.apache.fop.apps.Version;
import org.apache.fop.apps.XSLTInputHandler;
import org.apache.fop.apps.InputHandler;
import org.xml.sax.*;
import org.apache.fop.messaging.MessageHandler;
import java.io.*;

//import org.apache.fop.apps.InputHandler;
//import org.apache.fop.apps.XSLTInputHandler;

public class fop{

public void fop(String xmlFile, String xslFile, String outFile){

Driver driver = new Driver();
driver.setRenderer(Driver.RENDER_PDF);
InputHandler inputHandler = new XSLTInputHandler(xmlFile, xslFile);
XMLReader parser = inputHandler.getParser();
driver.setOutputStream(new FileOutputStream(outFile));
driver.render(parser, inputHandler.getInputSource());
}
}"

Unfortunately when I compile it, I get the following error:
fop.java:19: cannot resolve symbol
symbol : constructor XSLTInputHandler (java.lang.String,java.lang.String)
location: class org.apache.fop.apps.XSLTInputHandler
InputHandler inputHandler = new XSLTInputHandler(xmlFile, xslFile);
^
1 error

Can you help me ?

I would also like to embed the FOP as a Web Service.
Can you give me any pointers to tutorials on this ?

Regards,
Boris
a***@se.abb.com
2002-12-09 12:32:13 UTC
Permalink
i use this method and it works fine


public void createPdf(String xml, String xslPath, String outputPdfPath) throws TransformerConfigurationException, TransformerException,
FileNotFoundException, FOPException, IOException
{

MessageHandler.setScreenLogger(log);

FileOutputStream fos=new FileOutputStream(outputPdfPath);

Driver driver=new Driver();
driver.setLogger(log);
driver.setOutputStream(fos);
driver.setRenderer(Driver.RENDER_PDF);

StreamSource xmlStreamSource=new StreamSource(new StringReader(xml));

Transformer transformer=TransformerFactory.newInstance().newTransformer(new StreamSource(xslPath));
transformer.transform(xmlStreamSource, new SAXResult(driver.getContentHandler()));

fos.close();
}
a***@se.abb.com
2002-12-09 12:36:15 UTC
Permalink
Sorry for beeing forced to ask this question,
I have created I java class that takes xml file and xsl file and creates
pdf.

Now my xml has croatian character and those are showen as #.
I read documentation and it says that fonts must be avaliable on the
platform.
Now I don't understand this since I have croatina locale set and I can view
pdf files that have
croatian characters.
I have tried to use different font familises for fo:block tag. (Arial,
Symbol etc ...)


What should I do to make it work. If you can explain it to me step by step
I would be very thankful.

My method is

public void createPdf(String xml, String xslPath, String outputPdfPath)
throws TransformerConfigurationException, TransformerException,
FileNotFoundException, FOPException, IOException
{
Logger log = new ConsoleLogger(ConsoleLogger.LEVEL_WARN);
MessageHandler.setScreenLogger(log);
FileOutputStream fos=new FileOutputStream(outputPdfPath);

//Options options = new Options(new File("c:
\\Adis\\MedicSoft\\MedicsoftClient\\GUI\\config\\print\\userconfig.xml"));

Driver driver=new Driver();
driver.setLogger(log);
driver.setOutputStream(fos);
driver.setRenderer(Driver.RENDER_PDF);
StreamSource xmlStreamSource=new StreamSource(new StringReader(xml));
Transformer transformer=TransformerFactory.newInstance
().newTransformer(new StreamSource(xslPath));
transformer.transform(xmlStreamSource, new
SAXResult(driver.getContentHandler()));
fos.close();
}

and xsl that is used for fop is very easy

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format">

<xsl:template match="/">
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master master-name="simple"
page-height="29.7cm"
page-width="21cm"
margin-top="1cm"
margin-bottom="2cm"
margin-left="2.5cm"
margin-right="2.5cm">
<fo:region-body margin-top="3cm"/>
<fo:region-before extent="3cm"/>
<fo:region-after extent="1.5cm"/>
</fo:simple-page-master>
</fo:layout-master-set>

<fo:page-sequence master-reference="simple">
<fo:flow flow-name="xsl-region-body">
<xsl:apply-templates select="data"/>
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>

<xsl:template match="data">
<fo:block>
<xsl:apply-templates select="name"/>
<xsl:apply-templates select="description"/>
</fo:block>
</xsl:template>

<xsl:template match="name">
<fo:block font-size="18pt"
font-family="sans-serif"
line-height="24pt"
space-after.optimum="15pt"
background-color="blue"
color="white"
text-align="center"
padding-top="3pt">
<xsl:value-of select="."/>
</fo:block>
</xsl:template>

<xsl:template match="description">
<fo:block font-size="12pt"
font-family="Symbol"
line-height="15pt"
space-after.optimum="3pt"
text-align="justify">
<xsl:value-of select="."/>
</fo:block>
</xsl:template>

</xsl:stylesheet>
Oleg Tkachenko
2002-12-09 18:16:56 UTC
Permalink
Post by a***@se.abb.com
Now my xml has croatian character and those are showen as #.
I read documentation and it says that fonts must be avaliable on the
platform.
Now I don't understand this since I have croatina locale set and I can view
pdf files that have
croatian characters.
See http://xml.apache.org/fop/faq.html#faq-N103ED and
http://xml.apache.org/fop/fonts.html.
--
Oleg Tkachenko
eXperanto team
Multiconn Technologies, Israel
Oleg Tkachenko
2002-12-09 18:13:25 UTC
Permalink
Post by Boris Rousseau
fop.java:19: cannot resolve symbol
symbol : constructor XSLTInputHandler (java.lang.String,java.lang.String)
location: class org.apache.fop.apps.XSLTInputHandler
InputHandler inputHandler = new XSLTInputHandler(xmlFile, xslFile);
^
1 error
Can you help me ?
The only constructor XSLTInputHandler class has is
public XSLTInputHandler(File xmlfile, File xsltfile), there is no
XSLTInputHandler (java.lang.String,java.lang.String) constructor.
--
Oleg Tkachenko
eXperanto team
Multiconn Technologies, Israel
Boris Rousseau
2002-12-10 11:57:21 UTC
Permalink
All,

Do you always need a servlet to run an embedded FOP in a Java ?
I am a bit confused with the example provided in fop-0.20.4.
(an API documentation of the Driver, XSLTInputHandler, MessageHandler
classes would help a lot)
Obviously I don't want to use a servlet but rather use a Web Service to
be able to submit XML documents and get back PDFs.

Regards,
Boris

----- Original Message -----
From: "Oleg Tkachenko" <***@multiconn.com>
To: <fop-***@xml.apache.org>
Sent: Monday, December 09, 2002 6:13 PM
Subject: Re: embedding FOP in Java
*This message was transferred with a trial version of CommuniGate(tm) Pro*
Post by Boris Rousseau
fop.java:19: cannot resolve symbol
symbol : constructor XSLTInputHandler
(java.lang.String,java.lang.String)
Post by Boris Rousseau
location: class org.apache.fop.apps.XSLTInputHandler
InputHandler inputHandler = new XSLTInputHandler(xmlFile, xslFile);
^
1 error
Can you help me ?
The only constructor XSLTInputHandler class has is
public XSLTInputHandler(File xmlfile, File xsltfile), there is no
XSLTInputHandler (java.lang.String,java.lang.String) constructor.
--
Oleg Tkachenko
eXperanto team
Multiconn Technologies, Israel
---------------------------------------------------------------------
Oleg Tkachenko
2002-12-10 12:14:59 UTC
Permalink
Post by Boris Rousseau
Do you always need a servlet to run an embedded FOP in a Java ?
Not really, you can embed FOP anywhere from standalone application to ejb.
FOPServlet is just an example of how to embed, useful though.
Post by Boris Rousseau
Obviously I don't want to use a servlet but rather use a Web Service to
be able to submit XML documents and get back PDFs.
And what's you problem? You know how to embed FOP already, so write your web
service and embed FOP there.
--
Oleg Tkachenko
eXperanto team
Multiconn Technologies, Israel
Loading...