Reputation: 71
To convert my html to pdf i use itext7's API convertToDocument, passing parameters the template's ByteArrayInputStream, the PDFDocument and 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:
"Pdf indirect object belongs to other PDF document. Copy object to current pdf document.”
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
am I missing something?
thanks all for your help
Upvotes: 2
Views: 1178
Reputation: 77528
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."
What I didn't understand about iText 7 at first, is that you have a FontProgram
and a PdfFont
.
FontProgram
is a class that contains all the information that iText needs to use a font program. It can be reused in a process that creates many different PDF files.PdfFont
is a class that uses FontProgram
in the context of a single document. Each PdfFont
object belongs to exactly one 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."
In other words: you can't just reuse PdfFont
objects, only FontProgram
objects. This can be a problem when reusing ConverterProperties
(or a FontProvider
). The trick is not to cache PdfFont
objects in a FontProvider
, but cache the FontProgram
instead.
As this is very confusing, I have asked the iText 7 development team to fix this. 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
. 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
. 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. That subset is different for every document; hence every document needs a different PdfFont
instance.
I will add a reference to this question in the tickets about this topic.
Upvotes: 4