node.jsphantomjspdf-generationpuppeteergoogle-chrome-headless

Which one of the headless browse do i choose?


We have used phantomjs on previous days for generating pdf with server-side prince tool. Nowadays we like to change the headless browser from phantomjs to another one.

I research about which one is quickly rendering both phantomjs and puppeteer. In my case Reporting app to generate the PDF, only I test it with puppeteer and phantomjs. phantomjs quickly process the HTML compare to puppeteer.

And then I learn google-chrome-headless options to generate the PDF. It looks like a Chrome browser GUI application built-in modules. I am working with nodejs application. I am used with the below command from the command line

chrome --headless --disable-gpu --print-to-pdf <src_url>

I notice the --disable-gpu options, the other tools don't have it.


Solution

  • Here are what these tools do,

    You can control an instance of chromium and chromium headless with puppeteer. Of course it will allow you to provide arguments and generate PDF using that API.

    You can control the same API using Chrome CLI and Puppeteer.

    Here is how you can use chromium headless to generate a pdf of a website.

    chrome --headless --disable-gpu --print-to-pdf https://www.chromestatus.com/
    

    Here is the same thing using puppeteer,

    const puppeteer = require('puppeteer');
    
    (async () => {
      const browser = await puppeteer.launch();
      const page = await browser.newPage();
      await page.goto('https://www.chromestatus.com/');
      await page.pdf({path: 'chromestatus.pdf', format: 'A4'});
      await browser.close();
    })();