英文:
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 = "/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. the two methods triggered
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();
// 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();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论