Create my first PDF file with java and FOP

M

[Miren]

Hello with the bellow method i trying to load one PDF file.
my fisrt problem is thaat this method works with one xml file and other xsl
file
Now the xml i havn't into one file, i have into one String.
The xsl file have into one file (i can read and put into one String).

1.- Can you help me for using in this method XML String and not using one
file?

2.- this mehtod return one ByteArrayOutputStream . how can i write one PDF
file into the disk from the ByteArrayOutputStream ?

thanks

public ByteArrayOutputStream getXSL_PDF(String xmlFileName,
String xslFileName) throws IOException, FOPException {
Logger log = null;
Hierarchy hierarchy = Hierarchy.getDefaultHierarchy();
log = hierarchy.getLoggerFor("fop");
log.setPriority(Priority.FATAL_ERROR);

File xmlFile = new File(xmlFileName);
File xslFile = new File(xslFileName);

XSLTInputHandler input = new XSLTInputHandler(xmlFile, xslFile);

ByteArrayOutputStream out = new ByteArrayOutputStream();

Driver driver = new Driver();
driver.setLogger(log);
driver.setRenderer(Driver.RENDER_PDF);
driver.setOutputStream(out);
driver.render(input.getParser(), input.getInputSource());

out.flush();

return out;
}
 
B

Brusque

Hello with the bellow method i trying to load one PDF file.
my fisrt problem is thaat this method works with one xml file and other xsl
file
Now the xml i havn't into one file, i have into one String.
The xsl file have into one file (i can read and put into one String).

1.- Can you help me for using in this method XML String and not using one
file?

2.- this mehtod return one ByteArrayOutputStream . how can i write one PDF
file into the disk from the ByteArrayOutputStream ?

thanks

public ByteArrayOutputStream getXSL_PDF(String xmlFileName,
String xslFileName) throws IOException, FOPException {
Logger log = null;
Hierarchy hierarchy = Hierarchy.getDefaultHierarchy();
log = hierarchy.getLoggerFor("fop");
log.setPriority(Priority.FATAL_ERROR);

File xmlFile = new File(xmlFileName);
File xslFile = new File(xslFileName);

XSLTInputHandler input = new XSLTInputHandler(xmlFile, xslFile);

ByteArrayOutputStream out = new ByteArrayOutputStream();

Driver driver = new Driver();
driver.setLogger(log);
driver.setRenderer(Driver.RENDER_PDF);
driver.setOutputStream(out);
driver.render(input.getParser(), input.getInputSource());

out.flush();

return out;
}

If you look at the documentation for XSLTInputHandler you'll see that it can
accept an InputSource as well as a File (actually I suspect the File option
is only there for convenience).

So create InputSources for your XSLT and XML like this:

Reader xmlReader = new StringReader(xmlString);
InputSource xmlSource = new InputSource(reader);

Reader xslReader = new FileReader(xslFileName);
InputSource xslSource = new InputSource(xslReader);

XSLTInputHandler input = new XSLTInputHandler(xmlSource, xslSource);

As for your second question, there's two ways to do this. Probably the most
efficient is to not use a ByteArrayOutputStream at all. Create a
FileOutputStream instead and pass this to the driver.setOutputStream()
method. Or if you really need a ByteArrayOutputStream (e.g. you need the PDF
in memory) you can just create a FileOutputStream and then use the writeTo()
method on ByteArrayOutputStream to write the contents to the file.

Oh and make sure you close all your readers/streams properly, else you might
create some memory leaks. You may also want to use buffered readers and
streams.

Good luck!
 
M

[Miren]

I have made you say me but appears one error.
the methos is now:
public ByteArrayOutputStream getXSL_PDFDeString(String xml,
String xslFileName) throws IOException, FOPException {
Logger log = null;
Hierarchy hierarchy = Hierarchy.getDefaultHierarchy();
log = hierarchy.getLoggerFor("fop");
log.setPriority(Priority.FATAL_ERROR);

Reader xmlReader = new StringReader(xml);
InputSource xmlSource = new InputSource(xmlReader);

Reader xslReader = new FileReader(xslFileName);
InputSource xslSource = new InputSource(xslReader);

XSLTInputHandler input = new XSLTInputHandler(xmlSource, xslSource);

ByteArrayOutputStream out = new ByteArrayOutputStream();

Driver driver = new Driver();
driver.setLogger(log);
driver.setRenderer(Driver.RENDER_PDF);
driver.setOutputStream(out);
driver.render(input.getParser(), input.getInputSource());

out.flush();

return out;
}

But apears one error tha says:
The constructor XSLTInputHandler(InputSource, InputSource) is undefined



how can i solve it?
thanks
 
B

Brusque

I have made you say me but appears one error.
the methos is now:
public ByteArrayOutputStream getXSL_PDFDeString(String xml,
String xslFileName) throws IOException, FOPException {
Logger log = null;
Hierarchy hierarchy = Hierarchy.getDefaultHierarchy();
log = hierarchy.getLoggerFor("fop");
log.setPriority(Priority.FATAL_ERROR);

Reader xmlReader = new StringReader(xml);
InputSource xmlSource = new InputSource(xmlReader);

Reader xslReader = new FileReader(xslFileName);
InputSource xslSource = new InputSource(xslReader);

XSLTInputHandler input = new XSLTInputHandler(xmlSource, xslSource);

ByteArrayOutputStream out = new ByteArrayOutputStream();

Driver driver = new Driver();
driver.setLogger(log);
driver.setRenderer(Driver.RENDER_PDF);
driver.setOutputStream(out);
driver.render(input.getParser(), input.getInputSource());

out.flush();

return out;
}

But apears one error tha says:
The constructor XSLTInputHandler(InputSource, InputSource) is undefined

Which version of FOP are you using? The latest (0.20.5) has a constructor
that accepts InputSources. Perhaps you're using an old version? You could
also try using TraxInputHandler instead.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top