Java 到 PDF 的转换 [英] Java Transformation to PDF

查看:27
本文介绍了Java 到 PDF 的转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我的一个表演项目做 POC.目前我正面临 OutOfMemoryError.首先,我们使用 DOM 加载了一个 XML 文件,然后尝试使用 XSL 将其转换为 PDF.在阅读了该论坛的一条评论后,我切换到了 SAX 解析器,但它仍然出现相同的错误.

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.

文件为 30MB,系统内存为 512MB.

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 <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);
        }

    }

推荐答案

也许这会有所帮助:http://xmlgraphics.apache.org/fop/1.0/running.html#memory

这篇关于Java 到 PDF 的转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