DEV Community

Cover image for HTML to PDF — Java
Erwan Le Tutour
Erwan Le Tutour

Posted on

2

HTML to PDF — Java

Use Case

At the beginning of the year 2020, my wife wanted to redo her CV, so I offered to give her a small tool where she had only to inform her trainings, her diplomas and her experiences and who would then put them in a document.
At first I wanted to create a document in word format, but I was not satisfied with the rendering, the HTML of the frontend looked better than the final document, that’s when I wondered if there was no way to turn this HTML into PDF.
Which led to the use of html2pdf, my frontend sent me an object corresponding to her CV and I just had to format it and generate the PDF.

HTML2PDF

The Dependency

First imported via your favorite dependency manager (here maven).

<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>html2pdf</artifactId>
<version>3.0.0</version>
</dependency>
view raw html2pdf.xml hosted with ❤ by GitHub

What is a PDF?

What is a PDF? It’s a file format, so let’s start from the beginning, create a file (thanks captain obvious).

Fill the File

To do this, nothing is simpler than to use the HtmlConverter class and its convertToPdf method which takes as a parameter :

  • A String : The HTML code as a String

  • An OutputStream : The PDF file.

    File f = new File(fileName + ".pdf");
    HtmlConverter.convertToPdf(html.toString(), new FileOutputStream(f));
    In the above example, the html variable is a StringBuilder that is built by browsing the properties of a previously populated object. Warning, tags such as are not taken into account, so how to insert css?

That simple :

  • Use the style property of the HTML tag

    <span style=”font-weight: bold;”>

  • use tags and feed the content between the two by the content of a css file that you will read before

    private void style(StringBuilder html) {
    html.append("<style>");
    String content = "";
    try {
    File file = ResourceUtils.getFile("classpath:static/bootstrap.min.css");
    content = Files.readString(file.toPath(), StandardCharsets.UTF_8);
    } catch (IOException e) {
    e.printStackTrace();
    }
    html.append(content);
    html.append("</style>");
    }
    And then use the class property of the HTML tag to add your style

    html.append("

    ")

    Warning : complex file like the Bootstrap css can produce some errors in the console of your IDE, but nothing to fear, the file will be generated anyway.

    pros and cons

    Pro

    • Easy to use.

    • Well documented.

    • Converting HTML to PDF is a fraction of the possibility of this library.

    Cons

    • Code redundancy if the object in question is already displayed on the frontend side.

    • Complex CSS files can generate console errors.

    • Maintenance difficulty in case of a large file.

Sentry blog image

How I fixed 20 seconds of lag for every user in just 20 minutes.

Our AI agent was running 10-20 seconds slower than it should, impacting both our own developers and our early adopters. See how I used Sentry Profiling to fix it in record time.

Read more

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay