1

我正在使用 Jpedal 工具将 PDF 转换为图像。当 PDF 页面的数量非常大并且我们对其进行处理以进行转换时,tomcat 会停止并抛出异常-

  javax.imageio.IIOException: I/O error writing PNG file.

任何人都可以请帮忙。

public boolean createPDF2ImageTask(String sourcePDFAbsPath, String destinationImageAbsPath, Float scalingFactor, String fileFormat, int softLimitInKB) throws Exception
    { 

        System.setProperty("org.jpedal.flattenForm","true");
        logger.info("createPDF2ImageTask ( sourcePDFAbsPath = "+sourcePDFAbsPath+" , destinationImageAbsPath = "+destinationImageAbsPath+ ", scalingFactor = "+scalingFactor+ " , fileFormat = "+fileFormat+ " softLimitInKB ="+softLimitInKB );
        boolean status = true;
        Float newScalingFactor;
        int sizeOfImageInKB;

        //PdfDecoder object provides the conversion 

        PdfDecoderServer decoder = null;
        Map mapValues = null;
        BufferedImage imageToSave = null;
        BufferedOutputStream bufferedOutputStream = null;
        long startTime = System.currentTimeMillis();
        try
        {

            Helper.deleteFile(destinationImageAbsPath);

            //mappings for non-embedded fonts to use
            FontMappings.setFontReplacements();

            decoder = new PdfDecoderServer(true);
            decoder.openPdfFile(sourcePDFAbsPath);


            mapValues = new HashMap();


            mapValues.put(JPedalSettings.EXTRACT_AT_BEST_QUALITY_MAXSCALING, 2);

            //alternatively secify a page size (aspect ratio preserved so will do best fit)
            //set a page size (JPedal will put best fit to this)
            PdfPageData pageData = decoder.getPdfPageData();
            int width = (int)(scalingFactor*pageData.getCropBoxWidth(1));
            int height = (int)(scalingFactor*pageData.getCropBoxHeight(1));
            logger.info("width = "+ width + "   height= "+height);          
            mapValues.put(JPedalSettings.EXTRACT_AT_PAGE_SIZE, new String[]{String.valueOf(width),String.valueOf(height)});

            //which takes priority (default is false)
            mapValues.put(JPedalSettings.PAGE_SIZE_OVERRIDES_IMAGE, Boolean.TRUE);

            PdfDecoderServer.modifyJPedalParameters(mapValues);                              

            /////////////////////////////////////////////////////////////////////////////////////

            try
            {
                imageToSave = decoder.getPageAsHiRes(1, null, false);
                decoder.flushObjectValues(true);
                if(imageToSave != null)
                {
                    logger.info("Start saving image as a file");
                    bufferedOutputStream = new BufferedOutputStream(new FileOutputStream(new File(destinationImageAbsPath)));

                    ImageIO.write(imageToSave, fileFormat, bufferedOutputStream);

                }
                else
                {
                    throw new Exception("imageToSave is null, Exception in extractPageAsImage ");
                }
            }
            catch(Exception e)
            {
                logger.error("Exception in extractPageAsImage :: "+e);
                logger.error("Exception stack trace in extractPageAsImage :: ",e);
                throw new Exception("Exception in extractPageAsImage :: "+e);
            }

它抛出异常 - extractPageAsImage :: javax.imageio.IIOException 中的异常:写入 PNG 文件的 I/O 错误!

4

1 回答 1

0

也许您可以尝试通过增加 Tomcat 实例的内存来解决这个问题。在 Unix 平台上,这可以通过添加一些 Java 选项来实现setenv.sh

export JAVA_OPTS="$JAVA_OPTS -Xms128m -Xmx2048m -XX:MaxPermSize=512m"

或在文件中的 Windows 上setenv.bat

set "JAVA_OPTS=%JAVA_OPTS% -Xms128m -Xmx2048m -XX:MaxPermSize=512m" 

请注意,以上示例中的数字只是示例,因为它们取决于您平台上的可用内存。

有关如何在您的平台上配置 Tomcat 的更多详细信息,请参阅Tomcat 的 RUNNING.txt 。

于 2015-09-03T17:54:33.390 回答