All Projects â†’ westy92 â†’ Html Pdf Chrome

westy92 / Html Pdf Chrome

Licence: mit
HTML to PDF converter via Chrome/Chromium

Programming Languages

javascript
184084 projects - #8 most used programming language
typescript
32286 projects

Projects that are alternatives of or similar to Html Pdf Chrome

Pdf Bot
🤖 A Node queue API for generating PDFs using headless Chrome. Comes with a CLI, S3 storage and webhooks for notifying subscribers about generated PDFs
Stars: ✭ 2,551 (+305.56%)
Mutual labels:  pdf, pdf-generation, node-js, chromium, headless, headless-chrome, google-chrome
Phpchrometopdf
A slim PHP wrapper around google-chrome to convert url to pdf or to take screenshots , easy to use and clean OOP interface
Stars: ✭ 127 (-79.81%)
Mutual labels:  pdf, pdf-generation, chrome, chromium, headless, headless-chrome
Chrome Charset
An extension used to modify the page default encoding for Chromium 55+ based browsers.
Stars: ✭ 346 (-44.99%)
Mutual labels:  google, chrome, chromium, google-chrome
Url To Pdf Api
Web page PDF/PNG rendering done right. Self-hosted service for rendering receipts, invoices, or any content.
Stars: ✭ 6,544 (+940.38%)
Mutual labels:  pdf, chrome, headless, headless-chrome
Mocha Chrome
☕️ Run Mocha tests using headless Google Chrome
Stars: ✭ 66 (-89.51%)
Mutual labels:  google, chrome, headless, headless-chrome
Cuprite
Headless Chrome/Chromium driver for Capybara
Stars: ✭ 743 (+18.12%)
Mutual labels:  chrome, chromium, headless, headless-chrome
Ferrum
Headless Chrome Ruby API
Stars: ✭ 1,009 (+60.41%)
Mutual labels:  chrome, chromium, headless, headless-chrome
Puppetron
Puppeteer (Headless Chrome Node API)-based rendering solution.
Stars: ✭ 429 (-31.8%)
Mutual labels:  pdf, chrome, chromium, headless
Pychrome
A Python Package for the Google Chrome Dev Protocol [threading base]
Stars: ✭ 469 (-25.44%)
Mutual labels:  chrome, headless, headless-chrome
Pdfsave
Convert websites into readable PDFs
Stars: ✭ 46 (-92.69%)
Mutual labels:  pdf, pdf-generation, node-js
Doctron
Docker-powered html convert to pdf(html2pdf), html to image(html2image like jpeg,png),which using chrome(golang) kernel, add watermarks to pdf, convert pdf to images etc.
Stars: ✭ 141 (-77.58%)
Mutual labels:  pdf, pdf-generation, headless-chrome
Serverless Chrome
🌐 Run headless Chrome/Chromium on AWS Lambda
Stars: ✭ 2,625 (+317.33%)
Mutual labels:  chrome, chromium, headless-chrome
Chromium Ipc Sniffer
A tool to capture communication between Chromium processes on Windows
Stars: ✭ 197 (-68.68%)
Mutual labels:  chrome, chromium, google-chrome
Gotenberg
A Docker-powered stateless API for PDF files.
Stars: ✭ 3,272 (+420.19%)
Mutual labels:  pdf, google-chrome, chromium
Puppeteer Docs Zh Cn
Google Puppeteer 文档的中文版本 , 目标版本 1.9.0, 翻译中...
Stars: ✭ 61 (-90.3%)
Mutual labels:  google, chrome, chromium
Chromeless
🖥 Chrome automation made simple. Runs locally or headless on AWS Lambda.
Stars: ✭ 13,254 (+2007.15%)
Mutual labels:  chrome, headless, headless-chrome
Playwright Go
Playwright for Go a browser automation library to control Chromium, Firefox and WebKit with a single API.
Stars: ✭ 272 (-56.76%)
Mutual labels:  chromium, headless, headless-chrome
node-headless-chrome
⚠️ 🚧 Install precompiled versions of the Chromium/Chrome headless shell using npm or yarn
Stars: ✭ 20 (-96.82%)
Mutual labels:  headless, chromium, headless-chrome
Headless Chrome Crawler
Distributed crawler powered by Headless Chrome
Stars: ✭ 5,129 (+715.42%)
Mutual labels:  chrome, chromium, headless-chrome
Uget Chrome Wrapper
Moved to https://github.com/ugetdm/uget-integrator and https://github.com/ugetdm/uget-extension
Stars: ✭ 74 (-88.24%)
Mutual labels:  chrome, chromium, google-chrome

html-pdf-chrome

npm version Linux & Mac Build Status Windows Build Status Maintainability Code Coverage Dependency Status Known Vulnerabilities

HTML to PDF converter via Chrome/Chromium.

Prerequisites

  • Latest Chrome/Chromium (latest recommended, 61 or higher required but some features may not work)
  • Windows, macOS, or Linux
  • Node.js 6 or later (we only test on 10+, mileage may vary)

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).

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=<port goes here>
# run your Node.js app.

TypeScript:

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

const html = '<p>Hello, world!</p>';
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 = '<p>Hello, world!</p>';
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 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: `
      <div class="text center">
        Page <span class="pageNumber"></span> of <span class="totalPages"></span>
      </div>
    `,
    footerTemplate: '<div class="text center">Custom footer!</div>',
  },
});

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
  • 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.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.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].