简体   繁体   English

将PDF转换为Tiff时出现PDFBox Outofmemory,如何压缩JPEG图像?

[英]PDFBox Outofmemory while converting pdf to Tiff, how to compress JPEG images?

Am trying to images as new page to the pdf files, the jpeg images are too large in size, though it gets added fine, am facing issue while converting the pdf to Tiff, its throwing outofmemory exception, is there a way to compress these jpeg files 我试图将图像作为新页面映射到pdf文件,但jpeg图像的尺寸太大,尽管添加得很好,但在将pdf转换为Tiff时遇到了问题,它抛出了内存不足的异常,有没有办法压缩这些jpeg档案

Below code to convert images to pdf 下面的代码将图像转换为pdf

PDImageXObject image = PDImageXObject.createFromFile(imagePath, doc);           
            PDPage page = new PDPage(new PDRectangle(image.getWidth(), image.getHeight()));
            doc.addPage(page);
            try (PDPageContentStream contents = new PDPageContentStream(doc, page)) {
                contents.drawImage(image, 0,0);     
                contents.close();

            }

Below to convert to pdf to tiff 下面转换为PDF到TIFF

document = PDDocument.load(new File(pdfFilename));
            PDFRenderer pdfRenderer = new PDFRenderer(document);
            BufferedImage[] images = new BufferedImage[document.getNumberOfPages()];

            for (int i = 0; i < images.length; i++) {
                PDPage page = (PDPage) document.getPage(i);
                BufferedImage image;
                try {
                    image = pdfRenderer.renderImageWithDPI(i, 300, ImageType.RGB); // its throwing outofmemory error at this line
                    images[i] = image;
                } catch (IOException e) {
                    LOGGER.error("Exception while reading merged pdf file:" + e.getMessage());
                    throw e;
                }
            }

            File tempFile = new File(tiffFileName+".tiff");
            ImageWriter writer = ImageIO.getImageWritersByFormatName("TIFF").next();
            ImageOutputStream output = ImageIO.createImageOutputStream(tempFile);
            writer.setOutput(output);
            ImageWriteParam params = writer.getDefaultWriteParam();
            params.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
            writer.prepareWriteSequence(null);
            for (int i = 0; i < images.length; i++) {
                params.setCompressionType("JPEG");
                writer.writeToSequence(new IIOImage(images[i], null, null), params);
            }
            writer.endWriteSequence();

To avoid the out of memory exception, try starting your application by passing it Xms and Xmx parameters, using suitable values. 为避免内存不足异常,请尝试通过使用适当的值传递Xms和Xmx参数来启动应用程序。 See this post What are the Xms and Xmx parameters when starting JVMs? 请参阅本文。启动JVM时,Xms和Xmx参数是什么? and https://docs.oracle.com/cd/E21764_01/web.1111/e13814/jvm_tuning.htm#PERFM150 : https://docs.oracle.com/cd/E21764_01/web.1111/e13814/jvm_tuning.htm#PERFM150

Setting initial and minimum heap size 设置初始和最小堆大小

-Xms Oracle recommends setting the minimum heap size (-Xms) equal to the maximum heap size (-Xmx) to minimize garbage collections. -Xms Oracle建议将最小堆大小(-Xms)设置为等于最大堆大小(-Xmx),以最大程度地减少垃圾回收。

Setting maximum heap size 设置最大堆大小

-Xmx Setting a low maximum heap value compared to the amount of live data decrease performance by forcing frequent garbage collections. -Xmx设置与活动数据量相比较低的最大堆值会通过强制频繁进行垃圾收集而降低性能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM