2016-11-17 25 views
-1

私はHTMLテンプレートを解析し、itexライブラリを使用してpdfを変換しようとしています。ステージングとqaでは問題なく動作していますが、production.Belowはサンプルですコード。以下はメモリからのヒープスペースエラーitextを通じてpdfを生成中

String template = null; 
    Document document = null; 
    HTMLWorker htmlWorker; 
    OutputStream outputStream = null; 
    document = new Document(); 
    document.open(); 
    document.addTitle("SeatSeller Agent Invoice"); 
    document.addHeader("Content-Disposition", "attachment"); 
    document.addHeader("name", "redbusTicket"); 
    document.addHeader("filename", "test.pdf"); 
    document.addHeader("Content-Type", "application/pdf"); 
    document.addHeader("Content-ID", "test.pdf"); 
    document.addHeader("Content-Description", "SeatSellerInvoice.pdf"); 
    htmlWorker = new HTMLWorker(document); 
    Handlebars handlebars = new Handlebars(); 
    String filePath = null; 

    template = handlebars.compile("invoicepdftemplate").apply(creditInvoice); 
    filePath = creditInvoice.getAccount().toString() + "_" 
     + MMM_FORMATTER.format(creditInvoice.getDate()) 
     + YYYY_FORMATTER.format(creditInvoice.getDate()) + ".pdf"; 


    htmlWorker.parse(new StringReader(template)); 

    document.close(); 

    outputStream = new FileOutputStream(filePath); 

    ITextRenderer iTextRenderer = new ITextRenderer(); 
    iTextRenderer.setDocumentFromString(template); 
    iTextRenderer.layout(); 
    iTextRenderer.createPDF(outputStream); 
    File fileToBeUploaded = new File(filePath); 

エラー - のスタックトレースです:

java.lang.OutOfMemoryError: Java heap space 
    at com.lowagie.text.pdf.codec.PngImage.decodeIdat(Unknown Source) ~[itext-2.1.7.jar:na] 
    at com.lowagie.text.pdf.codec.PngImage.getImage(Unknown Source) ~[itext-2.1.7.jar:na] 
    at com.lowagie.text.pdf.codec.PngImage.getImage(Unknown Source) ~[itext-2.1.7.jar:na] 
    at com.lowagie.text.pdf.codec.PngImage.getImage(Unknown Source) ~[itext-2.1.7.jar:na] 
    at com.lowagie.text.Image.getInstance(Unknown Source) ~[itext-2.1.7.jar:na] 
    at com.lowagie.text.Image.getInstance(Unknown Source) ~[itext-2.1.7.jar:na] 
    at com.lowagie.text.html.simpleparser.HTMLWorker.startElement(Unknown Source) ~[itext-2.1.7.jar:na] 
    at com.lowagie.text.xml.simpleparser.SimpleXMLParser.processTag(Unknown Source) ~[itext-2.1.7.jar:na] 
    at com.lowagie.text.xml.simpleparser.SimpleXMLParser.go(Unknown Source) ~[itext-2.1.7.jar:na] 
    at com.lowagie.text.xml.simpleparser.SimpleXMLParser.parse(Unknown Source) ~[itext-2.1.7.jar:na] 
    at com.lowagie.text.html.simpleparser.HTMLWorker.parse(Unknown Source) ~[itext-2.1.7.jar:na] 

はcode.Productionマシンのメモリ内の任意のメモリリークまたはいずれかの問題があるのX ms-256 Xmxの-365.Canの誰もが

を助けてくださいています
+0

あなたはOOMEを取得していますか?スタックトレースを共有します。 – rkosegi

+1

手元の問題とは別に、あなたは 'HTMLWorker'を使用しています。それは2011年に 'XMLWorker'に取って代わられました。 –

+2

適切なiTextサンプルに従ってください。あなたはiTextの 'Document'を作成しますが、ライターは付けないので、あなたはどこにもPDFを書くことができません。次にiTextの* part *ではなく、他のライブラリで使われているiTextの機能のための*ラッパー*である 'ITextRenderer'を使います。さらに更新を検討する。 iText 2.1.7は古代です。 – mkl

答えて

-1

outputStream =新しいFileOutputStream(filePath); ここに: - あなたのコードでoutputStreamが閉じられていません。 これを一度確認するか、リソースを試してください。

https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html

+0

FileOutputStreamはヒープメモリを消費する必要はありません。クローズするとFDのみが解放されます(http://hg.openjdk.java.net/jdk7/jdk7/jdk/file/9b8c96f96a0f/src/windows/native/java/io/FileOutputStream_md.c) – rkosegi

+0

@rkosegi次に、OutputStreamが実装されている理由閉じることができますか? –

+1

FDを閉じる必要があるためです。ソースbtwを確認してください。 (自動)Closeableを実装しても、オブジェクトが大量のヒープを消費しているわけではありません。 – rkosegi

0

問題は、私は私のHTMLテンプレートで400キロバイトのサイズの画像を使用していたし、それがメモリエラーの外に投げていたHTMLファイルを解析しながら、生産機械は非常に少ないmemory.Soを持っているということでした。

関連する問題