java xsl转换pdf_Java转换为PDF

博主在性能项目POC中,使用Java将XML文件通过DOM加载并用XSL转换为PDF时遇到OutOfMemoryError,切换到SAX解析器仍报错。文件30MB,系统内存512MB,文中给出了相关代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I am doing a POC for one of my performance projects. Currently I'm facing an OutOfMemoryError. First we had loaded an XML file using DOM and then attempted to transform it with XSL into a PDF. After reading one of the comments from this forum, I switched to a SAX parser but it still gives the same error.

The file is 30MB and the System Memory is 512MB.

System.out.println("FOP XMLTOPDFConverter\n");

System.out.println("Preparing...");

// Setup directories

/* File baseDir = new File(".");

File outDir = new File(baseDir, "out");

outDir.mkdirs();*/

// Setup input and output files

File xmlfile = new File("C:/Documents and Settings/agarwgau/Desktop/300k/File_0000036357.XML");

File xsltfile = new File("C:/Documents and Settings/agarwgau/Desktop/300k/UCB110037EventList.xsl");

File pdffile = new File("C:/Documents and Settings/agarwgau/Desktop/300k/ResultXML2PDF.pdf");

System.out.println("Input: XML (" + xmlfile + ")");

System.out.println("Stylesheet: " + xsltfile);

System.out.println("Output: PDF (" + pdffile + ")");

System.out.println();

System.out.println("Transforming...");

// configure fopFactory as desired

FopFactory fopFactory = FopFactory.newInstance();

FOUserAgent foUserAgent = fopFactory.newFOUserAgent();

// configure foUserAgent as desired

// Setup output

OutputStream out = new java.io.FileOutputStream(pdffile);

out = new java.io.BufferedOutputStream(out);

try {

// Construct fop with desired output format

Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF,

foUserAgent, out);

// Setup XSLT

TransformerFactory factory = TransformerFactory.newInstance();

Transformer transformer = factory

.newTransformer(new StreamSource(xsltfile));

// Set the value of a in the stylesheet

transformer.setParameter("versionParam", "2.0");

// Setup input for XSLT transformation

Source src = new StreamSource(xmlfile);

// Resulting SAX events (the generated FO) must be piped through

// to FOP

Result res = new SAXResult(fop.getDefaultHandler());

// Start XSLT transformation and FOP processing

transformer.transform(src, res);

} finally {

out.close();

}

System.out.println("Success!");

} catch (Exception e) {

e.printStackTrace();

System.exit(-1);

}

}

解决方案

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值