Java – PDF report with embedded HTML
We have a Java based system that can read data from the database, merge various data fields with preset xsl-fo tags, and convert the results into PDF FOP in PDF format
In xsl-fo format, it looks like this:
<?xml version="1.0" encoding="utf-8" ?> <!DOCTYPE Html [ <!ENTITY nbsp " "> <!-- all other entities --> ]> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:fo="http://www.w3.org/1999/XSL/Format"> <xsl:output method="xml" indent="yes" /> <xsl:template match="/"> <fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:svg="http://www.w3.org/2000/svg" font-family="..." font-size="..."> <fo:layout-master-set> <fo:simple-page-master master-name="Letter Page" page-width="8.500in" page-height="11.000in"> <!-- appropriate settings --> </fo:simple-page-master> </fo:layout-master-set> <fo:page-sequence master-reference="Letter Page"> <!-- some static content --> <fo:flow flow-name="xsl-region-body"> <fo:block> <fo:table ...> <fo:table-column ... /> <fo:table-body> <fo:table-row> <fo:table-cell ...> <fo:block text-align="..."> <fo:inline font-size="..." font-weight="..."> <!-- Header / Title --> </fo:inline> </fo:block> </fo:table-cell> </fo:table-row> </fo:table-body> </fo:table> </fo:block> <fo:block> <fo:table ...> <fo:table-column ... /> <fo:table-body> <fo:table-row> <fo:table-cell> <fo:block ...> <!-- Field A --> </fo:block> </fo:table-cell> </fo:table-row> </fo:table-body> </fo:table> <!-- Other fields in a very similar fashion as the above "Field A" --> </fo:block> </fo:flow> </fo:page-sequence> </fo:root> </xsl:template> </xsl:stylesheet>
Now I'm looking for a way to allow some fields to contain content in static HTML format This content will be generated by our HTML enabled editor (similar to cleditor, ckeditor, etc.) or pasted externally
My plan is to follow the recipe from this JavaWorld article:
>Convert HTML formatted strings to correct XHTML using jtidy > further modify xhtml2fo from antenna house XSL to delete all document and page wide transformations > apply this modified XSLT to my XHTML string (javax. XML. Transform) > extract all nodes under the root using XPath (javax. XML. XPath) > provide the results directly to the existing xsl-fo document
I have a bare bone version of such code and get the following error:
My question:
>What is the solution to this problem? > Can < fo: block > is used as a general container for other objects (including tables) nested in it? Is this the overall rational way to solve the task?
If someone has "done it there", please share your experience
Solution
The best way to solve the problem is to use a validated viewer / editor to check XSL fo Many, such as oxygen, will display errors in the XSL fo structure when you open them and will describe the problem (just like the reported error)
In your case, you obviously have a fo: table - body as a fo: block child It can't be A fo: table body has only one valid parent class, fo: table You will lose the fo: table tag, or you mistakenly insert fo: block into this location
In my opinion, I may do something slightly different I will put XHTML content in the XSL fo you want Then I'll create an identity transformation that copies all fo - based content, but transforms the XHTML part using XSL In this way, you can actually perform the transformation in an XSL editor such as oxygen and see why the error occurred Like anyone else
Note: you can also view other XSL, especially if your HTML may have any style = "" CSS attribute If this is the case, it is not simple HTML, then you will need a better way to handle HTML to CSS
http://www.cloudformatter.com/css2pdf Is based on this complete transformation General style sheets can be found here: http://xep.cloudformatter.com/doc/XSL/xeponline-fo-translate-2.xsl
I'm the author of that style sheet It's much more than you want, but a fairly complex parsing recursion is used to transform CSS styles into XSL fo properties