【问题标题】:Is there an npm package for converting RTF strings to PDF file in node.js?node.js 中是否有用于将 RTF 字符串转换为 PDF 文件的 npm 包?
【发布时间】:2020-11-11 17:31:18
【问题描述】:

我正在寻找适用于 Node.js 的 RTF 到 PDF 转换器。我在 npm 站点上没有找到太多的包。

应用程序如下:我需要从 AWS S3 存储桶中提取一个 RTF 文档(在 Word 中创建并保存为 .rtf)。然后我需要将文件作为 RTF 字符串读取。接下来,我需要用值替换 RTF 字符串中的某些占位符标记(例如,在“{host-name} 想邀请您在 {date} 到 {address}。”--我需要替换 {host-name }、{address} 和 {date} 的实际值)。然后,插入实际值后,我需要将字符串转换为可以附加到电子邮件的 PDF。

出于这个原因,我不能使用任何几个 Word-to-PDF 包中的任何一个(因为我需要干预用值插入占位符的中间步骤),所以我正在寻找一个 RTF-to -PDF 已转换(RTF 以字符串开头)。

有这种事吗?

谢谢。

【问题讨论】:

  • 中间步骤可以用 HTML/CSS 代替 RTF 吗?我认为网页转pdf可能更容易,您仍然可以替换值。

标签: node.js pdf rtf npm-package


【解决方案1】:

headless mode 中的 LibreOffice 应提供文件到 PDF 的转换。 您可以将其与LibreOffice-Convert 集成到节点中。

但是,您的计算机中需要 LibreOffice,因为它没有随 npm 一起安装。根据您的系统,查看here 的位置。

这是它在命令行中的使用方式,因此它支持 RTF 到 PDF 的转换:

soffice --headless --convert-to pdf file_name.rtf (source)

理论上应该是这样实现的(改编自npmjs):

const libre = require('libreoffice-convert');
const path = require('path');
const fs = require('fs');
const enterPath = path.join(__dirname, '/resources/example.rtf');
const outputPath = path.join(__dirname, '/resources/example.pdf');

// Read file
const file = fs.readFileSync(enterPath);
// Convert it to pdf format with undefined filter (see Libreoffice doc about filter)
libre.convert(file, '.pdf', undefined, (err, done) => {
    if (err) {
      console.log(`Error converting file: ${err}`);
    }
    
    // Here in done you have pdf file which you can save or transfer in another stream
    fs.writeFileSync(outputPath, done);
});

【讨论】:

  • 感谢 Ronen,但如果这需要安装 LibreOffice,它可能不适合我的目的。我们需要推送到 AWS Lambda 函数环境,除了节点包直接附带的内容之外,它不会进行安装。还有其他想法吗?
  • @gib65 除非您找到它的软件包,否则我建议您使用执行此操作的 SaaS 并使用他们的 API,或者通过运行安装了 LibreOffice 的 EC2 自己创建一个并私下公开它您的 VPC。你也可以使用 docker 容器来实现弹性。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-04-06
  • 2010-12-23
  • 2017-01-14
  • 1970-01-01
  • 2021-12-22
  • 1970-01-01
相关资源
最近更新 更多