Пытаюсь конвертировать JSON в документ PDF, используя Apache FOP. Ниже приведен мой код Java, который используется для создания PDF-документа.
public class XML_XSLFOtoPDF {
private Logger logger = Logger.getLogger(getClass().getName());
private static XML_XSLFOtoPDF instance = new XML_XSLFOtoPDF();
public static XML_XSLFOtoPDF getInstance() {
return instance;
}
public String getPDF(JSONObject obj) throws IOException, DocumentException, TransformerException, FOPException, TransformerFactoryConfigurationError {
ClassLoader classLoader = getClass().getClassLoader();
String orderAcknowledgementTemplate = classLoader
.getResource("resoources/sample2.xsl").getFile();
String xslPath = orderAcknowledgementTemplate.replaceAll("%20", " ");
File xsltfile = new File(xslPath);
StringBuilder xml = new StringBuilder();
String header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
StringBuilder sb = new StringBuilder();
sb.append(header);
JSONObject json = new JSONObject(obj.toString());
xml.append(header);
xml.append("\n");
xml.append("<receipt>");
xml.append(XML.toString(json));
xml.append("</receipt>");
StreamSource source = new StreamSource(new StringReader(xml.toString()));
System.out.println("XML --->"+xml.toString());
StreamSource transformSource = new StreamSource(xsltfile);
FopFactory fopFactory = FopFactory.newInstance(new File(".").toURI());
FOUserAgent foUserAgent = fopFactory.newFOUserAgent();
ByteArrayOutputStream outStream = new ByteArrayOutputStream();
Transformer xslfoTransformer;
try
{
xslfoTransformer = getTransformer(transformSource);
Fop fop;
try
{
fop = fopFactory.newFop
(MimeConstants.MIME_PDF, outStream);
Result res = new SAXResult(fop.getDefaultHandler());
try
{
xslfoTransformer.transform(source, res);
File pdffile = new File("Result.pdf");
OutputStream out = new java.io.FileOutputStream(pdffile); // Error is throwing on this line
out = new java.io.BufferedOutputStream(out);
FileOutputStream str = new FileOutputStream(pdffile);
str.write(outStream.toByteArray());
str.close();
out.close();
}
catch (TransformerException e) {
throw e;
}
}
catch (FOPException e) {
throw e;
}
}
catch (TransformerConfigurationException e)
{
throw e;
}
catch (TransformerFactoryConfigurationError e)
{
throw e;
}
return xslPath;
}
private static Transformer getTransformer(StreamSource streamSource)
{
net.sf.saxon.TransformerFactoryImpl impl =
new net.sf.saxon.TransformerFactoryImpl();
try {
return impl.newTransformer(streamSource);
} catch (TransformerConfigurationException e) {
e.printStackTrace();
}
return null;
}
}
Получаю приведенную ниже ошибку при вызове метода для создания PDF.
org.apache.fop.fo.ValidationException: документ пуст (что-то не так с вашей таблицей стилей XSLT).
для следующих строк:
xslfoTransformer.transform(source, res);
OutputStream out = new java.io.FileOutputStream(pdffile);
Это мой XSL-файл с ошибкой:
<?xml version="1.0" encoding="iso-8859-1"?>
<xsl:stylesheet version="1.1"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
xmlns:fo="http://www.w3.org/1999/XSL/Format"
exclude-result-prefixes="fo">
<xsl:template match="root">
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
<fo:layout-master-set>
<fo:simple-page-master master-name="my-page" page-height="8.5in" page-width="11in">
<fo:region-body margin="1in"/>
</fo:simple-page-master>
</fo:layout-master-set>
<fo:page-sequence master-reference="my-page">
<fo:flow flow-name="xsl-region-body">
<fo:block>
Hello, <xsl:value-of select="name" />
!</fo:block>
<fo:block>
<fo:table>
<fo:table-column column-width="25mm"/>
<fo:table-column column-width="25mm"/>
<fo:table-column column-width="75mm"/>
<fo:table-header>
<fo:table-cell border="solid 1px black" text-align="center" font-weight="bold">
<fo:block>
No.
karthkeyan
</fo:block>
</fo:table-cell>
<fo:table-cell border="solid 1px black" text-align="center" font-weight="bold" width="60mm">
<fo:block>
Name
</fo:block>
</fo:table-cell>
<fo:table-cell border="solid 1px black" text-align="center" font-weight="bold">
<fo:block>
Phone Number
</fo:block>
</fo:table-cell>
</fo:table-header>
<fo:table-body>
<xsl:for-each select="./friend">
<fo:table-row>
<fo:table-cell border="solid 1px black" text-align="center">
<fo:block>
<xsl:value-of select="position()" />
</fo:block>
</fo:table-cell>
<fo:table-cell border="solid 1px black" text-align="center">
<fo:block>
<xsl:value-of select="name" />
</fo:block>
</fo:table-cell>
<fo:table-cell border="solid 1px black" text-align="center">
<fo:block>
<xsl:value-of select="phNo" />
</fo:block>
</fo:table-cell>
</fo:table-row>
</xsl:for-each>
</fo:table-body>
</fo:table>
</fo:block>
</fo:flow>
</fo:page-sequence>
</fo:root>
</xsl:template>
</xsl:stylesheet>