user3573837
user3573837

Reputation: 1

Can itext convert html to pdf?

Can itext convert html to pdf. Not an entire screen, but something like this: A screen has a free form field where the user can enter something like this sentence enclosed in an HTML tags. This is a free form field that needs (bold tag here) important stuff bolded (end bold tag here) but nothing else bolded. I will need to get this information from the screen exactly as it was entered and I need create a report that will include it, but have it look like this: This is a free form field that needs important stuff bolded but nothing else bolded. Does itext have something that does this?

Upvotes: 0

Views: 213

Answers (2)

Mark
Mark

Reputation: 46

In old versions of itext you can use htmlWorker, but this is now deprecated. Now you should use the class XMLWorkerHelper and its parseXHtml method.

        try {
          Document document = new Document(PageSize.LETTER);
          PdfWriter pdfWriter = PdfWriter.getInstance
               (document, new FileOutputStream("c://test.pdf"));
          document.open();
          document.addTitle("test iText");

          XMLWorkerHelper worker = XMLWorkerHelper.getInstance();

          String str = "<html><head></head><body>"+
            "<h1>This is a title</h1>" +
            "<p>This is a paragraph</p>" +
            "</body></html>";
          worker.parseXHtml(pdfWriter, document, new StringReader(str));
          document.close();
          System.out.println("You should find you PDF in c:");
          }
        catch (Exception e) {
          e.printStackTrace();
        }

Hope this helps!

Upvotes: 1

user3291093
user3291093

Reputation:

I did give it a try a while ago and I must say it isn't the best option for converting PDF to HTML. At first I thought my PDF was badly formatted due to the CSS, but even without CSS it looked bad. So personally for me, I would say iText does convert HTML to PDF, but it's stylesheet support is questionable. Again, I could be wrong, due to recent updates.

Upvotes: 0

Related Questions