6

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?

Amedee Van Gasse
  • 7,280
  • 5
  • 55
  • 101
Kino Fung
  • 61
  • 1
  • 3
  • As for now, XmlWorker is not available for iText7 – Alexey Subach Jun 03 '16 at 08:11
  • Thank your for reply. I am evaluating features of iText 7. I hear that support and updates seems no longer available for iText 5 after 2017, what is the schedule for the release of xmlworker in iText 7? Whether the feature is no longer available after iText 7 or not? – Kino Fung Jun 03 '16 at 10:05

2 Answers2

5

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.

Bruno Lowagie
  • 75,994
  • 9
  • 109
  • 165
  • @Bruno Is this answer still valid. I see a html2pdf on the site. – HopeKing Oct 26 '17 at 09:29
  • @HopeKing Yes, we have released [pdfHTML](https://itextpdf.com/itext7/pdfHTML) and yes, it's a tad confusing that the package names / Maven id is `html2pdf` (html2pdf = pdfHTML). You can find a tutorial here: https://developers.itextpdf.com/content/itext-7-converting-html-pdf-pdfhtml – Bruno Lowagie Oct 26 '17 at 09:57
  • @Bruno Thanks. Looks like this is not available on maven and needs a license key rightway. Is there a way to test how it works ? – HopeKing Oct 26 '17 at 10:05
  • @Bruno I use maven as my build tool. Hence i was using html2pdf from https://itextpdf.com/blog/itext-702-release but I get a error "Missing artifact com.itextpdf:html2pdf:jar:1.0.1". Is there a maven dependency in your link ? Sorry, if I missed something obvious – HopeKing Oct 26 '17 at 10:18
  • @BrunoLowagie can you help please. Thanks. – HopeKing Oct 27 '17 at 12:12
5

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.

Alexey Subach
  • 11,903
  • 7
  • 34
  • 60