简体   繁体   English

在itext7中将html转换为pdf时出错

[英]error while convert html to pdf in itext7

To convert my html to pdf i use itext7's API convertToDocument, passing parameters the template's ByteArrayInputStream, the PDFDocument and convertProperties. 要将我的html转换为pdf,我使用itext7的API convertToDocument,将模板的ByteArrayInputStream,PDFDocument和convertProperties传递给参数。

relevant code snippet: 相关代码段:

HtmlConverter.convertToDocument(new ByteArrayInputStream(templateWritten), pdfDocument, converterProps);

As docs says, if I set baseURI of convertProperties there is no problem, but if I set PDF fonts this error shows up when the are many concurrent calls: 如文档所述,如果我设置convertProperties的baseURI没问题,但是如果我设置PDF字体,则在有许多并发调用时会显示此错误:

"Pdf indirect object belongs to other PDF document. Copy object to current pdf document.” “ Pdf间接对象属于其他PDF文档。将对象复制到当前pdf文档。”

creation of convert properties 创建转换属性

private ConverterProperties addResourcesForInitiative(String templateKey, FontProvider fontProvider) {
//        CustomDefaultFontProvider cdfp = new CustomDefaultFontProvider();
        ConverterProperties converterprops = new ConverterProperties();
//        converterprops.setFontProvider(fontProvider);
        converterprops.setBaseUri(ConfigurationManager.getParamValue("resource.path") + templateKey + "/resources/");
        log.info("Properties for conversione are setted. Url of folder loaded " + converterprops.getBaseUri());
        return converterprops;
    }

the object is created each call before convertToDocument API 在每次调用convertToDocument API之前都会创建对象

am I missing something? 我错过了什么吗?

thanks all for your help 感谢你的帮助

I experienced the same problem. 我遇到了同样的问题。 It is related to (but not a duplicate of) Itext7 generate pdf with Exception "Pdf indirect object belongs to other PDF document. Copy object to current pdf document." 它与Itext7生成pdf有关(但不是重复的), 但生成pdf异常,但“ Pdf间接对象属于其他PDF文档。将对象复制到当前pdf文档中。”

What I didn't understand about iText 7 at first, is that you have a FontProgram and a PdfFont . 一开始我对iText 7并不了解,因为您有一个FontProgram和一个PdfFont

  • FontProgram is a class that contains all the information that iText needs to use a font program. FontProgram是一个类,其中包含iText使用字体程序所需的所有信息。 It can be reused in a process that creates many different PDF files. 可以在创建许多不同PDF文件的过程中重复使用它。
  • PdfFont is a class that uses FontProgram in the context of a single document. PdfFont是在单个文档的上下文中使用FontProgram的类。 Each PdfFont object belongs to exactly one PdfDocument . 每个PdfFont对象完全属于一个PdfDocument

If you try to use a PdfFont object to create another document, you get the error "Pdf indirect object belongs to other PDF document. Copy object to current pdf document." 如果尝试使用PdfFont对象创建另一个文档,则会出现错误“ Pdf间接对象属于其他PDF文档。将对象复制到当前pdf文档。”

In other words: you can't just reuse PdfFont objects, only FontProgram objects. 换句话说:您不能仅重用PdfFont对象,而只能重用FontProgram对象。 This can be a problem when reusing ConverterProperties (or a FontProvider ). 重用ConverterProperties (或FontProvider )时,这可能是个问题。 The trick is not to cache PdfFont objects in a FontProvider , but cache the FontProgram instead. 诀窍是不要缓存PdfFont的对象FontProvider ,但缓存FontProgram代替。

As this is very confusing, I have asked the iText 7 development team to fix this. 因为这非常令人困惑,所以我已要求iText 7开发团队解决此问题。 When I look at the closed ticketing system, I can see that the documentation will be fixed, and that efforts will be done with respect to the ConverterProperties . 当我查看封闭式票务系统时,可以看到文档已修复,并且将对ConverterProperties This means that you'll see improvements in the next version. 这意味着您将在下一版本中看到改进。

In the meantime, I have fixed this for myself by changing the way I use a FontProvider and ConverterProperties . 同时,通过更改FontProviderConverterProperties使用方式,我自己解决了此问题。 New instances of PdfFont have to be created for every new document, and I understand why: PdfFont keeps track of the characters that are used in a specific document, and uses that information to create a font subset. 必须为每个新文档创建PdfFont新实例,我了解原因: PdfFont跟踪特定文档中使用的字符,并使用该信息创建字体子集。 That subset is different for every document; 每个文档的子集都不相同; hence every document needs a different PdfFont instance. 因此,每个文档都需要一个不同的PdfFont实例。

I will add a reference to this question in the tickets about this topic. 我将在有关该主题的故障单中添加对此问题的引用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM