Java写入PDF后无法删除图像文件,itext7 html To Pdf转换图像文件锁定

时间:2018-03-30 02:20:50

标签: java tomcat delete-file itext7 html-to-pdf

我正在从HTML模板创建一个多页PDF文件。还有一些我用PDF格式显示的图像。 PDF生成成功,图像添加到PDF文件中。 但在此之后,当我想删除他们没有删除的图像时,弹出窗口显示文件被锁定。 当我杀死写PDF文件的程序时,你可以删除图像。

public void createPdf(String htmlString) throws IOException {
     PdfWriter writer = new PdfWriter("C:\\Users\\DELL\\Desktop\\filename.pdf");
     PdfDocument pdf = new PdfDocument(writer);

     ConverterProperties properties = new ConverterProperties();
     HtmlConverter.convertToPdf(htmlString, pdf, properties);

     pdf.close();
     writer.close();
}

注意

  1. 我正在将HTML 文字转换为pdf。
  2. 在html文本中, img 标签用于从本地文件系统插入外部图像。

1 个答案:

答案 0 :(得分:1)

这确实是iText7中的一个错误。它已经修复,修复版将在7.1.2版本中提供(相应的pdfHTML 2.0.2)。

如果您想使用固定版本,您可以自己从源代码构建iText Core。解决问题的提交是4565db5de87666dc60d65befb463a0519fb8334d。您不必构建自定义pdfHTML版本,因为修复仅在iText7 Core中。

可以使用构建说明here