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

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

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

问题

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

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

下面是相关内容:

1. 触发该过程的方法

  1. @GetMapping(path = "/split")
  2. public String splitHtml2Pdf() {
  3. try {
  4. String inputHtml = "<html><body><landscape>Content 1</landscape><landscape>Content 2</landscape></body></html>";
  5. Map<Integer, PdfDocument> documentMap = splitHtmlToPdf.splitAndCreateDocuments(inputHtml);
  6. String outputPdfPath = "/Downloads/output.pdf";
  7. splitHtmlToPdf.createFinalPdf(documentMap, outputPdfPath);
  8. } catch (Exception e) {
  9. throw new RuntimeException(e);
  10. }
  11. return "DONE SPLIT HTML2PDF!";
  12. }

2. 两个触发的方法

  1. public Map<Integer, PdfDocument> splitAndCreateDocuments(String inputHtml) throws IOException {
  2. var converterProperties = new ConverterProperties();
  3. Map<Integer, PdfDocument> documentMap = new HashMap<>();
  4. String[] segments = inputHtml.split("<landscape>");
  5. for (int i = 1; i < segments.length; i++) {
  6. String segmentHtml = segments[i];
  7. PdfDocument pdfDocument = new PdfDocument(new PdfWriter("temp_" + i + ".pdf"));
  8. HtmlConverter.convertToPdf(segmentHtml, pdfDocument, converterProperties);
  9. documentMap.put(i, pdfDocument);
  10. }
  11. return documentMap;
  12. }
  13. public void createFinalPdf(Map<Integer, PdfDocument> documentMap, String outputPath) throws IOException {
  14. PdfDocument finalPdf = new PdfDocument(new PdfWriter(outputPath));
  15. PdfMerger merger = new PdfMerger(finalPdf);
  16. for (Map.Entry<Integer, PdfDocument> entry : documentMap.entrySet()) {
  17. PdfDocument pdfDocument = entry.getValue();
  18. // 此处抛出错误
  19. int docNrPages = pdfDocument.getNumberOfPages();
  20. merger.merge(pdfDocument, 1, docNrPages);
  21. pdfDocument.close();
  22. }
  23. finalPdf.close();
  24. }

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

  1. int docNrPages = pdfDocument.getNumberOfPages();

4. 关闭文档的调用

  1. 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

  1. @GetMapping(path = &quot;/split&quot;)
  2. public String splitHtml2Pdf() {
  3. try {
  4. 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;;
  5. Map&lt;Integer, PdfDocument&gt; documentMap = splitHtmlToPdf.splitAndCreateDocuments(inputHtml);
  6. String outputPdfPath = &quot;/Downloads/output.pdf&quot;;
  7. splitHtmlToPdf.createFinalPdf(documentMap, outputPdfPath);
  8. } catch (Exception e) {
  9. throw new RuntimeException(e);
  10. }
  11. return &quot;DONE SPLIT HTML2PDF!&quot;;
  12. }

2. the two methods triggered

  1. public Map&lt;Integer, PdfDocument&gt; splitAndCreateDocuments(String inputHtml) throws IOException {
  2. var converterProperties = new ConverterProperties();
  3. Map&lt;Integer, PdfDocument&gt; documentMap = new HashMap&lt;&gt;();
  4. String[] segments = inputHtml.split(&quot;&lt;landscape&gt;&quot;);
  5. for (int i = 1; i &lt; segments.length; i++) {
  6. String segmentHtml = segments[i];
  7. PdfDocument pdfDocument = new PdfDocument(new PdfWriter(&quot;temp_&quot; + i + &quot;.pdf&quot;));
  8. HtmlConverter.convertToPdf(segmentHtml, pdfDocument, converterProperties);
  9. documentMap.put(i, pdfDocument);
  10. }
  11. return documentMap;
  12. }
  13. public void createFinalPdf(Map&lt;Integer, PdfDocument&gt; documentMap, String outputPath) throws IOException {
  14. PdfDocument finalPdf = new PdfDocument(new PdfWriter(outputPath));
  15. PdfMerger merger = new PdfMerger(finalPdf);
  16. for (Map.Entry&lt;Integer, PdfDocument&gt; entry : documentMap.entrySet()) {
  17. PdfDocument pdfDocument = entry.getValue();
  18. // HERE THE METHOD IS THROWN
  19. int docNrPages = pdfDocument.getNumberOfPages();
  20. merger.merge(pdfDocument, 1, docNrPages);
  21. pdfDocument.close();
  22. }
  23. finalPdf.close();
  24. }

3. THE ERROR IS THOWRON BY the call

  1. int docNrPages = pdfDocument.getNumberOfPages();

4. and the call witch close the document is

  1. 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来检索它。

一个代码示例:

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

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:

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

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:

确定