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

puppeteer api

Puppeteer is a Node library developed by Google and provides a high-level API for developers.

Puppeteer是Google开发的Node库,并为开发人员提供了高级API。

With Node.js already up and running, we will install puppeteer via NPM (node package manager).

在Node.js已经启动并运行的情况下,我们将通过NPM (节点程序包管理器) 安装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

step 1 - Generate PDF file using Node.js and Puppeteer API

"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 API
const 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 directory
path: 'testpdf.pdf', 
//  specifies the format
format: 'A4', 
// print background property
printBackground: 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,它将创建一个文件名为test.pdf新A4页面

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

通过像常规Node.js文件一样在命令提示符下启动代码来运行代码。

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

按照我们的代码,转换完成后将打印完成。

Output Image 1 - Generate PDF file using Node.js and Puppeteer API

The Output pdf file is then stored in the default Node.js directory with name test.pdf.

然后,输出pdf文件存储在默认的Node.js目录中,名称为test.pdf

Output Image 2 - Generate PDF file using Node.js and Puppeteer API

Output PDF file...

输出PDF文件...

Output Image 3 - Generate PDF file using Node.js and Puppeteer API

翻译自: https://www.includehelp.com/node-js/generate-pdf-file-using-node-js-and-puppeteer-api.aspx

puppeteer api

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

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

相关文章

php中进制转换

我们知道,进制有二进制、八进制、十进制、十六进制,但在php中只能存取八进制、十进制、十六进制 在讲进制转换之前,我们先说一下进制单词的缩写: 二进制:bin八进制:oct十进制:dec十六进制&…

java canvas画圆圈_java – 在视图上绘制一个圆圈(android)

几点意见:在确定圆的中心点和半径时,您需要考虑分配给视图的宽度和高度.您应该考虑分配给视图的填充,这样就不会绘制该保留部分.你应该避免在onDraw方法中分配对象,因为这会被调用很多.为了允许在XML布局中指定视图,您需要提供带有Context和AttributeSet的构造函数.…

第七章:项目成本管理

项目成本管理包括对成本进行估算、预算和控制的各过程,从而确保项目在批准的预算内完工。其包括 估算成本:对完成项目活动所需资金进行近似估算的过程制定预算:汇总所有单个活动或工作包的估算成本,建立一个经批准的成本基准的过程…

python rgb 图像_在Python中查找RGB图像的互补图像

python rgb 图像Complementary image is a transformed image such that it consists of complementary colours of the ones, which is present in the original image. 互补图像是一种变换后的图像 ,它由原始图像中存在的互补色组成。 For finding the complemen…

php的字符串、双引号输出变量的问题、转义字符

字符串 php中字符串可以用单引号和双引号表示&#xff0c;但单引号效率比双引号高&#xff0c;因为单引号是真正的字符串&#xff0c;双引号要做运算&#xff0c;即将字符串中的变量替换成值&#xff0c;单引号不需要 看下面的例子 <?phpheader(content-type:text/html;…

jmeter从mysql取值_Jmeter获取数据库值并作为参数请求(转载)

转载自&#xff1a;https://www.cnblogs.com/mawenqiangios/p/11088672.html01Jmeter连接数据库1、添加JDBC Connection Configuration(右键测试计划-->配置元件-->JDBC Connection Configuration)2、配置数据库连接信息&#xff0c;其中DataBase URL&#xff1a;jdbc:my…

圣斗士星矢

一部漏洞百出&#xff0c;情节重复&#xff0c;对白肉麻啰唆&#xff0c;人物刻画单一的动漫绵延了近二十年80一代的情结&#xff0c;每一个人都曾用稚嫩的声音&#xff0c;以“庐山升龙霸”抑或是“凤翼天翔”怒吼&#xff0c;今天&#xff0c;讨论的问题是&#xff1a;他&…

用于将类型从double转换为int的C#程序

Given a double type of variable, we have to convert it into an integer in C#. 给定双重类型的变量&#xff0c;我们必须在C&#xff03;中将其转换为整数。 Syntax: 句法&#xff1a; int_variable (int)double_variable;Example: 例&#xff1a; Input:double a 123…

ASCII码

