How to Generate PDF in node.js

ぐ巨炮叔叔 提交于 2020-05-25 05:00:06

问题


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/


回答1:


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();
            });
        });
    });
});



回答2:


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



回答3:


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



来源:https://stackoverflow.com/questions/17105873/how-to-generate-pdf-in-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!