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:

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:

    Disadvantage

    Library:
    Advantage:

    Disadvantage


    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