Created
December 27, 2019 09:27
-
-
Save codesorter2015/07bea7b896cdfd0780cb61678068b5cc to your computer and use it in GitHub Desktop.
Grab pdf content using puppeteer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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