最常用的生成PDF报告的方法(JavaScript,node.js)? [英] Most used approach to generate a PDF report (JavaScript, node.js)?

查看:64
本文介绍了最常用的生成PDF报告的方法(JavaScript,node.js)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从事此类工作的任何人都可以描述总体流程吗?我现在很困惑.报告是指带有徽标,表格,页眉和页脚的外观吸引人的文档,并且可以动态检索数据.

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.

我看过的方法是:

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

  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.

在客户端生成PDF.问题:最受欢迎的库似乎是jsPDF,但是它不能很好地生成外观精美的文档.

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

在PDF源代码中编写模板,并在服务器端填写数据.问题:编码很奇怪,例如,如果我只执行 doc.text("1"),则仅字符串"1"会出现很多无法识别的字符.我对此很困惑.

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!

推荐答案

我还没有亲自做过,但是我要尝试的第一件事是:

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

  1. 在服务器端动态构建适当的HTML文档和CSS
  2. 使用 phantomJS 呈现该文档
  3. 告诉phantomJS将文档转换为PDF,并保存在临时文件中
  4. 通过将临时PDF文件写入响应正文,将PDF发送回HTTP响应
  5. 删除临时文件

如果您在处理内容类型,内容处置等问题,则不必担心磁盘上是否有有效的pdf文件,只需将该数据写入HTTP响应即可.使用正确的标题,您应该能够要求浏览器显示PDF或将其视为要保存为下载文件的文件.

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报告的方法(JavaScript,node.js)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