Skip to content

Instantly share code, notes, and snippets.

@codesorter2015
Created December 27, 2019 09:27
Show Gist options
  • Save codesorter2015/07bea7b896cdfd0780cb61678068b5cc to your computer and use it in GitHub Desktop.
Save codesorter2015/07bea7b896cdfd0780cb61678068b5cc to your computer and use it in GitHub Desktop.
Grab pdf content using puppeteer
const fse = require('fs-extra');
const puppeteer = require('puppeteer');
const getPdf = (page) => {
let url = await page.url();
return page.evaluate(url => {
return new Promise(async resolve => {
const reader = new FileReader();
const response = await window.fetch(url, {
credentials: 'same-origin',
method: 'POST'
});
const data = await response.blob();
reader.readAsBinaryString(data);
reader.onload = () => resolve(reader.result);
reader.onerror = () => reject('Error occurred while reading binary string');
});
}, url);
}
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('PDF URL', {waitUntil: 'networkidle2'});
const pdfString = await getPdf(page);
const pdfData = Buffer.from(pdfString, 'binary');
fse.writeFileSync('./sample.pdf', pdfData);
await browser.close();
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment