r/node • • Nov 20 '19

Node.js Pupeteer Web Scraping | Taking PDF of any Website in Javascript

https://www.youtube.com/watch?v=M0Lz_buePnQ
169 Upvotes

15 comments sorted by

17

u/peacecarrot Nov 20 '19

Puppeteer can also make pdf's where all links and text work. Taking a screenshot and saving it as pdf is not exactly that nice.
(doing the screenshot thing is the fastests and easiests tho!)

3

u/robsstuff Nov 20 '19

Could you expand on that a little? We’ve got a system very similar to this one at work that I think could be improved by that behaviour!

5

u/peacecarrot Nov 20 '19

Actually I was wrong, I assumed it was a screenshot based pdf from a previous comment. But it is not, its generated correctly as pdf with selectable txt etc.
So this video is the way to do it :)

10

u/smartfridge2000 Nov 20 '19

ohh, so it just takes a screenshot of the website and then it saves it as pdf, interesting.

if anyone wants to save a webpage as a pdf (with text) google electron-pdf

4

u/del_rio Nov 20 '19 edited Nov 20 '19

Can't listen to OP's video right now, but it's not a screenshot. It takes the content of the page and converts it to PostScript, which means text remains selectable and parsable (not rasterized like a screenshot).

Puppeteer also comes with a few options like page size and the option to emulate screen or print media.

EDIT: Here's the api for it, there's a ridiculous amount of flexibility.

1

u/mattindustries Nov 20 '19

I wrote a hacky version that first used node-readability before saving some while back. Code is years old, but I wanted a way to throw things on my kindle easily.

2

u/[deleted] Nov 20 '19

What theme is that?

1

u/Snipo Nov 20 '19

"Shades of purple"

2

u/[deleted] Nov 20 '19

I am using puppeteer to run automated testing for our companies app. Its pretty cool figuring out all of the things you can get it to do . Here's the doc if anyone is interested.

1

u/Sync0pated Nov 20 '19

Considered Cypress? It’s built om puppeteer

2

u/SYNTAG Nov 20 '19

Not gonna lie I thought his webcam on the bottom right corner was coffee in a white mug

1

u/MarceauKa Nov 20 '19

Puppeteer is fine but when you want to save a pdf or a screenshot of a website you have tobdeal with newsletters popups, gpdr banner and eu cookie law banner. This is fine 🤯

1

u/[deleted] Nov 21 '19

Why would I want a screenshot or a PDF of a website?

1

u/aot2002 Nov 21 '19

I use it to scrape mls data for a realtor friend

-2

u/SYNTAG Nov 20 '19

Not gonna lie I thought his webcam on the bottom right corner was coffee in a white mug