ASCII 码使用指定的7 位或8 位二进制数组合来表示128 或256 种可能的字符。标准ASCII 码也叫基础ASCII码&#xff0c;使用7 位二进制数&#xff08;剩下的1位二进制为0&#xff09;来表示所有的大写和小写字母&#xff0c;数字0 到9、标点符号&#xff0c;以及在美式英语中使用…

如果有一天生你养你的两个人都走了

如果有一天 生你养你的两个人都走了 这世间唯一与你有着最亲密血缘关系的人都不在了 所以 孩子们啊 人在世的时候 要对父母好点 别让父母总是为你们操心 父母不需要你挣多少钱 但他们很需要子女的陪伴 因为子女是父母最深的牵挂如果有一天 生你养你的两个人都走了 这世间…

MySQL 纯insert_MySQL使用INSERT插入多条记录

MySQL使用INSERT插入多条记录&#xff0c;应该如何操作呢&#xff1f;下面就为您详细介绍MySQL使用INSERT插入多条记录的实现方法&#xff0c;供您参考。看到这个标题也许大家会问&#xff0c;这有什么好说的&#xff0c;调用多次INSERT语句不就可以插入多条记录了吗&#xff0…

php中对ASCII码的处理ord() 、chr()

和ASCII有关的部分函数 ord(): 函数返回字符串的首个字符的 ASCII 值 <?phpheader(content-type:text/html;charsetutf-8);echo ord(A),<br>;echo ord(ABC),<br>; ?>chr():指定的 ASCII 值返回字符&#xff0c;ASCII 值可被指定为十进制值、八进制值或十…

c# sizeof_C#程序演示sizeof()运算符的示例

c# sizeofC&#xff03;sizeof()运算符 (C# sizeof() operator) It is used to obtain the size of a data type in bytes (primitive data type). 它用于获取以字节为单位的数据类型的大小(原始数据类型)。 Syntax: 句法&#xff1a; sizeof(type);sizeof(int); //returns 4…

高手要耐得住寂寞

应该有不少高手会抱怨&#xff1a;为什么CSDN首页推荐的都是一些没有技术含量的文章&#xff1f;而我自己写的含金量非常高的&#xff0c;却很少能获得推荐&#xff1f; 其实这是一个很有意思的问题。程序员这职业&#xff0c;和别的职业相比&#xff0c;所占的人口比率肯定不高…

PHP中字符串定界符

作用&#xff1a;为输出大量的文本提供简单方法 格式&#xff1a; <<<名字 //代码 名字;注意&#xff1a; 开始和结束的名字必须一样&#xff0c;名字的命名格式和变量一样&#xff0c;结束的名字必须顶格写 <?phpheader(content-type:text/html;charsetutf-8);…

c语言 函数的参数传递示例_nexttoward()函数以及C ++中的示例

c语言 函数的参数传递示例C nexttoward()函数 (C nexttoward() function) nexttoward() function is a library function of cmath header, it is used to get the next representable value after the given first number in the direction of given second number, it accep…

java实现递归层次遍历_Java实现二叉树的前序、中序、后序、层序遍历(递归方法)...

在数据结构中&#xff0c;二叉树是树中我们见得最多的&#xff0c;二叉查找树可以加速我们查找的效率&#xff0c;那么输出一个二叉树也变得尤为重要了。二叉树的遍历方法分为四种&#xff0c;分别为前序遍历、中序遍历、后序、层序遍历。下图即为一个二叉树。前序遍历&#xf…

Object之MemberwiseClone方法

示例: 代码 usingSystem;usingSystem.Collections.Generic;usingSystem.Configuration;usingSystem.Data;usingSystem.Linq;usingSystem.Web;usingSystem.Web.Security;usingSystem.Web.UI;usingSystem.Web.UI.HtmlControls;usingSystem.Web.UI.WebControls;usingSystem.Web.UI…

php数组的声明和类型

数组的声明 方法一&#xff1a;使用array()&#xff0c;自动分配索引&#xff0c;从0开始 <?phpheader(content-type:text/html;charsetutf-8);$personarray("DL_one",18,man);print_r($person); ?>方法二&#xff1a;手动分配索引 <?phpheader(conte…

np.isfinite_带有Python示例的math.isfinite()方法

np.isfinitePython math.isfinite()方法 (Python math.isfinite() method) math.isfinite() method is a library method of math module, it is used to check whether a given number is a finite number of not, it accepts a number (integer/float, finite, infinite or N…