Skip to content

Instantly share code, notes, and snippets.

Revisions

  1. Kevinlearynet revised this gist Aug 28, 2018. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions How to save a webpage as PDF file using Puppeteer.js
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@ https://addaptive-microservices.herokuapp.com/api/html-to-pdf
    const puppeteer = require('puppeteer');

    class Webpage {
    static async generatePDF(url) {
    static async generatePDF(html) {
    const browser = await puppeteer.launch({ headless: true }); // Puppeteer can only generate pdf in headless mode.
    const page = await browser.newPage();

    @@ -33,5 +33,5 @@ class Webpage {

    (async() => {
    const url = 'https://ia601405.us.archive.org/18/items/alicesadventures19033gut/19033-h/19033-h.htm';
    const buffer = await Webpage.generatePDF(url);
    const buffer = await Webpage.generatePDF(html);
    })();
  2. Kevinlearynet revised this gist Aug 28, 2018. 1 changed file with 1 addition and 0 deletions.
    1 change: 1 addition & 0 deletions How to save a webpage as PDF file using Puppeteer.js
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,5 @@
    // Save PDF from HTML Microservice
    https://addaptive-microservices.herokuapp.com/api/html-to-pdf

    const puppeteer = require('puppeteer');

  3. Kevinlearynet revised this gist Aug 28, 2018. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions How to save a webpage as PDF file using Puppeteer.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    // Save PDF from HTML Microservice

    const puppeteer = require('puppeteer');

    class Webpage {
  4. Kevinlearynet revised this gist Aug 28, 2018. 1 changed file with 5 additions and 2 deletions.
    7 changes: 5 additions & 2 deletions How to save a webpage as PDF file using Puppeteer.js
    Original file line number Diff line number Diff line change
    @@ -4,9 +4,11 @@ class Webpage {
    static async generatePDF(url) {
    const browser = await puppeteer.launch({ headless: true }); // Puppeteer can only generate pdf in headless mode.
    const page = await browser.newPage();
    await page.goto(url, { waitUntil: 'networkidle', networkIdleTimeout: 5000 }); // Adjust network idle as required.

    page.setContent('<p>Hello, world!</p>');

    const pdfConfig = {
    path: 'url.pdf', // Saves pdf to disk.
    path: 'url.pdf', // Saves pdf to disk.
    format: 'A4',
    printBackground: true,
    margin: { // Word's default A4 margins
    @@ -16,6 +18,7 @@ class Webpage {
    right: '2.54cm'
    }
    };

    await page.emulateMedia('screen');
    const pdf = await page.pdf(pdfConfig); // Return the pdf buffer. Useful for saving the file not to disk.

  5. @glenhallworthreadify glenhallworthreadify created this gist Oct 5, 2017.
    31 changes: 31 additions & 0 deletions How to save a webpage as PDF file using Puppeteer.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,31 @@
    const puppeteer = require('puppeteer');

    class Webpage {
    static async generatePDF(url) {
    const browser = await puppeteer.launch({ headless: true }); // Puppeteer can only generate pdf in headless mode.
    const page = await browser.newPage();
    await page.goto(url, { waitUntil: 'networkidle', networkIdleTimeout: 5000 }); // Adjust network idle as required.
    const pdfConfig = {
    path: 'url.pdf', // Saves pdf to disk.
    format: 'A4',
    printBackground: true,
    margin: { // Word's default A4 margins
    top: '2.54cm',
    bottom: '2.54cm',
    left: '2.54cm',
    right: '2.54cm'
    }
    };
    await page.emulateMedia('screen');
    const pdf = await page.pdf(pdfConfig); // Return the pdf buffer. Useful for saving the file not to disk.

    await browser.close();

    return pdf;
    }
    }

    (async() => {
    const url = 'https://ia601405.us.archive.org/18/items/alicesadventures19033gut/19033-h/19033-h.htm';
    const buffer = await Webpage.generatePDF(url);
    })();