日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

puppeteer api_使用Node.js和Puppeteer API生成PDF文件

發(fā)布時間:2023/12/1 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 puppeteer api_使用Node.js和Puppeteer API生成PDF文件 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

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文件的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內容還不錯,歡迎將生活随笔推薦給好友。