文档已关闭。无法执行pdfDocument.getNumberOfPages()上的操作。

huangapple go评论56阅读模式
英文:

Document was closed. It is impossible to execute action on pdfDocument.getNumberOfPages()

问题

我在处理iText7(v7.2.5)和html2pdf(v4.0.5)中的PDF文档时遇到了这个错误:

com.itextpdf.kernel.exceptions.PdfException: Document was closed. It is impossible to execute action.

下面是相关内容:

1. 触发该过程的方法

@GetMapping(path = "/split")
public String splitHtml2Pdf() {

    try {
        String inputHtml = "<html><body><landscape>Content 1</landscape><landscape>Content 2</landscape></body></html>";
        Map<Integer, PdfDocument> documentMap = splitHtmlToPdf.splitAndCreateDocuments(inputHtml);

        String outputPdfPath = "/Downloads/output.pdf";
        splitHtmlToPdf.createFinalPdf(documentMap, outputPdfPath);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    return "DONE SPLIT HTML2PDF!";
}

2. 两个触发的方法

public Map<Integer, PdfDocument> splitAndCreateDocuments(String inputHtml) throws IOException {
    
    var converterProperties = new ConverterProperties();
    
    Map<Integer, PdfDocument> documentMap = new HashMap<>();
    String[] segments = inputHtml.split("<landscape>");
    
    for (int i = 1; i < segments.length; i++) {
        String segmentHtml = segments[i];
        PdfDocument pdfDocument = new PdfDocument(new PdfWriter("temp_" + i + ".pdf"));
    
        HtmlConverter.convertToPdf(segmentHtml, pdfDocument, converterProperties);
    
        documentMap.put(i, pdfDocument);
    }
    
    return documentMap;
}


public void createFinalPdf(Map<Integer, PdfDocument> documentMap, String outputPath) throws IOException {
    PdfDocument finalPdf = new PdfDocument(new PdfWriter(outputPath));
    PdfMerger merger = new PdfMerger(finalPdf);
    
    for (Map.Entry<Integer, PdfDocument> entry : documentMap.entrySet()) {
        PdfDocument pdfDocument = entry.getValue();
        // 此处抛出错误
        int docNrPages = pdfDocument.getNumberOfPages();
        merger.merge(pdfDocument, 1, docNrPages);
        pdfDocument.close();
    }
    
    finalPdf.close();
}

3. 错误发生在以下调用处

int docNrPages = pdfDocument.getNumberOfPages();

4. 关闭文档的调用

HtmlConverter.convertToPdf(segmentHtml, pdfDocument, converterProperties);

问题:

有什么方法可以解决这个问题吗?

英文:

I got this error on handling a PDF document on itext7 (v7.2.5) and html2pdf (v4.0.5)

com.itextpdf.kernel.exceptions.PdfException: Document was closed. It is impossible to execute action.

Below you'll find:

1. the method witch trigger the process

@GetMapping(path = &quot;/split&quot;)
public String splitHtml2Pdf() {

	try {
		String inputHtml = &quot;&lt;html&gt;&lt;body&gt;&lt;landscape&gt;Content 1&lt;/landscape&gt;&lt;landscape&gt;Content 2&lt;/landscape&gt;&lt;/body&gt;&lt;/html&gt;&quot;;
		Map&lt;Integer, PdfDocument&gt; documentMap = splitHtmlToPdf.splitAndCreateDocuments(inputHtml);

		String outputPdfPath = &quot;/Downloads/output.pdf&quot;;
		splitHtmlToPdf.createFinalPdf(documentMap, outputPdfPath);
	} catch (Exception e) {
		throw new RuntimeException(e);
	}

	return &quot;DONE SPLIT HTML2PDF!&quot;;
}

2. the two methods triggered

public Map&lt;Integer, PdfDocument&gt; splitAndCreateDocuments(String inputHtml) throws IOException {

    var converterProperties = new ConverterProperties();

    Map&lt;Integer, PdfDocument&gt; documentMap = new HashMap&lt;&gt;();
    String[] segments = inputHtml.split(&quot;&lt;landscape&gt;&quot;);

    for (int i = 1; i &lt; segments.length; i++) {
        String segmentHtml = segments[i];
        PdfDocument pdfDocument = new PdfDocument(new PdfWriter(&quot;temp_&quot; + i + &quot;.pdf&quot;));

        HtmlConverter.convertToPdf(segmentHtml, pdfDocument, converterProperties);

        documentMap.put(i, pdfDocument);
    }

    return documentMap;
}


public void createFinalPdf(Map&lt;Integer, PdfDocument&gt; documentMap, String outputPath) throws IOException {
    PdfDocument finalPdf = new PdfDocument(new PdfWriter(outputPath));
    PdfMerger merger = new PdfMerger(finalPdf);

    for (Map.Entry&lt;Integer, PdfDocument&gt; entry : documentMap.entrySet()) {
        PdfDocument pdfDocument = entry.getValue();
        // HERE THE METHOD IS THROWN
        int docNrPages = pdfDocument.getNumberOfPages();
        merger.merge(pdfDocument, 1, docNrPages);
        pdfDocument.close();
    }

    finalPdf.close();

}

3. THE ERROR IS THOWRON BY the call

int docNrPages = pdfDocument.getNumberOfPages();

4. and the call witch close the document is

HtmlConverter.convertToPdf(segmentHtml, pdfDocument, converterProperties);

QUESTION:

Any idea how can I solve this issue?

答案1

得分: 2

你可以通过调用HtmlConverter提供的另一个方法来防止立即关闭PdfDocument实例:例如,HtmlConverter#convertToDocument的某个重载将返回一个未关闭的Document对象。你可以提供PdfDocument对象或PdfWriter作为参数,例如。

如果你没有提供PdfDocument对象作为convertToDocument方法之一的参数,仍然可以通过调用Document#getPdfDocument来检索它。

一个代码示例:

public byte[] convertPdf(String inFile) throws IOException {
    var baos = new ByteArrayOutputStream();
    var pdf = new PdfDocument(new PdfWriter(baos));
    var doc = HtmlConverter.convertToDocument(new FileInputStream(inFile), pdf, new ConverterProperties());
    System.out.println(pdf.getNumberOfPages());
    doc.close();
    return baos.toByteArray();
}
英文:

You can prevent immediate closing of the PdfDocument instance by calling another Method provided by HtmlConverter: One of the HtmlConverter#convertToDocument overloads for example will return a Document object that isn't closed. You can provide a PdfDocument object or PdfWriter as parameter for example.

In case you don't provide a PdfDocument object as parameter to one of the convertToDocument methods, you can still retrieve it by calling Document#getPdfDocument

A code example:

    public byte[] convertPdf(String inFile) throws IOException {
    var baos = new ByteArrayOutputStream();
    var pdf = new PdfDocument(new PdfWriter(baos));
    var doc = HtmlConverter.convertToDocument(new FileInputStream(inFile),pdf,new ConverterProperties());
    System.out.println(pdf.getNumberOfPages());
    doc.close();
    return baos.toByteArray();
}

huangapple
  • 本文由 发表于 2023年8月10日 19:44:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76875430.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定