最常用的生成 PDF 报告的方法(JavaScript、node.js)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17330835/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 14:54:29  来源:igfitidea点击:

Most used approach to generate a PDF report (JavaScript, node.js)?

javascriptnode.jspdf-generation

提问by Erica Xu

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:

我查看的方法是:

  1. 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.

  2. Generate PDF on client side. Problem: Most popular library seems to be jsPDF, but it's not very capable of producing sophisticated-looking documents.

  3. 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.

  1. 使用生成 PDF 的服务器端库(node.js 模块)。将字符串表示作为响应发送Content-Type: application/pdf。问题:我选择了 PDFKit,但它不起作用并且根本没有显示任何内容。它使用旧的 PDF 1.3。

  2. 在客户端生成 PDF。问题:最流行的库似乎是 jsPDF,但它不是很能生成看起来很复杂的文档。

  3. 在PDF源代码中编写模板并在服务器端填写数据。问题:编码很奇怪,例如,如果我只是这样做doc.text("1"),则会出现许多无法识别的字符仅用于字符串“1”。我对此感到非常困惑。

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!

非常感谢任何类似任务的经验!

回答by Peter Lyons

I haven't personally done this, but the first thing I would try would be:

我没有亲自这样做,但我会尝试的第一件事是:

  1. On the server side dynamically build the appropriate HTML document and CSS
  2. Use phantomJSto render that document
  3. Tell phantomJS to convert that document to PDF, saved in a temp file
  4. Send the PDF back as the HTTP response by writing the temp PDF file to the response body
  5. Delete the temp file
  1. 在服务器端动态构建相应的 HTML 文档和 CSS
  2. 使用phantomJS呈现该文档
  3. 告诉 phantomJS 将该文档转换为 PDF,保存在临时文件中
  4. 通过将临时 PDF 文件写入响应正文,将 PDF 作为 HTTP 响应发送回
  5. 删除临时文件

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.

如果您在处理内容类型、内容处理等问题时遇到问题,如果您在磁盘上有一个有效的 pdf 文件,并且只需将该数据写入 HTTP 响应,您就不必担心。使用正确的标题,您应该能够要求浏览器显示 PDF 或将其视为要保存为下载的文件。

回答by Jan Blaha

As the member of jsreport team, I would give it a shot.

作为 jsreport 团队的成员,我会试一试。

jsreportplatform provides multiple ways how to generate pdf reports. The most common included one is to transform html into pdf using phantomjs. 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.

jsreport平台提供了多种生成pdf 报告的方式。最常见的一种是使用 phantomjs 将 html 转换为 pdf。如果指定,jsreport 还将编译和呈现把手或 jsrender html 模板,它可以嵌入图像、添加页眉/页脚、运行自定义 javascripts 等。

You can play with the "Hello World Phantom Pdf" example and see the options you have https://playground.jsreport.net

您可以使用“Hello World Phantom Pdf”示例并查看您拥有的选项 https://playground.jsreport.net

When you are done with playing, you can use jsreport onlineor download and installjsreport server to your company. Then you are ready to call its REST APIand generate reports.

玩完后,你可以在线使用jsreport或者下载安装jsreport服务器到你的公司。然后您就可以调用其REST API并生成报告了。

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
  • jsreport 将在 pdf 或 html 的响应中提供正确的内容类型。你可以让浏览器显示结果
  • 数据可以发送到 jsreport api 或通过自定义脚本检索

回答by AlliterativeAlice

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 wkhtmltopdfto convert the HTML into a PDF. If you're using .NET you should check out WkHtmlToXSharp(which is a .NET wrapper for wkhtmltopdf.)

我不确定最常用的方法是什么,但我个人喜欢创建一个 HTML 模板,将它填充到我的服务器端代码中,然后使用wkhtmltopdf将 HTML 转换为 PDF。如果您使用 .NET,您应该查看WkHtmlToXSharp(这是wkhtmltopdf的 .NET 包装器。)