admin管理员组文章数量:1415484
Can anyone who worked on something like this describe the general process? I'm very confused right now. By report I mean a visually appealing document with logo, tables, headers and footers, and the data will be retrieved dynamically.
The approaches I looked at are:
Use a server side library (node.js module) that generates the PDF. Send the string representation as response with
Content-Type: application/pdf
. Problem: I chose PDFKit, but it doesn't work and no content shows up at all. It uses PDF 1.3, which is old.Generate PDF on client side. Problem: Most popular library seems to be jsPDF, but it's not very capable of producing sophisticated-looking documents.
Write template in PDF source code and fill in the data on server side. Problem: The encoding is weird, for example if I just do
doc.text("1")
, a lot of unrecognizable characters appear for just the string "1". I'm very confused about this.
Finally, it'll be super helpful if anyone provides a link that can help me understand the encoding! It's super confusing to me.
Any experience with similar tasks is much appreciated!
Can anyone who worked on something like this describe the general process? I'm very confused right now. By report I mean a visually appealing document with logo, tables, headers and footers, and the data will be retrieved dynamically.
The approaches I looked at are:
Use a server side library (node.js module) that generates the PDF. Send the string representation as response with
Content-Type: application/pdf
. Problem: I chose PDFKit, but it doesn't work and no content shows up at all. It uses PDF 1.3, which is old.Generate PDF on client side. Problem: Most popular library seems to be jsPDF, but it's not very capable of producing sophisticated-looking documents.
Write template in PDF source code and fill in the data on server side. Problem: The encoding is weird, for example if I just do
doc.text("1")
, a lot of unrecognizable characters appear for just the string "1". I'm very confused about this.
Finally, it'll be super helpful if anyone provides a link that can help me understand the encoding! It's super confusing to me.
Any experience with similar tasks is much appreciated!
Share Improve this question asked Jun 26, 2013 at 21:32 Erica XuErica Xu 5551 gold badge4 silver badges14 bronze badges 3- See also JavaScript PDF generator library. – Dan Dascalescu Commented Feb 5, 2015 at 3:14
- @DanDascalescu link's post has been removed – jawath Commented Dec 25, 2017 at 3:08
- @jawath: that question was deleted by SO mods as "off topic", because just closing it wasn't enough. Please direct complaints to the moderators such as mureinik. – Dan Dascalescu Commented Dec 30, 2017 at 2:17
3 Answers
Reset to default 18I haven't personally done this, but the first thing I would try would be:
- On the server side dynamically build the appropriate HTML document and CSS
- Use phantomJS to render that document
- Tell phantomJS to convert that document to PDF, saved in a temp file
- Send the PDF back as the HTTP response by writing the temp PDF file to the response body
- Delete the temp file
If you are struggling with content-type, content-disposition, etc, you shouldn't have to worry about that if you have a valid pdf file on disk and just write that data to the HTTP response. With the right headers, you should be able to ask the browser to either display the PDF or treat it as a file to be saved as a download.
As the member of jsreport team, I would give it a shot.
jsreport platform provides multiple ways how to generate pdf reports. The most common included one is to transform html into pdf using headless chrome. jsreport will also compile and render handlebars or jsrender html templates if its specified, it can embed images, add header/footer, run custom javascripts and more.
You can play with the examples and see the options you have https://playground.jsreport.net
When you are done with playing, you can use jsreport online or download and install jsreport server to your company. Then you are ready to call its REST API and generate reports.
More to your question
- jsreport will provide correct content-type in the response for pdf or html. You can just let the browser to display the result
- data can be sent to jsreport api or retreived by custom script
I'm not sure what the most common approach is, but personally I like to create an HTML template, populate it in my server-side code, and then use wkhtmltopdf to convert the HTML into a PDF. If you're using .NET you should check out WkHtmlToXSharp (which is a .NET wrapper for wkhtmltopdf.)
本文标签: Most used approach to generate a PDF report (JavaScriptnodejs)Stack Overflow
版权声明:本文标题:Most used approach to generate a PDF report (JavaScript, node.js)? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1737616586a1998986.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论