Thanks for your replies guys. Using your feedback I have been able to
solve my issue in the following way:
The xslt file looks like this:
<xsl:stylesheet version="1.1"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
*xmlns:svg="http://www.w3.org/2000/svg"*
xmlns:fo="http://www.w3.org/1999/XSL/Format"
exclude-result-prefixes="fo">
...
<fo:block>
*<fo:instream-foreign-object>
<xsl:copy-of select="svg:svg" />
</fo:instream-foreign-object>*
</fo:block>
This was not enough however, because the svg data came from an object
that was converted into xml, an then the xml was transformed into a fo
file using the above xslt file. Because of this, the svg string was
escaped instead of being treated as xml, so i had to create a sax source
which used my own xml reader and input source:
new SAXSource(new ReportXMLReader(),
new ReportInputSource());
The ReportXMLReader implements the XMLReader interface to parse my java
object into an xml file and uses the ReportInputSource class to retrieve
the report object that is to be converted into xml.
Then, in the parse method of the ReportXMLReader I used the following code:
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(*svgString*));
XMLReader xmlReader = XMLReaderFactory.createXMLReader();
xmlReader.setContentHandler(new
MirroringContentHandler(handler));
xmlReader.parse(is);
to transform the svg string into its xml correspondent. The
MirroringContentHandler is just a ContentHandler implementation that
receives the xml elements from the svgString and outputs them to the sax
source content handler(the handler object that is passed to the
MirroringContentHandler):
@Override
public void startElement(String uri, String localName, String
qName, Attributes attributes) throws SAXException {
outputHandler.startElement(uri, localName, qName, attributes);
}
@Override
public void endElement(String uri, String localName, String
qName) throws SAXException {
outputHandler.endElement(uri, localName, qName);
}
@Override
public void characters(char[] ch, int start, int length) throws
SAXException {
outputHandler.characters(ch, start, length);
}
the rest of its methods being inherited from the DefaultHandler class.
Thanks again for all your help,
Cristi.
value-of is just going to pull in the value (text) associated with an
xml tag.
The svg tag will produce the svg output (image).
You can put svg code into the xsl to generate a dynamic image based on
data in the xml.
If you need the xsl tags themselves to be dynamic you can parse the
input and mix it in from multiple sources if you're printing from custom
embedded code.
The xsl should be fed into the TransformerFactory.newTransformer method
as a SAXSource which can be created from an InputSource which can be
created from a ByteArrayInputStream which can be created from a simple
byte[] array which can be created from a FileInputStream. Since you
have the xsl file just sitting as a byte[] object in memory you can
manipulate that however you want. You can read the file in one byte at
a time looking for a particular byte sequence to mark where to add in
code from a different file or read in the whole file at once then search
the byte[] array for the byte point to insert other code. There may be
a simpler way to substitute using tags in the xsl file but it sounds
like this is what you may be trying to accomplish.
-----Original Message-----
From: Cristi Cioriia [mailto:[email protected]]
Sent: Monday, January 17, 2011 10:57 AM
To: [email protected]
Subject: Re: Embedding SVG dynamically into fo file
Thanks for such a quick reply.
Using your feedback I was able to advance a little: if I replace "Some
text " from your sample with<xsl:value-of select="svg"/> I can see in
my report the svg that is generated. What I need though is that this
code is executed so that in the PDF report I have the actual image, not
its SVG representation. Any hint on how can I achieve that?
Regards,
Cristi
Cristi,
In order to include SVG in your XSLT file, first declare the name
space as
follows:
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format"
xmlns:svg="http://www.w3.org/2000/svg">
Then you can generate SVG as shown below:
<fo:block>
<fo:instream-foreign-object
span="all"
content-width="500pt" content-height="700pt" border-style="solid"
border-width="1" start-indent="0" margin-left="0">
<svg
xmlns="http://www.w3.org/2000/svg" height="500" width="700">
<rect x="50" y="50"
height="400"
width="630" fill="none" stroke="black" stroke-width="1" />
<text x="200" y="75"
style="font-family: ariel; fill:black; font-size:14;
font-weight:bold;">Some text</text>
</svg>
</fo:instream-foreign-object>
</fo:block>
Regards,
Jayant
|------------>
| From: |
|------------>
-----------------------------------------------------------------------
------------------------------------------------------------------------
---|
|Cristi Cioriia<[email protected]>
|
---------------------------------------------------------------------
----------------------------------------------------------------------
-------|
|------------>
| To: |
|------------>
-----------------------------------------------------------------------
------------------------------------------------------------------------
---|
|<[email protected]>
|
---------------------------------------------------------------------
----------------------------------------------------------------------
-------|
|------------>
| Date: |
|------------>
-----------------------------------------------------------------------
------------------------------------------------------------------------
---|
|01/17/2011 10:10 AM
|
---------------------------------------------------------------------
----------------------------------------------------------------------
-------|
|------------>
| Subject: |
|------------>
-----------------------------------------------------------------------
------------------------------------------------------------------------
---|
|Embedding SVG dynamically into fo file
|
---------------------------------------------------------------------
----------------------------------------------------------------------
-------|
Hi guys,
I have ran into a dead end trying to generate a PDF report which
should contain SVG files generated on the fly, and I was hoping that I
can find some help here on how to proceed next.
At first, I embedded a svg file into a fo file using the
fo:instream-foreign-object and everything worked as expected,
generating the PDF report that I needed from the fo file by following
the examples from the FO website.
But then the pdf report that I have to generate uses as source data a
java object which contains a string property which provides the svg
file and looking at the example from the FOP website
http://svn.apache.org/viewvc/xmlgraphics/fop/trunk/examples/embedding/
java/embedding/ExampleObj2PDF.java
I created an xslt file like this:
<xsl:template match="reportEntry">
<fo:block>
<fo:block>
<xsl:value-of select="title"/>
</fo:block>
<fo:block>
<xsl:value-of select="description"/>
</fo:block>
<fo:instream-foreign-object>
<xsl:value-of select="svg"/>
</fo:instream-foreign-object>
</fo:block>
</xsl:template>
But now, when I run the code I get the following exception:
javax.xml.transform.TransformerException:
org.apache.fop.fo.ValidationException: Error(Unknown location):
fo:instream-foreign-object is missing child elements.
Required Content Model: one (1) non-XSL namespace child
I have tried to find any help with google on how I can approach this,
but the only resources that I found use static resources as I did in
the first scenario, which is not enough for my use case. Can you point
me into the right direction on how to approach this?
Thanks for your help,
Cristi
--
Cristian-Andrei Cioriia
Programator Java
1&1 Internet Development S.R.L. - Bucharest/Romania - Web Applications
Hosting Romania
18 Mircea Eliade St
Sect 1, Bucharest
RO Bucharest, 012015
Număr de telefon: +40 751 812 984
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
--
Cristian-Andrei Cioriia
Programator Java
1&1 Internet Development S.R.L. - Bucharest/Romania - Web Applications
Hosting Romania
18 Mircea Eliade St
Sect 1, Bucharest
RO Bucharest, 012015
Număr de telefon: +40 751 812 984
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]
--
Cristian-Andrei Cioriia
Programator Java
1&1 Internet Development S.R.L. - Bucharest/Romania - Web Applications Hosting
Romania
18 Mircea Eliade St
Sect 1, Bucharest
RO Bucharest, 012015
Număr de telefon: +40 751 812 984
[email protected]