我正在使用Jpedal工具将 PDF 转换为图像。Jpedal 工具有助于将任何或大量 PDF 页面(假设 PDF 页面 = 1200)转换为 .PNG 图像。一般来说,这是可行的,但是在将这些大量 PDF 页面转换为图像期间,有时会导致 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);
}
我必须找出问题所在,以及为什么会导致 tomcat 停止。在日志中,我只能看到以下几行。也不例外。执行后logger.info("width = "+ width + " height="+height); 它停止了Tomcat。
[INFO ] 2015-09-02 04:49:45 com.pearson.ebook.pdfprocessors.PDF2IMAGEConversionTask.PDF2ImageConversationTask(PDF2IMAGEConversionTask.java:115) - Creating page level image using JPedal tool
[INFO ] 2015-09-02 04:49:45 com.pearson.ebook.pdfprocessors.PDF2IMAGEConversionTask.createPDF2ImageTask(PDF2IMAGEConversionTask.java:297) - createPDF2ImageTask ( sourcePDFAbsPath = /export/home/apps/ebookcm/dev1/content/simpleebook/curriculum/hcl_test/test1/test2_jg/ingest/pdf/cropped-pdf//7cad530e57768d8d7361baebf0b51b47.pdf , destinationImageAbsPath = /export/home/apps/ebookcm/dev1/content/simpleebook/curriculum/hcl_test/test1/test2_jg/ebookCM29922739/1.0/dlimages/7cad530e57768d8d7361baebf0b51b47.png, scalingFactor = 2.0 , fileFormat = png softLimitInKB =400
[INFO ] 2015-09-02 04:49:45 com.pearson.ebook.helper.Helper.deleteFile(Helper.java:381) - deleteFile sourceFile=/export/home/apps/ebookcm/dev1/content/simpleebook/curriculum/hcl_test/test1/test2_jg/ebookCM29922739/1.0/dlimages/7cad530e57768d8d7361baebf0b51b47.png
[INFO ] 2015-09-02 04:49:45 com.pearson.ebook.pdfprocessors.PDF2IMAGEConversionTask.createPDF2ImageTask(PDF2IMAGEConversionTask.java:334) - width = 1296 height= 1566