使用 JavaScript 将 docx/odt 转换为 PDF

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23593886/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 01:10:57  来源:igfitidea点击:

Converting docx/odt to PDF using JavaScript

javascriptnode.jsms-wordconverterjspdf

提问by ncohen

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...

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

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?

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

Thanks

谢谢

回答by zarkone

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. 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.

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

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

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

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

    还有最后一件事。Libreoffice 不是很轻,但质量很好。您还可以找到值得注意的unoconv工具。

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

截至 2019 年 1 月,有docx-wasm,它在 node 中工作并在安装 node 的本地执行转换。专有但免费。

回答by Ryan

It appears that even after three years ncohen had not found an answer. It was also unclear if it had to be a free (as in dollars) solution.

看起来,即使三年后,ncohen 也没有找到答案。也不清楚它是否必须是免费的(如美元)解决方案。

The original requirements were:

最初的要求是:

using client side resources only and no plugins

Do you mean you don't want server side conversion? Right, I would like my app to be totally autonomous.

仅使用客户端资源,不使用插件

你的意思是你不想要服务器端转换?是的,我希望我的应用程序是完全自主的。

Since all the other answers/comments only offered server side component solutions, which the author clearly stated was not what they wanted, here is a proposed answer.

由于所有其他答案/评论仅提供服务器端组件解决方案,作者明确表示这不是他们想要的,这里是一个建议的答案。

The company I work for has had this solution for a few years now, that can convert DOCX (not odt yet) files to PDF completely in the browser, with no server side component required. This currently uses either asm.js/PNaCl/WASM depending on the exact browser being used.

我工作的公司已经有了这个解决方案几年了,它可以在浏览器中将 DOCX(尚未 odt)文件完全转换为 PDF,无需服务器端组件。这当前使用 asm.js/PNaCl/WASM 取决于所使用的确切浏览器。

https://www.pdftron.com/samples/web/samples/viewing/viewing/

https://www.pdftron.com/samples/web/samples/viewing/viewing/

Open an office file using the demo above, and you will see no server communication. Everything is done client side. This demo works on mobile browsers also.

使用上面的演示打开一个 office 文件,您将看到没有服务器通信。一切都在客户端完成。此演示也适用于移动浏览器。