2

我收到堆空间错误/内存不足异常。我正在尝试使用 iText 生成 PDF 并使用aspose api. 正在生成的 PDF 有 3 页,我正在将该 PDF 逐页转换为图像,并将它们拼接成一个 jpg 图像。此代码在我的本地开发机器上运行良好,但在移动到测试服务器时出现异常。

我为此使用的代码是:

public void silSignedPDF(AgreementBean agBean,String sourceTemplatePDFURL, Hashtable<String, String> val, String destinationPDFPath) throws IOException, DocumentException, SQLException{
 String methodName = "silSignedPDF";
LogTracer.writeDebugLog(className, methodName, "Start");
String serverPath = System.getProperty("jboss.server.home.dir");
String sourceTemplatePDFURL1 = serverPath+AppConstants.PDL_Agreement_Template +"/Online_Installment_Agreement.pdf";
System.out.println("sourceTemplatePDFURL1 "+sourceTemplatePDFURL1);

File f = new File(sourceTemplatePDFURL1);
InputStream sourceTemplatePDFUrlStream = new BufferedInputStream(new FileInputStream(f));
File destinationFile = new File(destinationPDFPath+"/"+agBean.getDealNbr()+".pdf");
PdfReader reader = new PdfReader(sourceTemplatePDFUrlStream);
PdfStamper stamper = new PdfStamper(reader, new FileOutputStream(
        destinationFile));
AcroFields form = stamper.getAcroFields();

Enumeration enumeration = val.keys();
// iterate through Hashtable val keys Enumeration
while (enumeration.hasMoreElements()) {
    String nextElement = (String) enumeration.nextElement();
    String nextElementValue = (String) val.get(nextElement);
    form.setField(nextElement, nextElementValue);
}
stamper.setFormFlattening(true);
stamper.close();

PdfConverter pdf = new PdfConverter();
pdf.bindPdf(destinationPDFPath+"/"+agBean.getDealNbr()+".pdf");
try {
    pdf.doConvert();
} catch (Exception e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

//set start and end pages
pdf.setStartPage(1);
pdf.setEndPage(1);

//initialize conversion process

//convert pages to images
String suffix = ".jpg";
int imageCount = 1;
while (pdf.hasNextImage())
{
    try {
        pdf.getNextImage(destinationPDFPath+"/"+agBean.getDealNbr()+"_"+imageCount + suffix,ImageType.JPEG);
    } catch (Exception e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
   imageCount++;
}
/* PDFImages pdfDoc = new PDFImages (destinationPDFPath+"/"+agBean.getDealNbr()+".pdf", null);
for (int count = 0; count < pdfDoc.getPageCount(); ++count)
{
    pdfDoc.savePageAsJPEG(count,destinationPDFPath+"/"+agBean.getDealNbr()+"_"+count + ".png", 150, 0.8f);
}*/
File file1 = new File(destinationPDFPath+"/"+agBean.getDealNbr()+"_1" + ".jpg");
File file2 = new File(destinationPDFPath+"/"+agBean.getDealNbr()+"_2" + ".jpg");
File file3 = new File(destinationPDFPath+"/"+agBean.getDealNbr()+"_3" + ".jpg");

BufferedImage img1 = ImageIO.read(file1);
BufferedImage img2 = ImageIO.read(file2);
BufferedImage img3 = ImageIO.read(file3);

int widthImg1 = img1.getWidth();
int heightImg1 = img1.getHeight();
int heightImg2 = img2.getHeight();
int heightImg3 = img3.getHeight();

BufferedImage img = new BufferedImage(
widthImg1, 
heightImg1+heightImg2+heightImg3, 
BufferedImage.TYPE_INT_RGB);

img.createGraphics().drawImage(img1, 0, 0, null); 
img.createGraphics().drawImage(img2, 0, heightImg1, null);
img.createGraphics().drawImage(img3, 0, heightImg1+heightImg2, null);

File final_image = new File(destinationPDFPath+"/"+agBean.getDealNbr() + ".jpg");
ImageIO.write(img, "png", final_image);
file1.delete();
file2.delete();
file3.delete();
LogTracer.writeDebugLog(className, methodName, "End");
}
4

2 回答 2

1

使用 Aspose.Pdf 进行页面到图像转换需要比默认更多的内存。使用至少 512 MB 内存 (-Xmx512m) 运行程序。

于 2013-10-22T12:30:13.743 回答
1

应该为您的 JVM 更改堆大小,但不要将其更改为任何随机数。堆大小应根据系统使用的内存进行修改。您可以检查以获取进一步的规范。

于 2013-10-15T05:20:11.393 回答