Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/358.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java到PDF的转换_Java_Pdf_Apache Fop - Fatal编程技术网

Java到PDF的转换

Java到PDF的转换,java,pdf,apache-fop,Java,Pdf,Apache Fop,我正在为我的一个绩效项目进行POC。目前我面临一个OutOfMemory错误。首先,我们使用DOM加载了一个XML文件,然后尝试使用XSL将其转换为PDF。在阅读了这个论坛的一条评论后,我切换到了SAX解析器,但它仍然给出了相同的错误 文件为30MB,系统内存为512MB System.out.println("FOP XMLTOPDFConverter\n"); System.out.println("Preparing..."); // Se

我正在为我的一个绩效项目进行POC。目前我面临一个OutOfMemory错误。首先,我们使用DOM加载了一个XML文件,然后尝试使用XSL将其转换为PDF。在阅读了这个论坛的一条评论后,我切换到了SAX解析器,但它仍然给出了相同的错误

文件为30MB,系统内存为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);
        }

    }
System.out.println(“FOP XMLTOPDFConverter\n”);
System.out.println(“准备…”);
//设置目录
/*文件baseDir=新文件(“.”);
File outDir=新文件(baseDir,“out”);
outDir.mkdirs()*/
//设置输入和输出文件
文件xmlfile=新文件(“C:/Documents and Settings/agarwgau/Desktop/300k/File_u0000036357.XML”);
文件xsltfile=new文件(“C:/Documents and Settings/agarwgau/Desktop/300k/UCB110037EventList.xsl”);
文件pdffile=新文件(“C:/Documents and Settings/agarwgau/Desktop/300k/ResultXML2PDF.pdf”);
System.out.println(“输入:XML(+xmlfile+”);
System.out.println(“样式表:+xsltfile”);
System.out.println(“输出:PDF(“+pdffile+”));
System.out.println();
System.out.println(“转换…”);
//根据需要配置fopFactory
FopFactory FopFactory=FopFactory.newInstance();
FOUserAgent FOUserAgent=fopFactory.newFOUserAgent();
//根据需要配置FousAgent
//设置输出
OutputStream out=新java.io.FileOutputStream(pdffile);
out=新java.io.BufferedOutputStream(out);
试一试{
//使用所需的输出格式构造fop
Fop Fop=fopFactory.newFop(MimeConstants.MIME_PDF,
Fourseragent,out);
//设置XSLT
TransformerFactory=TransformerFactory.newInstance();
变压器=工厂
.新变压器(新StreamSource(xsltfile));
//在样式表中设置a的值
transformer.setParameter(“versionParam”、“2.0”);
//设置XSLT转换的输入
Source src=新的StreamSource(xmlfile);
//产生的SAX事件(生成的FO)必须通过管道传输
//伪造
Result res=新的SAXSult(fop.getDefaultHandler());
//启动XSLT转换和FOP处理
变换(src,res);
}最后{
out.close();
}
System.out.println(“成功!”);
}捕获(例外e){
e、 printStackTrace();
系统退出(-1);
}
}

也许这会有所帮助:

这将被移植到如此合适的位置-但与此同时,您使用的是什么版本的JDK?您应该能够连接jconsole/visualvm,以了解内存泄漏/扩展超出您的限制的位置。是的,你可以移动一些代码,等等,但我认为这是一个很好的机会,可以使用正确的工具来“在猜测之前进行测量”,在我们称之为“transformer.transform(src,res);“这是一种什么样的OutOfMemory错误?”?“Java堆空间”?如果是这样,也许您可以尝试使用-Xms(最小堆空间)和-Xmx(最大堆空间)VM参数来扩展堆的大小?我已经将大小增加到1024,然后它还为我提供了Java堆空间和内存。我也实施了这一点