A brief introduction to Wget, Curl, and Headless Chrome.

Wget and Curl and small programs easily installed through your package manager, or directly by downloading and installing. Headless Chrome can be installed from the command line using node and node's package manager npm. Install node globally and add add it to your path (or use your package manager to install it). Ensure npm is installed. Headless Chrome can be used globally or in a local working directory via a package.json file. Once node and npm are installed, you need only install headless chrome with npm like so `npm install chrome`.

To print an html file as a pdf using headless chrome, you can use the javascript below. That code will print the html file, your-pdf.html as test.pdf in the working directory, with specified margins and a4 format. Additional arguments can be used to style headers and footers. Put the javascript in a js file and run it from the command line using node via a package.json.


const puppeteer = require('puppeteer');
(async() => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('file://c:/Node/projects/public_html/pdf-print/your-pdf.html');
await page.pdf({
  path: 'test.pdf',
  format: 'A4',
  margin: {
        top: "20px",
        left: "20px",
        right: "20px",
        bottom: "20px"
  }
});
await browser.close();
})();


That’s all for now…

comments powered by Disqus