node.js - HTML and CSS to PDF in JavaScript - Stack Overflow

I know there are a bunch of libraries out there but none seem to exactly match my scenario. So I'm

I know there are a bunch of libraries out there but none seem to exactly match my scenario. So I'm hoping to get some advice here...

technologies: I'm using Node.js on Express.js for the backend, and html/css/js for the front-end. Browser support is IE8 and up, chrome, FF, and other modern browsers.

What I need to do is: have a "to pdf" button for the user to click, which would then convert a chunk of the DOM to pdf. This chunk of DOM's html is generated dynamically when the back-end makes API calls to another app. The CSS is static.

Other than these, I have a lot of freedom to do whatever I want, as long as I don't have to send clear text data to third parties and etc.

Any remended ways to do it?


Update: looking into wkhtmltopdf

I know there are a bunch of libraries out there but none seem to exactly match my scenario. So I'm hoping to get some advice here...

technologies: I'm using Node.js on Express.js for the backend, and html/css/js for the front-end. Browser support is IE8 and up, chrome, FF, and other modern browsers.

What I need to do is: have a "to pdf" button for the user to click, which would then convert a chunk of the DOM to pdf. This chunk of DOM's html is generated dynamically when the back-end makes API calls to another app. The CSS is static.

Other than these, I have a lot of freedom to do whatever I want, as long as I don't have to send clear text data to third parties and etc.

Any remended ways to do it?


Update: looking into wkhtmltopdf

Share Improve this question edited Jul 27, 2012 at 23:32 Max asked Jul 27, 2012 at 19:25 MaxMax 1,45917 silver badges29 bronze badges 2
  • Which libraries have you found and why don't they match your case? – Bergi Commented Jul 27, 2012 at 19:36
  • well, for instance, jspdf looks great, but it doesn't take in CSS – Max Commented Jul 27, 2012 at 19:38
Add a ment  | 

2 Answers 2

Reset to default 5

Basically, this is what I ended up doing

console.log("before");
fs.writeFile(html_filename, html, function (err) {
  if (err) {res.writeHead(400); res.end("" + err); return;}

  console.log("wrote html fine; now converting");
  exec('wkhtmltopdf ' + html_filename + ' ' + pdf_filename, function (err, stdout, stderr) {
    if (err) {res.writeHead(400); res.end("" + err); return;}

    console.log("converted; now reading");
    fs.readFile(pdf_filename, function (err, data) {
      if (err) {res.writeHead(400); res.end("" + err); return;}

      console.log("read fine; now serving");
      res.writeHead(200, {"content-type" : "application/pdf"});
      res.end(data);
    });
  });
});

IMO it's a bit ugly as it requires making a file, then converting it, and then serving it, and finally delete the two. I suspect scalability problems here. Also wkhtmltopdf seems to not play nice with osx. There's no such problem on linux though.

Try wkhtmltopdf . You can easily look at the current implementation of the driver for Ruby on Rails and apply it to node and express. You basically call a mand line app and it will be converted to a PDF using your print CSS.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745642611a4637782.html

相关推荐

  • node.js - HTML and CSS to PDF in JavaScript - Stack Overflow

    I know there are a bunch of libraries out there but none seem to exactly match my scenario. So I'm

    4小时前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信