java使用Apache PDFBox进行pdf转图片内存溢出,因dpi太大

在使用Apache PDFBox转图片时候报错了,之前使用一直是正常的我一看pdf只有一页,然后用了其他工具报了一个pdf的宽高太大,所以我将dpi从200改为30重新转换就正常出来了,不过有一个比较尴尬的问题就是没办法自己识别自适应去调节dpi,所以在这里想请有遇到这种问题的小伙伴分享一下方法。

报错是这一行

 BufferedImage image = renderer.renderImageWithDPI(i, dpi);


    public List<File> convertPdfToImage(File file, String destination,String imageName,int dpi,int maxWidth,int maxHeight) throws Exception {

        File destinationFile = new File(destination);

        if (!destinationFile.exists()) {
            destinationFile.mkdir();
            System.out.println("DESTINATION FOLDER CREATED -> " + destinationFile.getAbsolutePath());
        }else if(destinationFile.exists()){
            System.out.println("DESTINATION FOLDER ALLREADY CREATED!!!");
        }else{
            System.out.println("DESTINATION FOLDER NOT CREATED!!!");
        }

        if (file.exists()) {
//            PDDocument doc = PDDocument.load(file);
            PDDocument doc = Loader.loadPDF(file);



            PDFRenderer renderer = new PDFRenderer(doc);
            List<File> fileList = new ArrayList<File>();

            String fileName = file.getName().replace(".pdf", "");
            System.out.println("CONVERTER START.....");

            for (int i = 0; i < doc.getNumberOfPages(); i++) {
                // default image files path: original file path
                // if necessary, file.getParent() + "/" => another path
                File fileTemp = new File(destination + imageName + (i+1)); // jpg or png


                BufferedImage image = renderer.renderImageWithDPI(i, dpi);


                System.out.println("输出第"+i+"张图片 ,宽度:"+ image.getWidth()+"高度:"+image.getHeight());

                if (image.getWidth()>maxWidth || image.getHeight()>maxHeight){
                    float widthRatio=(float) image.getWidth()/(float)maxWidth;
                    float heightRatio=(float)image.getHeight()/(float)maxHeight;
                    float Ratio = widthRatio>heightRatio?widthRatio:heightRatio;

                    System.out.println(Ratio);

                    int newWidth = Math.round((float)image.getWidth()/(float)Ratio);
                    int heightWidth = Math.round((float)image.getHeight()/(float)Ratio);
                    image=resizeBufferedImage(image,newWidth,heightWidth,true);
                    System.out.println("输出第"+i+"张图片,宽高超出,进行缩放新的 ,宽度:"+ newWidth+"高度:"+heightWidth);
                }
                ImageIO.write(image, "JPEG", fileTemp); // JPEG or PNG
                fileList.add(fileTemp);
            }
            doc.close();
            System.out.println("CONVERTER STOPTED.....");
            System.out.println("IMAGE SAVED AT -> " + destinationFile.getAbsolutePath());
            return fileList;
        } else {
            System.err.println(file.getName() + " FILE DOES NOT EXIST");
        }
        return null;
    }

我成功在类中找到了获取宽高的函数,这样应该可以自适应出dpi

主要代码

         PDPage page = doc.getPage(i);
                PDRectangle cropbBox = page.getCropBox();
                float widthPt = cropbBox.getWidth();
                float heightPt = cropbBox.getHeight();
  PDDocument doc = Loader.loadPDF(file);



            PDFRenderer renderer = new PDFRenderer(doc);
            List<File> fileList = new ArrayList<File>();

            String fileName = file.getName().replace(".pdf", "");
            System.out.println("CONVERTER START.....");

            for (int i = 0; i < doc.getNumberOfPages(); i++) {
                // default image files path: original file path
                // if necessary, file.getParent() + "/" => another path
                File fileTemp = new File(destination + imageName + (i+1)); // jpg or png

                PDPage page = doc.getPage(i);
                PDRectangle cropbBox = page.getCropBox();
                float widthPt = cropbBox.getWidth();
                float heightPt = cropbBox.getHeight();
                System.out.println("测试获取pdf宽高....."+widthPt);
                System.out.println("测试获取pdf宽高....."+heightPt);
}

如果你的pdf文档只有一页或者几页,可以看看free spire.pdf for java工具,它可以把pdf转指定dpi图片,不过自适应调节dpi不支持

        PdfDocument doc = new PdfDocument();
        doc.loadFromFile("test.pdf");
        BufferedImage image;
        for (int i = 0; i < doc.getPages().getCount(); i++) {
            image = doc.saveAsImage(i, PdfImageType.Bitmap, 200, 200);
            File file = new File( String.format("ToImage-img-%d.png", i));
            ImageIO.write(image, "PNG", file);
        }
        doc.close();