我正在使用 Jpedal 工具将PDF转换为图片。 Jpedal工具有助于将任何或大量PDF页面(比方说PDF页面= 1200)转换为.PNG图像。一般情况下这是有效的,但是在将这些大量PDF页面转换为图像时,它会导致应用程序和tomcat在没有任何信息或日志的情况下停止。
它似乎只是一个没有打印的 OutOfMemoryError 。处理时会打印行 logger.info(“width =”+ width +“height =”+ height); 多次,但在某些时候退出并停止tomcat。 任何人都可以帮助做同样的事情。 我的代码: -
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);
}