Steven Hovland
Steven Hovland

Reputation: 67

itext html to pdf without embedding fonts

I'm following this guide in Chapter 6 of iText 7: Converting HTML to PDF with pdfHTML on adding extra fonts:

public static final String FONT = "src/main/resources/fonts/cardo/Cardo-Regular.ttf";
public void createPdf(String src, String font, String dest) throws IOException {
    ConverterProperties properties = new ConverterProperties();
    FontProvider fontProvider = new DefaultFontProvider(false, false, false);
    FontProgram fontProgram = FontProgramFactory.createFont(font);
    fontProvider.addFont(fontProgram, "Winansi");
    properties.setFontProvider(fontProvider);
    HtmlConverter.convertToPdf(new File(src), new File(dest), properties);
}

While it's working as expected and embedding subsets of the fonts being used, I'm wondering if there is a way for the resulting PDF document to not embed the fonts at all. This is possible when creating BaseFont instances and setting the embedded property to false and using them to build various PDF building blocks. What I'm looking for is this same behavior when using the HtmlConverter.convertToPdf().

Upvotes: 2

Views: 1732

Answers (1)

Alexey Subach
Alexey Subach

Reputation: 12312

What you should normally do is override FontProvider:

FontProvider fontProvider = new DefaultFontProvider(false, false, false) {
    @Override
    public boolean getDefaultEmbeddingFlag() {
        return false;
    }
};

However, the problem is that at the moment this font provider would be overwritten by pdfHTML further into the pipeline in ProcessorContext#reset.

While this issue is not fixed in iText you can build a custom version of pdfHTML for your needs. The repo is located at https://github.com/itext/i7j-pdfhtml and you are interested in this line. Just replace it with the overload as above and build the jar.

UPD The fix is available starting from pdfHTML 2.1.3. From that version on you can use custom font providers freely.

Upvotes: 2

Related Questions