将JSON响应转换为pdf - NodeJS

时间:2018-04-17 13:07:23

标签: html json node.js

将获取请求发送至:' 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中完成吗?

2 个答案:

答案 0 :(得分:0)

Conversion isn't the right word but generation is. According to the generalized response in json response you can write logic for generation of pdf from it in node-js server. PDFKit and PDFmake are two good libraries for this purpose. I've used pdfmake and is very good. See doc here: https://pdfmake.github.io/docs/

答案 1 :(得分:0)

使用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);
});

注意:上面的示例代码段可处理数组中的第一个对象