Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xmlworker is missing in iText 7 core

Tags:

java

itext

itext7

I am trying to use iText 7 in Java. Want to covert HTML/XHTML to PDF.

Apparently xmlworker.jar doesn't exist in iText 7 core.

What is the replacement for iText 7?

Any solutions?

like image 486
Kino Fung Avatar asked Jun 02 '16 09:06

Kino Fung


2 Answers

XML Worker is the next thing on the road map at iText, so yes, it will be available for iText 7. But first we need to finish the port of iText 7 for Java to iText 7 for C# and we're still working hard on documenting iText 7. See for instance: iText 7: Building Blocks.

In open source, one releases often, one releases soon. Rather than keeping all code closed until everything is finished, we opted for the open source way of releasing: whatever is ready, gets released. Whatever isn't ready, will be released as soon as it's ready.

The major overhaul of iText requires us to rewrite XML Worker. The benefit: iText 7 was written with XML Worker in mind. All the items marked with a key in the tutorial I mentioned are "new in iText 7", e.g. inheritance of properties (which allows us to apply CSS in a much better way).

You'll see significant improvements when it's done.

like image 88
Bruno Lowagie Avatar answered Oct 13 '22 20:10

Bruno Lowagie


iText pdfHTML module has been released as a replacement for XmlWorker. C# version can be downloaded from the NuGet Gallery. Java version can be downloaded from the Artifactory.

The main class you are looking for is HtmlConverter. It has a lot of static method overloads for converting html either to a list of elements to be future added to layout structures, to a whole com.itextpdf.layout.Document instance, or right to the .pdf file.

Example of converting .html file to .pdf:

HtmlConverter.convertToPdf(new File(htmlFilePath), new File(outPdfFilePath));

Example of converting html to layout elements:

String html = "<p>Hello world!</p>";
List<IElement> lst = HtmlConverter.convertToElements(html);

Also, pdfHTML now supports @media rules, so you might want to provide a configuration which will be used for applying CSS, for instance to use @media print instructions, you would need to set up MediaDeviceDescription accordingly:

ConverterProperties properties = new ConverterProperties()
     .setMediaDeviceDescription(new MediaDeviceDescription(MediaType.PRINT));
HtmlConverter.convertToPdf(new File(htmlPath), new File(outPdfPath), properties);

To specify the set of fonts you would like to use when converting HTML to PDF, you can also set up a FontProvider:

FontProvider fontProvider = new FontProvider();
fontProvider.addDirectory(fontsDir)
properties.setFontProvider(fontProvider);

Or you can use the DefaultFontProvider and specify its settings in the constructor:

FontProvider fontProvider = new DefaultFontProvider(false, false, true);
properties.setFontProvider(fontProvider);

DefaultFontProvider has three parameters in the constructor: first one is to specify whether to use Standard fonts (Helvetica, Courier, Times etc), second one to specify whether to use fonts that are shipped with pdfHTML, and the third one to specify whether to load system fonts. DefaultFontProvider is just a subclass of FontProvider and therefore you can always call addDirectory or addFont after the instance has been created.

like image 39
Alexey Subach Avatar answered Oct 13 '22 18:10

Alexey Subach