You are viewing a plain text version of this content. The canonical link for it is here.
Posted to fop-users@xmlgraphics.apache.org by m....@gmx.de on 2002/10/24 13:18:17 UTC
Embedding without writing to a file
Hi,
I generate my xslfo code in a java programm and currently write out the
xslfo to a file. Then I read it again in an InputSource and pass it to FOP to
create PDF.
Can someone please guide me with an example on how to create an InputSource
from a String and directly pass it to fop?
Thanks in advance
Markus
--
------------------------------------------------------
+++ GMX - Mail, Messaging & more http://www.gmx.net +++
NEU: Mit GMX ins Internet. Rund um die Uhr für 1 ct/ Min. surfen!
Re: Embedding without writing to a file
Posted by William Gilbert <Gi...@Informagen.com>.
> I generate my xslfo code in a java programm and currently write out the xslfo to a
> file. Then I read it again in an InputSource and pass it to FOP to create PDF.
> Can someone please guide me with an example on how to create an InputSource from a
> String and directly pass it to fop?
The trick you need is to convert your String into a org.w3c.dom.Document then render
that. Better yet would be to build a org.w3c.dom.Document instead of a String
Here's a snippet from one of my servlets passing in a org.w3c.dom.Document
private byte[] renderPDF(Document foDocument) throws ServletException {
ByteArrayOutputStream pdfStream = null;
try {
pdfStream = new ByteArrayOutputStream();
foProcessor.reset();
foProcessor.setRenderer(Driver.RENDER_PDF);
foProcessor.setOutputStream(pdfStream);
foProcessor.render(foDocument);
} catch (Exception e) {
throw new ServletException(e);
}
return pdfStream.toByteArray();
}
------------------------------------------------------------------------------------
If you need to convert a String to a Document use something like this
public static DocumentBuilder documentBuilder = null;
.
.
.
// At Servlet start up instance documentBuilder for reuse
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
documentBuilder = factory.newDocumentBuilder();
} catch (ParserConfigurationException pce) {
throw new ServletException(pce);
}
.
.
.
// Resuse documentBuilder in this public utility to convert Strings to Documents
public static Document stringToDocument(String xmlString) throws ServletException {
Document document = null;
if (xmlString == null)
return document;
try {
document = documentBuilder.parse(new ByteArrayInputStream(xmlString.getBytes()));
} catch (Exception e) {
throw new ServletException(e);
}
return document;
}
Re: Embedding without writing to a file
Posted by Christian Wolfgang Hujer <Ch...@itcqis.com>.
Hello,
Am Donnerstag, 24. Oktober 2002 23:38 schrieb J.Pietschmann:
> > I don't know anything about FOP's internals, but wouldn't it be even
> > better to use a DOM structure and pass a Document to FOP?
>
> A DOM document uses memory, for no purpose in this case because FOP
> builds its own internal structure anyway.
Okay.
But I still would pass the DOM structure if the source already is DOM.
> You should avoid "low-level" and "high-level" here. Different
> approaches to the same problem (access XML data from a program
> or pass sort of XML data to somewhere else). Each with it's
> own strenghts and weaknesses.
Why do you consider SAX and DOM not to be low-level and high-level?
SAX in fact is a more physical (low level) representation of the document,
since it is event driven on orrurrence of physical markup, while DOM creates
a logical tree.
This is low-level and high-level. (As in OSI or DOD models for networking for
instance)
If you interpreted low-level and high-level as bad and good - then sorry. This
was definitely not meant by me.
Bye
--
ITCQIS GmbH
Christian Wolfgang Hujer
Geschäftsführender Gesellschafter
Telefon:+49 (0)89 27 37 04 37
Telefax:+49 (0)89 27 37 04 39
E-Mail:Christian.Hujer@itcqis.com
WWW: http://www.itcqis.com/
Re: Embedding without writing to a file
Posted by "J.Pietschmann" <j3...@yahoo.de>.
Christian Wolfgang Hujer wrote:
> I don't know anything about FOP's internals, but wouldn't it be even better to
> use a DOM structure and pass a Document to FOP?
A DOM document uses memory, for no purpose in this case because FOP
builds its own internal structure anyway.
> Or is FOP working on low-level SAX instead of high-level DOM anyway?
You should avoid "low-level" and "high-level" here. Different
approaches to the same problem (access XML data from a program
or pass sort of XML data to somewhere else). Each with it's
own strenghts and weaknesses.
J.Pietschmann
Re: Embedding without writing to a file
Posted by Christian Wolfgang Hujer <Ch...@itcqis.com>.
Hello Oleg, dear list members,
Am Mittwoch, 24. Juli 2002 20:05 schrieb Oleg Tkachenko:
> Christian Wolfgang Hujer wrote:
> > I don't know anything about FOP's internals, but wouldn't it be even
> > better to use a DOM structure and pass a Document to FOP?
>
> There is no difference from fop's point of view actually, but why do you
> want to create this potentially huge superfluous object? Using DOM probably
> requires less efforts from a developer, but if you do care a little about
> efficiency, you know that dom and sax are just Boeing-747 and F-16. DOM
> object is a full tree representation of the document hence it consumes a
> lot of memory, while SAX is an extremely lightweight callback-based
> approach and almost doesn't consume any memory.
I just think DOM is a bit easier to use than a callback event driven interface
like SAX.
And the source might already be a DOM structure. So if it already is DOM, it
already is a "potentially huge superflous object".
Bye
--
ITCQIS GmbH
Christian Wolfgang Hujer
Geschäftsführender Gesellschafter
Telefon:+49 (0)89 27 37 04 37
Telefax:+49 (0)89 27 37 04 39
E-Mail:Christian.Hujer@itcqis.com
WWW: http://www.itcqis.com/
Re: Embedding without writing to a file
Posted by Oleg Tkachenko <ol...@multiconn.com>.
Christian Wolfgang Hujer wrote:
> I don't know anything about FOP's internals, but wouldn't it be even better to
> use a DOM structure and pass a Document to FOP?
There is no difference from fop's point of view actually, but why do you want
to create this potentially huge superfluous object? Using DOM probably
requires less efforts from a developer, but if you do care a little about
efficiency, you know that dom and sax are just Boeing-747 and F-16. DOM object
is a full tree representation of the document hence it consumes a lot of
memory, while SAX is an extremely lightweight callback-based approach and
almost doesn't consume any memory.
> Or is FOP working on low-level SAX instead of high-level DOM anyway?
On building fo tree from an input - sure.
--
Oleg Tkachenko
eXperanto team
Multiconn Technologies, Israel
Re: Embedding without writing to a file
Posted by Christian Wolfgang Hujer <Ch...@itcqis.com>.
Hello,
Am Donnerstag, 24. Oktober 2002 15:46 schrieb Oleg Tkachenko:
> m.schaeffler@gmx.de wrote:
> > I generate my xslfo code in a java programm and currently write out the
> > xslfo to a file. Then I read it again in an InputSource and pass it to
> > FOP to create PDF.
> Intermediate file is a bad idea, but intermediate string is not much better
> one, because it means superfluous serializing to string and parsing from
> the string.
> The best approach here is using SAX. All you need is to output your xsl-fo
> as SAX events and feed them to fop, that's it, no intermediate objects, no
> wasting memory and cpu.
I don't know anything about FOP's internals, but wouldn't it be even better to
use a DOM structure and pass a Document to FOP?
Or is FOP working on low-level SAX instead of high-level DOM anyway?
Bye
--
ITCQIS GmbH
Christian Wolfgang Hujer
Geschäftsführender Gesellschafter
Telefon:+49 (0)89 27 37 04 37
Telefax:+49 (0)89 27 37 04 39
E-Mail:Christian.Hujer@itcqis.com
WWW: http://www.itcqis.com/
Re: Embedding without writing to a file
Posted by Oleg Tkachenko <ol...@multiconn.com>.
m.schaeffler@gmx.de wrote:
> I generate my xslfo code in a java programm and currently write out the
> xslfo to a file. Then I read it again in an InputSource and pass it to FOP to
> create PDF.
> Can someone please guide me with an example on how to create an InputSource
> from a String and directly pass it to fop?
Intermediate file is a bad idea, but intermediate string is not much better
one, because it means superfluous serializing to string and parsing from the
string.
The best approach here is using SAX. All you need is to output your xsl-fo as
SAX events and feed them to fop, that's it, no intermediate objects, no
wasting memory and cpu.
--
Oleg Tkachenko
eXperanto team
Multiconn Technologies, Israel