将 JSON 响应转换为 pdf - NodeJS

Posted

技术标签:

【中文标题】将 JSON 响应转换为 pdf - NodeJS【英文标题】:Convert a JSON Response to pdf - NodeJS 【发布时间】:2018-09-27 11:21:17 【问题描述】:

当获取请求发送到:'http://localhost:4000/features' 有一个带有 JSON 数据的响应,其中包含 html

我需要将字段名称和描述的内容保存为 PDF

示例:

["_id":"5ad4951d0ba1c37c65818bc7","name":"Find your work faster","description":"<p>With an improved <strong>quick search</strong>, searching through all your issues and projects will be nothing else but a breeze. Whether you know the full issue key, part of the issue name, or just have a distant memory of a project from a year ago, start typing the words, and we&rsquo;ll do the rest for you. The quick search instantly shows the most relevant results, and refreshes them whenever you change your search term.</p>\n\n<p><img alt=\"\" src=\"https://confluence.atlassian.com/jirasoftware/files/945521251/945528523/1/1518181922686/quicksearch.png\" style=\"height:400px; width:800px\" /></p>\n\n<p>If you&rsquo;ve already found what you were looking for, just treat quick search as a handy work diary. Click anywhere in the box to see the issues and projects you&rsquo;ve been working on recently, and have the most important work always at your fingertips.</p>\n\n<p><a href=\"https://confluence.atlassian.com/jirasoftwareserver/quick-searching-939938728.html\">Learn more</a></p>\n","__v":0,"_id":"5ad5ddddcd054b2b5b20143c","name":"Project sidebar","description":"<p>The project sidebar that we previewed in&nbsp;<a href=\"https://confluence.atlassian.com/jira/jira-6-4-release-notes-678561444.html\">JIRA 6.4</a>&nbsp;is here to stay. We built this new navigation experience to make it easier for you to find what you need in your projects. It&#39;s even better, if you are using JIRA Agile: your backlog, sprints, and reports are now just a click away. If you&#39;ve used the sidebar with JIRA Agile before, you&#39;ll notice that cross-project boards, which include multiple projects, now have a project sidebar as well &mdash;&nbsp;albeit a simpler version.</p>\n","__v":0]

这可以在 nodeJS 中完成吗?

【问题讨论】:

你检查过这个吗? ***.com/questions/14552112/html-to-pdf-with-node-js 我确认是的。但没有变得更聪明 【参考方案1】:

使用html-pdf从html生成PDF,它在phantom之上工作

var pdf = require('html-pdf');
pdf.create(file[0].description).toFile('./' + file[0].description + '.pdf', function (err, res) 
    console.log(res.filename);
);

注意:上面的示例代码 sn-p 用于处理数组中的第一个对象

【讨论】:

【参考方案2】:

转化不是正确的词,但世代才是正确的。根据 json 响应中的 generalized 响应,您可以在 node-js 服务器中编写用于生成 pdf 的逻辑。 PDFKit 和 PDFmake 是用于此目的的两个很好的库。 我用过pdfmake,非常好。 在此处查看文档:https://pdfmake.github.io/docs/

【讨论】:

以上是关于将 JSON 响应转换为 pdf - NodeJS的主要内容,如果未能解决你的问题,请参考以下文章

将表示 UCHAR 数组的 json 字符串响应转换为字节数组

如何将对象响应对象的猫鼬聚合数组转换为json对象响应

使用nodejs将html转换为pdf

Nodejs:将文档转换为 PDF

为 POST API 请求发送多个响应

如何在Nodejs中将json对象数组转换为String?