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文件的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: android 默认浏览器 视频播放 二
- 下一篇: Java类类的getDeclaringC
