【问题标题】:Convert a JSON Response to pdf - NodeJS将 JSON 响应转换为 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 中完成吗?

【问题讨论】:

标签: html json 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/

    【讨论】:

      猜你喜欢
      • 2018-07-29
      • 2019-11-27
      • 2021-09-04
      • 2019-09-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-05
      相关资源
      最近更新 更多