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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

puppeteer api_使用Node.js和puppeteer API从URL创建PDF文件

發布時間:2025/3/11 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 puppeteer api_使用Node.js和puppeteer API从URL创建PDF文件 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

puppeteer api

We will continue using Node.js and puppeteer which is a node library. As we saw in our last article, Puppeteer is a Node library developed by Google and provides a high-level API for developers.

我們將繼續使用Node.js和puppeteer(這是一個節點庫)。 正如我們在上一篇文章中看到的,Puppeteer是Google開發的Node庫,并為開發人員提供了高級API。

Note: You should have Node.js installed in your PC and puppeteer installed via NPM (node package manager).

注意:您應該在PC中安裝Node.js,并通過NPM(節點程序包管理器)安裝操縱p。

If you don't yet have Node.js installed, visit the Node.js official website and download for your PC version.

如果尚未安裝Node.js,請訪問Node.js官方網站并下載PC版本。

After the download, you can quickly install the puppeteer module by opening a command prompt window and type: npm I puppeteer

下載之后,您可以通過打開命令提示符窗口并鍵入以下命令來快速安裝puppeteer模塊: npm I 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:

const puppeteer = require ('puppeteer'); // Include puppeteer module const fs = require ('fs'); // file system Node.js module. (async function () {try { const browser = await puppeteer.launch(); // launch puppeteer APIconst page = await browser.newPage(); //1. Create PDF from URLawait page.goto('file:///E:/HDD%2080%20GB%20DATA/CODING%20TUTORIALS%20AND%20CODES/go237.com/go237%20web/New%20design/index.html')await page.emulateMedia ('screen');await page.pdf ({path: 'testpdf.pdf', // name of your pdf file in directoryformat: 'A4', // specifies the formatprintBackground: true // print background property});console.log ('done'); // console message when conversion is complete!await browser.close();process.exit();} catch (e) {console.log ('our error', e);}} ) () ;

The file system module is responsible for handling the properties of the output file, such as name or directory.

文件系統模塊負責處理輸出文件的屬性,例如名稱或目錄。

The puppeteer API is then launched and it creates a new A4 page with file name testpdf.pdf

然后啟動puppeteer API,它會創建一個新的A4頁面 ,文件名為testpdf.pdf

Note: At the URL field, you can use any URL of your choice. In my case, I used the url of a website I'm developing on my local host.

注意:在“ URL”字段中,可以使用您選擇的任何URL。 就我而言,我使用的是我在本地主機上開發的網站的url。

If you're using any URL on the world wide web (www), make sure you have internet connection which will enable puppeteer visit the website online and convert the current page to pdf.

如果您使用的是Internet(www)上的任何URL,請確保您具有互聯網連接,這將使偽娘能夠在線訪問該網站并將當前頁面轉換為pdf。

Run the code by initiating the file at the command prompt like a regular Node.js file.

通過在命令提示符處啟動文件(如常規Node.js文件)來運行代碼。

Following our code, done will be printed out on the console when the conversion is complete.

遵循我們的代碼,轉換完成后, done將打印在控制臺上。

The Output pdf file is then stored in the default node modules directory following our code, with name test.pdf.

然后,輸出pdf文件按照我們的代碼存儲在默認的節點模塊目錄中,名稱為test.pdf 。

Output file:

輸出文件:

So far, we have seen the three ways we can use to create a pdf file using Node.js and Google puppeteer API.

到目前為止,我們已經看到了使用Node.js和Google puppeteer API創建pdf文件的三種方法。

Isn't it cool !!!

是不是很酷!

In our next series we'll look at how to perform another powerful task with this awesome API.

在我們的下一個系列中,我們將研究如何使用這個很棒的API執行另一個強大的任務。

Please feel free to drop your comments if you have any problem.

如有任何問題,請隨時發表評論。

翻譯自: https://www.includehelp.com/node-js/create-pdf-file-from-url-using-node-js-and-puppeteer-api.aspx

puppeteer api

創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的puppeteer api_使用Node.js和puppeteer API从URL创建PDF文件的全部內容,希望文章能夠幫你解決所遇到的問題。

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