简体   繁体   中英

Pdf generation using phantom.js in node.js

I am trying to generate pdf using phantom.js in node.js with express. My js is as follow:

var express = require('express');
var router = express.Router();
var phantom = require('phantom');

router.get('/pdfDemo', function(req, res, next) {

    var html = '<!DOCTYPE html><html><head><title>My Webpage</title></head>' +
        '<body><h1>My Webpage</h1><p>This is my webpage. I hope you like it' +
        '!</body></html>';

    phantom.create(function (error, ph) {
        ph.createPage(function (error, page) {
            page.set('content', html, function (error) {

                if (error) {
                    console.log('Error setting content: %s', error);
                } else {
                    page.render('page.pdf', function (error) {
                        if (error) console.log('Error rendering PDF: %s', error);
                    });
                }

                ph.exit();
            });
        });
    });
});

module.exports = router;

I am getting following error while executing this:

phantomjs-node: You don't have 'phantomjs' installed

    /paht_to_file/WebStrom WorkSpace/PdfDemo/routes/pdf.js:21
            ph.createPage(function (error, page) {
               ^
    TypeError: Object Error: spawn ENOENT has no method 'createPage'

I am using webstrom as development environment. I am new in Node.js and can't able to figure it out what's wrong here. Can anyone help?

This has nothing to do with node. You should install phantomjs on your computer. You can do so by doing brew install phantomjs or apt-get install phantomjs , depending on your OS.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM