使用 JavaScript 将 docx/odt 转换为 PDF [英] Converting docx/odt to PDF using JavaScript

查看:50
本文介绍了使用 JavaScript 将 docx/odt 转换为 PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个需要将 docx 文件转换为 pdf 的节点网络应用程序(仅使用客户端资源,不使用插件).我找到了一个可能的解决方案,即使用 docxjs 将我的 docx 转换为 HTML,然后使用 jspdf (docx->HTML->PDF) 将 HTML 转换为 PDF.这个解决方案可以实现,但我遇到了几个问题,尤其是在渲染方面.我知道 docxjs 不会在 HTML 中保持与 docx 文件相同的渲染,所以这是一个问题...

I have a node web app that needs to convert a docx file into pdf (using client side resources only and no plugins). I've found a possible solution by converting my docx into HTML using docxjs and then HTML to PDF using jspdf (docx->HTML->PDF). This solution could make it but I encountered several issues especially with rendering. I know that docxjs doesn't keep the same rendering in HTML as the docx file so it is a problem...

所以我的问题是你知道任何可以直接完成工作而无需通过 HTML 的免费模块/解决方案(我也对 odt 作为源开放)?如果没有,你会建议我做什么?

So my question is do you know any free module/solution that could directly do the job without going through HTML (I'm open to odt as a source as well)? If not, what would you advise me to do?

谢谢

推荐答案

正如您所知,没有现成的和开放的库用于此.. 使用可用的变体无法获得好的结果.我的建议是:

As you already know there is no ready-to-use and open libs for this.. You just can't get good results with available variants. My suggesition is:

  1. 使用第三方 API.喜欢 https://market.mashape.com/convertapi/word2pdf-1#!documentation
  2. 为此目的创建您自己的服务.如果你有这样的能力,我建议在 node.js 上创建一个小型服务器(我打赌你知道怎么做).您可以将 Libreoffice 用作具有良好渲染质量的良好转换器,如下所示:

  1. Use third party API. Like https://market.mashape.com/convertapi/word2pdf-1#!documentation
  2. Create your own service for this purpose. If you have such ability, I suggest to create a small server on node.js (I bet you know how to do this). You can use Libreoffice as a good converter with good render quality like this:

libreoffice -headless -invisible -convert-to pdf {$file_name} -outdir/www-disk/

不要忘记这通常需要很多时间,不要阻塞请求-应答流:为每个转换操作使用单独的进程.

Don't forget that this is usually takes a lot of time, do not block the request-answer flow: use separate process for each convert operation.

最后一件事.Libreoffice 不是很轻,但质量很好.您还可以找到著名的 unoconv 工具.

And the last thing. Libreoffice is not very lightweight but it has good quality. You can also find notable unoconv tool.

截至 2019 年 1 月,有 docx-wasm,它有效在节点中并在安装节点的本地执行转换.专有但免费.

As of January 2019, there is docx-wasm, which works in node and performs the conversion locally where node is installed. Proprietary but freemium.

这篇关于使用 JavaScript 将 docx/odt 转换为 PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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