2

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.

Vipul Sharma
  • 768
  • 3
  • 11
  • 25

2 Answers2

1

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();
})();
Selmi Karim
  • 2,135
  • 14
  • 23
0

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' }
});
Selmi Karim
  • 2,135
  • 14
  • 23
  • I already tried that. it works. But I wanted to know if this is possible using hummusJS. – Vipul Sharma May 15 '18 at 09:41
  • Using a page content context, you can write text. (if you can write text so easy to write html code into pdf ) look at this: https://github.com/galkahana/HummusJS/wiki/Show-text – Selmi Karim May 15 '18 at 09:52
  • By HTML code I mean HTML parsed code. Just like any website page (and not the actual code). – Vipul Sharma May 15 '18 at 10:31