Search code examples
javaitextitext7

Html String to Pdf conversion in Java, Styling is not working as expected


I am trying to convert HTML string to PDF in java using itext library. Conversion of html to pdf is fine but styling is not working as expected. Here is the html page which I want to convert to PDF. Can anyone please help me to convert this Html string to PDF with proper styling as shown in link. Here is the code which I tried in Java

HtmlConverter.convertToPdf(new FileInputStream("index.html"), 
new FileOutputStream("index-to-pdf.pdf"));

Depedency which I used are

<dependencies>
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>itext7-core</artifactId>
        <version>7.1.9</version>
        <type>pom</type>
    </dependency>
    <dependency>
        <groupId>com.itextpdf</groupId>
        <artifactId>html2pdf</artifactId>
        <version>2.1.6</version>
    </dependency>

Please help me to create a PDF with styling from html string using non paid library.


Solution

  • without the whole code, it is unable to understand your problem. but I used your HTML reference and it's working perfectly fine for generating HTML to pdf using the above code and library.

    here is a link to my repository with your HTML you can check.

    I am adding a sample of your pdf after generating it with CSSenter image description here.