HTML to PDF with Node.js

前端 未结 12 1865
终归单人心
终归单人心 2020-11-27 09:20

I\'m looking to create a printable pdf version of my website webpages. Something like express.render() only render the page as pdf

相关标签:
12条回答
  • 2020-11-27 09:44

    Create PDF from External URL

    Here's an adaptation of the previous answers which utilizes html-pdf, but also combines it with requestify so it works with an external URL:

    Install your dependencies

    npm i -S html-pdf requestify
    

    Then, create the script:

    //MakePDF.js
    
    var pdf = require('html-pdf');
    var requestify = require('requestify');
    var externalURL= 'http://www.google.com';
    
    requestify.get(externalURL).then(function (response) {
       // Get the raw HTML response body
       var html = response.body; 
       var config = {format: 'A4'}; // or format: 'letter' - see https://github.com/marcbachmann/node-html-pdf#options
    
    // Create the PDF
       pdf.create(html, config).toFile('pathtooutput/generated.pdf', function (err, res) {
          if (err) return console.log(err);
          console.log(res); // { filename: '/pathtooutput/generated.pdf' }
       });
    });
    

    Then you just run from the command line:

    node MakePDF.js
    

    Watch your beautify pixel perfect PDF be created for you (for free!)

    0 讨论(0)
  • If you want to export HTML to PDF. You have many options. without node even

    Option 1: Have a button on your html page that calls window.print() function. use the browsers native html to pdf. use media queries to make your html page look good on a pdf. and you also have the print before and after events that you can use to make changes to your page before print.

    Option 2. htmltocanvas or rasterizeHTML. convert your html to canvas , then call toDataURL() on the canvas object to get the image . and use a JavaScript library like jsPDF to add that image to a PDF file. Disadvantage of this approach is that the pdf doesnt become editable. If you want data extracted from PDF, there is different ways for that.

    Option 3. @Jozzhard answer

    0 讨论(0)
  • 2020-11-27 09:50

    Phantom.js is an headless webkit server and it will load any web page and render it in memory, although you might not be able to see it, there is a Screen Capture feature, in which you can export the current view as PNG, PDF, JPEG and GIF. Have a look at this example from phantom.js documentation

    0 讨论(0)
  • 2020-11-27 09:52

    Use html-pdf

    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' } 
    });
    
    0 讨论(0)
  • 2020-11-27 09:54

    The best solution I found is html-pdf. It's simple and work with big html.

    https://www.npmjs.com/package/html-pdf

    Its as simple as that:

        pdf.create(htm, options).toFile('./pdfname.pdf', function(err, res) {
            if (err) {
              console.log(err);
            }
        });
    
    0 讨论(0)
  • 2020-11-27 09:54

    In case you arrive here looking for a way to make PDF from view templates in Express, a colleague and I made express-template-to-pdf

    which allows you to generate PDF from whatever templates you're using in Express - Pug, Nunjucks, whatever.

    It depends on html-pdf and is written to use in your routes just like you use res.render:

    const pdfRenderer = require('@ministryofjustice/express-template-to-pdf')
    
    app.set('views', path.join(__dirname, 'views'))
    app.set('view engine', 'pug')
    
    app.use(pdfRenderer())
    
    

    If you've used res.render then using it should look obvious:

    app.use('/pdf', (req, res) => {
        res.renderPDF('helloWorld', { message: 'Hello World!' });
    })
    

    You can pass options through to html-pdf to control the PDF document page size etc

    Merely building on the excellent work of others.

    0 讨论(0)
提交回复
热议问题