我正在爲我的一個性能項目做POC,目前它給了我當前情景中的內存不足錯誤,因爲我們已經將該xml加載爲DOM,然後將該XSL轉換爲PDF,這給OOMemory錯誤,Java轉換爲PDF
僅從這個論壇的意見之一後,我已經使用SAX解析器爲XML文件,並給XSL,然後用FOP這個30 MB,系統內存爲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 <param> 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);
}
}
是否要將XML處理爲某種人類可讀的格式,或者將XML內容本身放入PDF文件(可能使用語法高亮顯示)? – Bruno
一些可讀的格式? ' \t 蘇德赫 FathersName> \t 馬杜 \t Deepti \t 拉夫 SecondChild> ' PDF它會是這樣 後------- ------------------------- ' 父親\t母親\t \t FirstChild \t SecondChild –
gaurav
請僅編輯您的原始文章,而不是嘗試在評論中對問題進行新的描述。謝謝。 – sarnold