Search code examples
javanode.jspdf-generationheadless-browserpdf-conversion

Best way to convert HTML page into PDF file


I am writing a new service Convert-HTML-TO-PDF. But now I am confused that what way should I prefer.

What ways I have to implement:

  • Use Head-less browser and capture the HTML page and convert to PDF
  • Use Java/Node Lib to convert. Which will create HTML relevant component in PDF file and then render?

Now, please help me to understand what will be the best way to implement a service and why!

[update]

And what will be the advantages and disadvantages of each approach


Solution

  • In my view, the best way forward always depends on what you already have experience with and what approach you take. There is no right or wrong here, everyone has to decide that for themselves based on their preferences.

    Each approach has advantages and disadvantages. Some of them are:

    Headless Browser:
    Advantage:

    • No large Libs necessary, therefore very memory saving

    Disadvantage

    • the desired browser must be installed on the computer/server
    • rendering may differ for different browsers

    Library:
    Advantage:

    • different libraries available
    • for the popular libs there is a good documentation and code examples

    Disadvantage

    • When upgrading to a newer version, code usually needs to be adapted.
    • When upgrading to a newer version, the result may look different.

    In my projects I use a headless chrome browser. For this I found an easy to use api on Github, which uses the DevTools of Chrome.

    It also includes a simple example how to print a page into a PDF.

    For my purposes I have customized this example and write the HTML into a temporary file and then navigate to that file.

    // Navigate to HTML-File
    page.navigate(htmlTempFile.getAbsolutePath());
    

    I can't say if this is the best way, but for me this was the easiest and most understandable way