gpt4 book ai didi

java - 使用 FOP 将 XML 转换为 PDF 返回空白页面

转载 作者:行者123 更新时间:2023-12-01 05:10:00 25 4
gpt4 key购买 nike

我正在尝试使用 Apache FOP 将 xml 文件转换为 pdf 文件,但我只收到一个空白页,这是我的代码:

protected byte[] buildPDF(byte[] xml) throws IOException {
FileOutputStream fos;
byte[] pdfBytes = null;

try{

// Setup input and output files
File xmlfile = new File(MULTIMEDIA_PATH + File.separator + "xml/report.xml");
File xsltfile = new File(MULTIMEDIA_PATH + File.separator + "xml/transformation.xsl");

fos = new FileOutputStream(xmlfile);
fos.write(xml);
fos.close();

// Step 1: Construct a FopFactory
FopFactory fopFactory = FopFactory.newInstance();
FOUserAgent foUserAgent = fopFactory.newFOUserAgent();

// Step 2: Setup output stream
OutputStream out = new BufferedOutputStream(new FileOutputStream((MULTIMEDIA_PATH + File.separator + "xml/result.pdf")));

try {
// Step 3: Construct FOP with desired output format
Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, foUserAgent, out);

// Step 4: Setup JAXP using identity transformer
Source xslt = new StreamSource(xsltfile);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer transformer = factory.newTransformer(xslt);

// Step 5: Setup input and output for XSLT transformation
Source src = new StreamSource(xmlfile);
Result res = new SAXResult(fop.getDefaultHandler());

// Step 6: Start XSLT transformation and FOP processing
transformer.transform(src, res);

} finally {
out.close();
}

} catch (Exception e) {
e.printStackTrace(System.err);
System.exit(-1);
}

return pdfBytes;

}

你对这个问题有什么想法吗?

谢谢!!

最佳答案

如果你得到空输出,那么我认为你最后错过了类似的内容:

pdfBytes = out.toByteArray();

如果您的输出不为空(如果您确实有上述情况),那么您应该提供 conversion.xsl 的内容,也许有问题。

关于java - 使用 FOP 将 XML 转换为 PDF 返回空白页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12161204/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com