![]() Puppeteer creates its own browser user profile which it cleans up on every run. This article describes some differences for Linux users. See this article for a description of the differences between Chromium and Chrome. See Puppeteer.launch() for more information. You can also use Puppeteer with Firefox Nightly (experimental support). const puppeteer = require ( 'puppeteer' ) Ĭonst browser = await puppeteer. You create an instance of Browser, open pages, and then manipulate them with Puppeteer's API.Įxample: navigating to and saving a screenshot as example.png: Puppeteer will be familiar to people using other browser testing frameworks. All examples below use async/await which is only supported in Node v7.6.0 or greater. Starting from v3.0.0 Puppeteer starts to rely on Node 10.18.1+. In just a few lines of code we created a simple API that will perform such a task, and taking that code to production levels won’t be that hard.Prior to v1.18.1, Puppeteer required at least Node v6.4.0. ![]() Puppeteer is an excellent tool to convert the web into other formats such as PDFs and images, it has a lot of options that allow for customization and optimization and is super easy to use. You can try and optimize the size by playing with the options, but often you’ll have to take a second step to compress PDFsįor most scenarios, it may not be required, but if the size is a concern of yours, know that you should take additional steps to fix it. The PDFs generated by Puppeteer, and a lot depending on the website, may not be the most optimal regarding file size. Notice: the code provided is very simple and does not contemplate any error handling, so please beaware of that if you plan to use it in a production set up. Now start your server, and visit, for example: There you go, an API that takes a target URL and renders a PDF version of it. The next step is to write code to open up a browser and load a website:Īwait page. That installation with download among other things a Chromium browser into your node modules folder, so be patient, it may take a minute or two more than a regular package. Generating pre-rendered content for Single Page Applications (SPAs)īefore we do anything with Puppeteer, we need to install it, and it is as easy as installing any other package with NPM.Create a PDF document and/or an image of a web page.Puppeteer allows you to work with a browser in headless mode, which allows you to do things like: Due to the lack of a GUI, the interactions with a headless browser take place over a command line.Įven though Puppeteer is mainly a headless browser, you can configure and use it as non-headless Chrome or Chromium. In that sense, a headless browser is simply just another browser that understands how to render HTML web pages and process JavaScript. If you are unfamiliar with the term headless browsers, it’s simply a browser without a GUI. Is, “A Node library which provides a high-level API to control headless Chrome or Chromium over the DevTools Protocol”. What Is Puppeteer, and Why Is It Awesome? ) that deal with PDF generation, but those libraries are often hard to use and involve a ton of steps, and APIs we are not familiar with.įortunately, there is an easier way, which is to convert web pages into PDF format, and that can be done with the help of a tool called Puppeteer. When we are faced with such a task our intuition is to google “JavaScript create pdf” and we get awesome SDKs (like PDFKit A common request I have seen in my career, especially when building web applications, is to generate PDF documents that users of the app can download. Writing a Puppeteer PDF API with ExpressĪs web developers, we love working with HTML, CSS, and JavaScript, but when we are working on projects, sometimes we are asked to solve problems that involve other technologies.Using Puppeteer to convert a web page into a PDF file.In just a few lines of code we created a simple API that will perform such a task, and taking that code to production levels won’t be that hard. What is Puppeteer, and why is it awesome? Puppeteer is an excellent tool to convert the web into other formats such as PDFs and images, it has a lot of options that allow for customization and optimization and is super easy to use.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |