chrome中pdf转换成html,GitHub - westy92/html-pdf-chrome: HTML to PDF converter via Chrome/Chromium...

html-pdf-chrome

68747470733a2f2f62616467652e667572792e696f2f6a732f68746d6c2d7064662d6368726f6d652e737667

68747470733a2f2f7472617669732d63692e6f72672f776573747939322f68746d6c2d7064662d6368726f6d652e7376673f6272616e63683d6d6173746572

68747470733a2f2f63692e6170707665796f722e636f6d2f6170692f70726f6a656374732f7374617475732f6769746875622f776573747939322f68746d6c2d7064662d6368726f6d653f6272616e63683d6d6173746572267376673d74727565

68747470733a2f2f6170692e636f6465636c696d6174652e636f6d2f76312f6261646765732f39333935646564363532393337663935386134312f6d61696e7461696e6162696c697479

68747470733a2f2f636f6465636f762e696f2f67682f776573747939322f68746d6c2d7064662d6368726f6d652f6272616e63682f6d61737465722f67726170682f62616467652e737667

68747470733a2f2f64617669642d646d2e6f72672f776573747939322f68746d6c2d7064662d6368726f6d652e737667

68747470733a2f2f736e796b2e696f2f746573742f6769746875622f776573747939322f68746d6c2d7064662d6368726f6d652f62616467652e737667

HTML to PDF converter via Chrome/Chromium.

Prerequisites

Latest Chrome/Chromium

Windows, macOS, or Linux

Installation

npm install --save html-pdf-chrome

Security

This library is NOT meant to accept untrusted user input. Doing so may have serious security risks such as Server-Side Request Forgery (SSRF).

CORS

If you run into CORS issues, try using the --disable-web-security Chrome flag, either when you start Chrome externally, or in options.chromeFlags. This option should only be used if you fully trust the code you are executing during a print job!

Usage

Note: It is strongly recommended that you keep Chrome running side-by-side with Node.js. There is significant overhead starting up Chrome for each PDF generation which can be easily avoided.

It's suggested to use pm2 to ensure Chrome continues to run. If it crashes, it will restart automatically.

As of this writing, headless Chrome uses about 65mb of RAM while idle.

# install pm2 globally

npm install -g pm2

# start Chrome and be sure to specify a port to use in the html-pdf-chrome options.

pm2 start google-chrome \

--interpreter none \

-- \

--headless \

--disable-gpu \

--disable-translate \

--disable-extensions \

--disable-background-networking \

--safebrowsing-disable-auto-update \

--disable-sync \

--metrics-recording-only \

--disable-default-apps \

--no-first-run \

--mute-audio \

--hide-scrollbars \

--remote-debugging-port=

# run your Node.js app.

TypeScript:

import * as htmlPdf from 'html-pdf-chrome';

const html = '

Hello, world!

';

const options: htmlPdf.CreateOptions = {

port: 9222, // port Chrome is listening on

};

// async

const pdf = await htmlPdf.create(html, options);

await pdf.toFile('test.pdf');

const base64 = pdf.toBase64();

const buffer = pdf.toBuffer();

const stream = pdf.toStream();

// Promise

htmlPdf.create(html, options).then((pdf) => pdf.toFile('test.pdf'));

htmlPdf.create(html, options).then((pdf) => pdf.toBase64());

htmlPdf.create(html, options).then((pdf) => pdf.toBuffer());

htmlPdf.create(html, options).then((pdf) => pdf.toStream());

JavaScript:

const htmlPdf = require('html-pdf-chrome');

const html = '

Hello, world!

';

const options = {

port: 9222, // port Chrome is listening on

};

htmlPdf.create(html, options).then((pdf) => pdf.toFile('test.pdf'));

htmlPdf.create(html, options).then((pdf) => pdf.toBase64());

htmlPdf.create(html, options).then((pdf) => pdf.toBuffer());

htmlPdf.create(html, options).then((pdf) => pdf.toStream());

