Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Generate PDF in node.js

I want to generate a module which will generate PDF by taking input as my Invoice and that PDF file is send to clients mail id automatic. In 1st step i got some code and try to generate PDF. That code is working fin and i am able to generate the PDF. but i am not able to open the file.

for code i use this link:http://github.com/marak/pdf.js/

like image 829
HomeWork Avatar asked Jun 14 '13 09:06

HomeWork


3 Answers

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
like image 134
Grant Miller Avatar answered Nov 05 '22 23:11

Grant Miller


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

like image 31
PatrickReagan Avatar answered Nov 06 '22 00:11

PatrickReagan


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();
            });
        });
    });
});
like image 10
Hadi Rasouli Avatar answered Nov 05 '22 23:11

Hadi Rasouli