简体   繁体   中英

Converting .docx/.doc files to .pdf in .Net Core

I am trying to convert MS Word files ( .docx/ .doc) to *.pdf file but didn't find any .net core package to do so.

Then I thought to use Node.js package for the same and to run it inside my .Net Core app. I tried node-msoffice-pdf node package. I first created a sample Node app just to test if this package works but when I run it, I get following error:

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)

What's the best way to convert MS Word files to PDF in .Net Core?

This Node.js package doesn't work for the same reason that the .NET Core one doesn't: it's calling Microsoft.Office.Interop , and that only works on Windows machines with the Office interop stuff installed.

Basically, when .NET Core compiles it knows that Microsoft.Office.Interop might not be available when it's installed, so throws an error then. However JS isn't compiled and finds out at run-time that it's installed on a server/container where C:\\Windows\\assembly\\GAC_MSIL\\... just isn't there.

Wrapping a Node.js app in your .NET Core just moves this problem from compile-time to run-time.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM