Search code examples
itextpdf-generationpie-charthtml2pdf

Generate PDF with pie chart -iText7


The obvious primary reason for using iText being , our company already have a license.

Requirement: Generate a report that includes pie chart. Our UX gave us a HTML, And I'm using html2pdf plugin to generate pdf. We're using tlx-chart for its simplest code.

Issue: PDF doesn't render/show piechart. No errors seen in logs. HTML is being created perfectly (Althought iText conversion seems to be not so good), and When I load it with chromedriver as provided in KB article below, it renders overlapped 2 piecharts instead of 1.

So, This knowledgebase article from iText doesn't help.

  • Infact, I've thought I cannot keep on updating chromedriver,selenium on prod server.
  • I've to provide a loosely coupled (OS>PDF>PIE) to generate PDF.

Code Snippet:

HtmlConverter.convertToPdf(reportHtml, pdfWriter, new ConverterProperties());

"<tlx-chart id=\"mychart\" chart-type=\"PieChart\" chart-editable=\"true\" chart-columns=\"${['Element','Percentage']}\" chart-data=\"${${pieData}}\" </tlx-chart>"

Please Help!


Solution

  • So the issue is to embed piechart in PDF since JavaScript tags were not being rendered by iText library. And I resolved this in my case using jFreeChart.

    • Generate an image using jFreechart and embed that image in HTML
    • iText perfectly renders this as per HTML CSS
    • For detailed explanation and code snippet on this please visit here
    • An alternative way is also documented here