View the full documentation in the source code.

Using an External Site

import * as htmlPdf from 'html-pdf-chrome';

const options: htmlPdf.CreateOptions = {

port: 9222, // port Chrome is listening on

};

const url = 'https://github.com/westy92/html-pdf-chrome';

const pdf = await htmlPdf.create(url, options);

Using Markdown

import * as htmlPdf from 'html-pdf-chrome';

import * as marked from 'marked';

const options: htmlPdf.CreateOptions = {

port: 9222, // port Chrome is listening on

};

const html = marked('# Hello [World](https://www.google.com/)!');

const pdf = await htmlPdf.create(html, options);

Using a Template Engine

Pug (formerly known as Jade)

import * as htmlPdf from 'html-pdf-chrome';

import * as pug from 'pug';

const template = pug.compile('p Hello, #{noun}!');

const templateData = {

noun: 'world',

};

const options: htmlPdf.CreateOptions = {

port: 9222, // port Chrome is listening on

};

const html = template(templateData);

const pdf = await htmlPdf.create(html, options);

HTTP Headers

Specify additional headers you wish to send with your request via CreateOptions.extraHTTPHeaders.

const options: HtmlPdf.CreateOptions = {

port: 9222, // port Chrome is listening on

extraHTTPHeaders: {

'Authorization': 'Bearer 123',

'X-Custom-Test-Header': 'This is great!',

},

};

const pdf = await HtmlPdf.create('https://httpbin.org/headers', options);

Custom Headers and Footers

Note: Requires Chrome 65 or later.

You can optionally provide an HTML template for a custom header and/or footer.

A few classes can be used to inject printing values:

date - formatted print date

title - document title

url - document location

pageNumber - current page number

totalPages - total pages in the document

You can tweak the margins with the printOptions of marginTop, marginBottom, marginLeft, and marginRight.

At this time, you must inline any images using base64 encoding.

You can view how Chrome lays out the templates here.

Example

const pdf = await htmlPdf.create(html, {

port,

printOptions: {

displayHeaderFooter: true,

headerTemplate: `

Page of

`,

footerTemplate: '

Custom footer!
',

},

});

Trigger Render Completion

There are a few CompletionTrigger types that wait for something to occur before triggering PDF printing.

Callback - waits for a callback to be called

Element - waits for an element to be injected into the DOM

Event - waits for an Event to fire

Timer - waits a specified amount of time

LifecycleEvent - waits for a Chrome page lifecycle event

Variable - waits for a variable to be set to true

Custom - extend htmlPdf.CompletionTrigger.CompletionTrigger

const options: htmlPdf.CreateOptions = {

port: 9222, // port Chrome is listening on

completionTrigger: new htmlPdf.CompletionTrigger.Timer(5000), // milliseconds

};

// Alternative completionTrigger options:

new htmlPdf.CompletionTrigger.Callback(

'cbName', // optional, name of the callback to define for the browser to call when finished rendering. Defaults to 'htmlPdfCb'.

5000 // optional, timeout (milliseconds)

),

new htmlPdf.CompletionTrigger.Element(

'div#myElement', // name of the DOM element to wait for

5000 // optional, timeout (milliseconds)

),

new htmlPdf.CompletionTrigger.Event(

'myEvent', // name of the event to listen for

'#myElement', // optional DOM element CSS selector to listen on, defaults to body

5000 // optional timeout (milliseconds)

),

new htmlPdf.CompletionTrigger.LifecycleEvent(

'networkIdle', // name of the Chrome lifecycle event to listen for. Defaults to 'firstMeaningfulPaint'.

5000 // optional timeout (milliseconds)

),

new htmlPdf.CompletionTrigger.Variable(

'myVarName', // optional, name of the variable to wait for. Defaults to 'htmlPdfDone'

5000 // optional, timeout (milliseconds)

),

License

html-pdf-chrome is released under the MIT License.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值