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 API
const page = await browser.newPage();
//1. Create PDF from URL
await 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 directory
format: 'A4', // specifies the format
printBackground: 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