在.Net Core中将.docx / .doc文件转换为.pdf

时间:2017-07-30 04:11:34

标签: node.js pdf asp.net-core ms-word .net-core

我正在尝试将MS Word文件( .docx / .doc)转换为* .pdf文件,但没有找到任何.net核心包来执行此操作。

然后我想使用Node.js包并在我的.Net Core应用程序中运行它。我尝试了node-msoffice-pdf节点包。我首先创建了一个示例Node应用程序,以测试此包是否有效,但是当我运行它时,我收到以下错误:

D:\Projects\test\node_modules\edge\lib\edge.js:169
    return edge.initializeClrFunc(options);
                ^
Error: Unable to compile C# code.
----> Errors when compiling as a CLR library:
error CS0006: Metadata file 'C:\Windows\assembly\GAC_MSIL\Microsoft.Office.Interop.Word\15.0.0.0__71e9bce111e9429c\Microsoft.Office.Interop.Word.dll
' could not be found
error CS0006: Metadata file 'C:\Windows\assembly\GAC_MSIL\Microsoft.Office.Interop.Excel\15.0.0.0__71e9bce111e9429c\Microsoft.Office.Interop.Excel.d
ll' could not be found
error CS0006: Metadata file 'C:\Windows\assembly\GAC_MSIL\Microsoft.Office.Interop.PowerPoint\15.0.0.0__71e9bce111e9429c\Microsoft.Office.Interop.Po
werPoint.dll' could not be found
error CS0006: Metadata file 'C:\Windows\assembly\GAC_MSIL\Office\15.0.0.0__71e9bce111e9429c\Office.dll' could not be found
error CS0006: Metadata file 'C:\Windows\assembly\GAC_MSIL\Microsoft.Vbe.Interop\15.0.0.0__71e9bce111e9429c\Microsoft.Vbe.Interop.dll' could not be f
ound
----> Errors when compiling as a CLR async lambda expression:
error CS0006: Metadata file 'C:\Windows\assembly\GAC_MSIL\Microsoft.Office.Interop.Word\15.0.0.0__71e9bce111e9429c\Microsoft.Office.Interop.Word.dll
' could not be found
error CS0006: Metadata file 'C:\Windows\assembly\GAC_MSIL\Microsoft.Office.Interop.Excel\15.0.0.0__71e9bce111e9429c\Microsoft.Office.Interop.Excel.d
ll' could not be found
error CS0006: Metadata file 'C:\Windows\assembly\GAC_MSIL\Microsoft.Office.Interop.PowerPoint\15.0.0.0__71e9bce111e9429c\Microsoft.Office.Interop.Po
werPoint.dll' could not be found
error CS0006: Metadata file 'C:\Windows\assembly\GAC_MSIL\Office\15.0.0.0__71e9bce111e9429c\Office.dll' could not be found
error CS0006: Metadata file 'C:\Windows\assembly\GAC_MSIL\Microsoft.Vbe.Interop\15.0.0.0__71e9bce111e9429c\Microsoft.Vbe.Interop.dll' could not be f
ound
    at Error (native)
    at Object.exports.func (D:\Projects\test\node_modules\edge\lib\edge.js:169:17)
    at Object.<anonymous> (D:\Projects\test\node_modules\node-msoffice-pdf\lib.js:4:23)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (D:\Projects\test\server\api\todo\controller\todo-controller.js:3:14)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)
    at tryModuleLoad (module.js:446:12)
    at Function.Module._load (module.js:438:3)
    at Module.require (module.js:497:17)
    at require (internal/module.js:20:19)
    at Object.<anonymous> (D:\Projects\test\server\api\todo\route\todo-route.js:3:25)
    at Module._compile (module.js:570:32)
    at Object.Module._extensions..js (module.js:579:10)
    at Module.load (module.js:487:32)

在.Net Core中将MS Word文件转换为PDF的最佳方法是什么?

1 个答案:

答案 0 :(得分:0)

这个Node.js软件包不起作用的原因与.NET Core没有这样做:它调用Microsoft.Office.Interop,并且仅适用于带有Microsoft.Office.Interop的Windows机器安装Office互操作的东西。

基本上,当.NET Core编译时​​,它知道C:\Windows\assembly\GAC_MSIL\...在安装时可能不可用,因此会抛出错误。但是JS没有编译,并且在运行时发现它安装在[23.22, 50.44 .... 32.53] 就在那里的服务器/容器上。

在.NET Core中包装Node.js应用程序只会将此问题从编译时移到运行时。