puppeteer api_使用Node.js和Puppeteer API生成PDF文件
puppeteer api
Puppeteer is a Node library developed by Google and provides a high-level API for developers.
Puppeteer是Google開發(fā)的Node庫,并為開發(fā)人員提供了高級API。
With Node.js already up and running, we will install puppeteer via NPM (node package manager).
在Node.js已經(jīng)啟動并運行的情況下,我們將通過NPM (節(jié)點程序包管理器) 安裝puppeteer 。
Note: You should have Node.js installed in your PC.
注意:您應該在PC中安裝Node.js。
To get started, let's install puppeteer:
首先,讓我們安裝puppeteer:
Open the command prompt and type npm i puppeteer or npm install puppeteer
打開命令提示符,然后鍵入npm i puppeteer或npm install puppeteer
"npm" will download and install the puppeteer library together with other dependencies and Chromium.
“ npm”將下載并安裝puppeteer庫以及其他依賴項和Chromium。
Open a text editor and type the following code and save it with the file name as app.js
打開文本編輯器,然后輸入以下代碼,并將其保存為文件名app.js。
// Include puppeteer module const puppeteer = require ('puppeteer'); // file system node js module. const fs = require ('fs'); (async function () {try { // launch puppeteer APIconst browser = await puppeteer.launch();const page = await browser.newPage();// content of PDF file await page.setContent ('WELCOME TO GOOGLE PUPPETEER API');await page.emulateMedia ('screen');await page.pdf ({// name of your pdf file in directorypath: 'testpdf.pdf', // specifies the formatformat: 'A4', // print background propertyprintBackground: true});// console message when conversion is complete!console.log ('done');await browser.close();process.exit();} catch (e) {console.log ('our error', e);} } ) () ;The file should be saved in your Node.js directory.
該文件應保存在您的Node.js目錄中。
From the code above, we first of all include the puppeteer module and the file system module. The puppeteer API is then launched and it creates a new A4 page with file name test.pdf.
從上面的代碼,我們首先包括puppeteer模塊和file system模塊 。 然后啟動puppeteer API,它將創(chuàng)建一個文件名為test.pdf的新A4頁面 。
Run the code by initiating it in the command prompt like a regular Node.js file.
通過像常規(guī)Node.js文件一樣在命令提示符下啟動代碼來運行代碼。
Following our code, done will be printed out when the conversion is complete.
按照我們的代碼,轉換完成后將打印完成。
The Output pdf file is then stored in the default Node.js directory with name test.pdf.
然后,輸出pdf文件存儲在默認的Node.js目錄中,名稱為test.pdf 。
Output PDF file...
輸出PDF文件...
翻譯自: https://www.includehelp.com/node-js/generate-pdf-file-using-node-js-and-puppeteer-api.aspx
puppeteer api
總結
以上是生活随笔為你收集整理的puppeteer api_使用Node.js和Puppeteer API生成PDF文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python 整数 1字节_Python
- 下一篇: 用于将类型从double转换为int的C