使用JavaScript将docx / odt转换为PDF

时间:2021-05-16 21:07:44

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

我有一个节点Web应用程序,需要将docx文件转换为pdf(仅使用客户端资源,没有插件)。通过使用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

1 个解决方案

#1


7  

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. 使用第三方API。例如https://market.mashape.com/convertapi/word2pdf-1#!documentation

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

    为此目的创建自己的服务。如果你有这样的能力,我建议在node.js上创建一个小服务器(我打赌你知道如何做到这一点)。您可以将Libreoffice用作具有良好渲染质量的良好转换器,如下所示:

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

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

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

#1


7  

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. 使用第三方API。例如https://market.mashape.com/convertapi/word2pdf-1#!documentation

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

    为此目的创建自己的服务。如果你有这样的能力,我建议在node.js上创建一个小服务器(我打赌你知道如何做到这一点)。您可以将Libreoffice用作具有良好渲染质量的良好转换器,如下所示:

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

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

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