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

Create PDF file from URL using Node.js and puppeteer API 1

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将打印在控制台上。

Create PDF file from URL using Node.js and puppeteer API 2

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

然后,输出pdf文件按照我们的代码存储在默认的节点模块目录中,名称为test.pdf

Create PDF file from URL using Node.js and puppeteer API 3

Output file:

输出文件:

Create PDF file from URL using Node.js and puppeteer API 4

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/542521.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

python线程同步锁_[python] 线程间同步之Lock RLock

为什么需要同步 同样举之前的例子,两个线程分别对同一个全局变量进行加减,得不到预期结果,代码如下: total 0 def add(): global total for i in range(1000000): total 1 def desc(): global total for i in range(1000000): t…

servlet的由来

2019独角兽企业重金招聘Python工程师标准>>> 动静态网页技术 首先说下访问网页的大概过程: 你在浏览器中输入网址,按下enter键,此时浏览器代你做了很多事,简要说为:将你输入的这个网址作为目的地参数&#…

php header 文件大小,php获取远程文件大小及信息的函数(head_php

php获取远程文件大小及信息的函数(header头信息获取)阿里西西Alixixi.com开发团队在做一个客户系统时,需要做远程下载的功能,并实时显示进度条效果。所以,需要预先读取远程文件的大小信息,然后做为实时下载进度条的参数。功能函数…

Java ObjectInputStream readUnsignedShort()方法(带示例)

ObjectInputStream类readUnsignedShort()方法 (ObjectInputStream Class readUnsignedShort() method) readUnsignedShort() method is available in java.io package. readUnsignedShort()方法在java.io包中可用。 readUnsignedShort() method is used to read 2 bytes (i.e. …

python中info的用法_Python pandas.DataFrame.info函数方法的使用

DataFrame.info(self, verboseNone, bufNone, max_colsNone, memory_usageNone, null_countsNone) [source] 打印DataFrame的简要摘要。 此方法显示有关DataFrame的信息,包括索引dtype和列dtype,非空值和内存使用情况。 参数:verbose &#x…

第四次作业 孙保平034 李路平029

用C编写一个学生成绩管理系统 1、可以实现以下功能&#xff1a; cout<<"〓〓〓〓〓〓〓〓〓★ ☆ 1.增加学生成绩 ☆ ★〓〓〓〓〓〓〓〓〓"<<endl; 2、用链表存储信息 * 程序头部的注释结束 3、约定的规范&#xff1a; 1界面设计简介&#xff0c;人性化…

php serialize error at offset,PHP Notice: unserialize(): Error at offset XX of XX bytes

之前同事在本地开发的时候&#xff0c;出现一个错误&#xff0c;如下图所示&#xff1a;字面意思就是反序列化错误&#xff0c;由此bug引申出来序列化和反序列化得应用&#xff0c;以及php array当key为string类型的数字值时&#xff0c;会发生什么情形。先来看序列化$str [1 …

Java ClassLoader setClassAssertionStatus()方法与示例

ClassLoader类setClassAssertionStatus()方法 (ClassLoader Class setClassAssertionStatus() method) setClassAssertionStatus() method is available in java.lang package. setClassAssertionStatus()方法在java.lang包中可用。 setClassAssertionStatus() method is used …

python怎么变各种颜色_python – 如何淡化颜色

有很多方法可以做到这一点.您如何选择这取决于您是否重视速度和简单性或感知均匀性.如果你需要它是真正统一的,你需要用颜色配置文件定义RGB颜色,你需要配置文件的原色,这样你就可以转换为XYZ,然后转换到LAB,你可以操作L通道. 大多数情况下,您不需要这样做,而是可以使用像Photo…

informatica中元数据管理

摘自&#xff1a; http://blog.itpub.net/28690368/viewspace-766528/ informaica是一个很强大的ETL工具&#xff0c;WORKFLOW MANAGER负责对ETL调度流程进行设计与管理和执行&#xff0c;informatica在资料库中提供以下表来存储调动流程的相关信息&#xff0c;以便WORKFLOW …

yii+php+当前目录,Yii应用的目录结构和入口脚本

以下是一个通过高级模版安装后典型的Yii应用的目录结构&#xff1a;~~~.├── backend├── common├── console├── environments├── frontend├── nbproject├── tests├── vendor├── composer.json├── composer.lock├── init├── init.bat├── …

8086 寻址方式_8086微处理器的不同寻址模式

8086 寻址方式Introduction: 介绍&#xff1a; Addressing mode tells us what is the type of the operand and the way they are accessed from the memory for execution of an instruction and how to fetch particular instruction from the memory. There are mainly 8 …

决策树的value是什么意思_从零开始的机器学习实用指南(六):决策树

类似SVM&#xff0c;决策树也是非常多功能的机器学习算法&#xff0c;可以分类&#xff0c;回归&#xff0c;甚至可以完成多输出的任务&#xff0c;能够拟合复杂的数据集&#xff08;比如第二章的房价预测例子&#xff0c;虽然是过拟合了。&#xff09;决策树也是很多集成学习的…

Hive中生成随机唯一标识ID的方法

2019独角兽企业重金招聘Python工程师标准>>> HIVE中处理的数据往往比较多&#xff0c;在处理数据的时候希望给处理得到的数据一个ID标识&#xff0c;这时候可以用到UUID。 UUID的算法的核心思想是结合机器的网卡、当地时间、一个随即数来生成UUID。从理论上讲&#…

php从网页获得数据,php根据URL获得网页内容

php 中根据url来获得网页内容非常的方便&#xff0c;可以通过系统内置函数file_get_contents(),传入url,即可返回网页的内容&#xff0c;比如获得百度首页的内容代码为&#xff1a;$html file_get_contents(http://www.baidu.com/);echo $html;就可以显示出百度首页的内容&…

2020知道python语言应用答案_2020知到Python语言应用答案章节期末答案

组合管理理论最早由哈维马科维兹于1962年系统的提出&#xff0c;他开创了对投资进行整体管理的先 公司型基金和契约型基金的区别&#xff0c;下列不包括&#xff08;&#xff09;。 A&#xff0e;资金的性质B&#xff0e;基金的营运依据C&#xff0e;基 我国&#xff08;&#…

如何在Bootstrap中使用Jumbotron和页面标头类?

Introduction 介绍 In the previous article, we have learned how Responsive column, Nesting Columns and offset Columns work and how to use them? I hope now, you all are comfortable with the grid system; what is it, how to use it and how we can use it for c…

python中的数字类型格式与运算,python数字数据类型

python数字数据类型1. 数字在我们很小的时候&#xff0c;父母便开始教我们数数&#xff0c;从1数到10&#xff0c;聪明的孩子可以数的更多。python支持3中数值类型整型(int)&#xff0c;通常称之为整型或整数&#xff0c;这个概念与我们小学时学过的整数是相同的&#xff0c;py…

getprocaddress得到为0_基于ZU+系列MPSoC芯片的USB3.0/2.0接口硬件设计

本文主要介绍Zynq UltraScale MPSoC系列芯片的USB3.0/2.0接口硬件设计。ZU系列MPSoC要实现USB3.0/2.0的全部功能&#xff0c;需要同时使用MIO和GTR。因为GTR接口中的USB接口只支持USB3.0&#xff0c;对USB2.0的支持需要通过MIO接口外接USB PHY实现。ZU系列MPSoC包括两个USB接口…

如何设置单词第一个字母大写_大写一行中每个单词的第一个和最后一个字母

如何设置单词第一个字母大写Problem statement: 问题陈述&#xff1a; Given an input line, capitalize first and last letter of each word in the given line. Its provided that the input line is in lowercase. 给定输入行&#xff0c; 将给定行中每个单词的第一个和最…