2

I am new in iText, I want to convert HTML string to Pdf in Itext. I found lots of example on the Internet. Then I found one example and want to implement same here, but not able to implement the example in my project. I added dependencies in my pom.XML file .

Here is the link :

https://developers.itextpdf.com/examples/xml-worker-itext5/basic-html-examples

Pom.xml

<!-- https://mvnrepository.com/artifact/com.itextpdf/itextpdf -->
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>itextpdf</artifactId>
        <version>5.0.6</version>
    </dependency>
**<!-- https://mvnrepository.com/artifact/com.itextpdf.tool/xmlworker -->
<dependency>
    <groupId>com.itextpdf.tool</groupId>
    <artifactId>xmlworker</artifactId>
    <version>5.4.1</version>
</dependency>**

Code snippet:

                PdfPTable table = new PdfPTable(1);
                PdfPCell cell = new PdfPCell();
                ElementList list =XMLWorkerHelper.parseToElementList(encountersoapnotes.getHpi(), null);
                for (Element element : list) {
                    cell.addElement(element);
                }
                table.addCell(cell);
                document.add(table);

The above parseToElementList(str,null) method is not available after installing maven command.

Anyone give me the maven dependency for this XMLWorkerHelper class which contains parseToElementList(str,null) method.

enter image description here

Thanks Sitansu

Sitansu
  • 3,225
  • 8
  • 34
  • 61
  • 1
    I added code now @AxelH – Sitansu Feb 07 '18 at 06:41
  • 1
    In the example you used, there is the duplicate URL in comment (because the example was create from that post). In the answer comments section. you can see the method was added in the version 5.5.4 – AxelH Feb 07 '18 at 06:51
  • @AxelH Thanks for sharing – Sitansu Feb 07 '18 at 06:56
  • 1
    You use iText version 5.0.6 and XMLWorker 5.4.1; this is bad, the versions should match. And as @AxelH says, that method has been introduced in version 5.5.4; so no wonder that method is missing. Thus, update your dependencies to current, matching versions. – mkl Feb 07 '18 at 07:44
  • @BrunoLowagie we need someone else to close it now ;-) every information about THAT problem is already explained in your post. For the newer version to use, this can't really be an answer to the question, so no point to keep that question. (PS: isn't it possible to add the minimal version of IText in the futur example/tutorial page or even better, using the `@since` tag in the javadoc of IText, it would be better for the documentation to now if an older project need to be updated or not ;) – AxelH Feb 07 '18 at 07:58
  • @mkl, the question is about the missing method in a class. That question occured after try to reproduce the example posted on IText, originally on SO, since the answer proposing the solution already mentioned in the comment section that the version should be at least 5.5.4, I don't see the point of having that question open. NOTE : I flagged this [duplicate](https://stackoverflow.com/questions/28472400/adding-pdfdiv-to-paragraph) but was lost by Bruno L. close/open manipulation... I might be a bit extreme here but it was documented on the tutorial. – AxelH Feb 07 '18 at 09:44
  • 1
    Ok, so I closed the question as duplicate to a set of three questions: [Brunos answer to "Adding PdfDiv to Paragraph" and comment](https://stackoverflow.com/a/28472955/1729265) show the required version, [Bruno's comment to "XML worker using itext"](https://stackoverflow.com/questions/18951790/xml-worker-using-itext#comment27992411_18951790) is about matching versions, and ["Converting HTML to PDF using iText"](https://stackoverflow.com/q/47895935/1729265) is more general about what should be used instead. – mkl Feb 07 '18 at 09:50
  • @AxelH Sorry, I misunderstood which post "in your post" in your comment to Bruno referred to. – mkl Feb 07 '18 at 09:54
  • @mkl I didn't notice my duplicated flag dissappeared until my last comment... my bad! – AxelH Feb 07 '18 at 09:59
  • Sorry for causing these misunderstanding. I shouldn't go on SO when I'm in a hurry. – Bruno Lowagie Feb 07 '18 at 16:07

1 Answers1

2

If you are new to iText, then you should not start with such an old version. You should start with the current version: iText 7.1.1 + pdfHTML 2.0.1. With that version, you need as little code as this:

public static void main() throws IOException {
   // IO File htmlSource = new File("input.html");
   File pdfDest = new File("output.pdf");
   // pdfHTML specific code
   ConverterProperties converterProperties = new ConverterProperties();
   HtmlConverter.convertToPdf(new FileInputStream(htmlSource), new FileOutputStream(pdfDest), converterProperties);
}

More information, tutorials, downloads etc at https://itextpdf.com/itext7/pdfHTML

Amedee Van Gasse
  • 7,280
  • 5
  • 55
  • 101