Search code examples
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.

  • How does this option differ from the other two headless browser tools(phantomjs, puppeteer)?
  • Which tool can I use it for pdf generation?

Solution

  • Here are what these tools do,

    • phantomJS is a scriptable Headless WebKit. Similar to chromium browser. It's deprecated and the project is archived.
    • chromium: Chromium is Google's open-source web browser project. It is a fully functional browser on its own and supplies the vast majority of code for the Google Chrome browser.
    • chromium-headless: Headless Chrome is shipping in Chrome 59. It's a way to run the Chrome browser in a headless environment. Essentially, running Chrome without chrome! It brings all modern web platform features provided by Chromium and the Blink rendering engine to the command line.
    • Puppeteer: Puppeteer is a Node library which provides a high-level API to control Chrome or Chromium over the DevTools Protocol. Puppeteer runs headless by default, but can be configured to run full (non-headless) Chrome or Chromium.

    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();
    })();