HummusJS - Convert HTML page to PDF in JavaScript

1.3k Views Asked by At

How can I use hummusJS to convert HTML code to PDF?
So far I am able to convert JPG images to pdf, as well as merge multiple pdfs.

2

There are 2 best solutions below

0
On BEST ANSWER

puppeteer is a good solution:

Installation

npm i puppeteer
# or "yarn add puppeteer"

Example - create a PDF.

const puppeteer = require('puppeteer');

(async () => {
  const browser = await puppeteer.launch();
  const page = await browser.newPage();
  await page.goto('https://news.ycombinator.com', {waitUntil: 'networkidle2'});
  await page.pdf({path: 'hn.pdf', format: 'A4'});

  await browser.close();
})();
3
On

try to use html-to-pdf-converter

Using headless chrome via puppeteer and than modifying generated pdf via HummusJS to add headers and footers with page numbers

Install

npm install html-to-pdf-converter

For me node-html-pdf with phantomjs is the best.

Install

npm install -g html-pdf 

code example:

var fs = require('fs');
var pdf = require('html-pdf');
var html = fs.readFileSync('./test/businesscard.html', 'utf8');
var options = { format: 'Letter' };

pdf.create(html, options).toFile('./businesscard.pdf', function(err, res) {
  if (err) return console.log(err);
  console.log(res); // { filename: '/app/businesscard.pdf' }
});