How to Generate PDF in node.js
Install http://phantomjs.org/ and the install the phantom node module https://github.com/amir20/phantomjs-node
Here is an example of rendering a pdf
var phantom = require('phantom');
phantom.create().then(function(ph) {
ph.createPage().then(function(page) {
page.open("http://www.google.com").then(function(status) {
page.render('google.pdf').then(function() {
console.log('Page Rendered');
ph.exit();
});
});
});
});
You can use Puppeteer (Headless Google Chrome Node API) to generate PDFs:
Puppeteer is a Node library which provides a high-level API to control Chrome or Chromium over the DevTools Protocol. Puppeteer runs headless by default, but can be configured to run full (non-headless) Chrome or Chromium.
You can easily generate a PDF using page.pdf()
:
'use strict';
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://example.com/');
await page.pdf({
path: 'example.pdf',
});
await browser.close();
})();
Then you can execute the program straight from Node.js:
node puppeteer.js
Another approach, starting from scratch using PDFKit...
Generating PDF's with Express https://blog.koenvangilst.nl/generating-a-pdf-with-express/
PDFKit, home page https://pdfkit.org/
pdfkit, npm page https://www.npmjs.com/package/pdfkit