嗨,我正在尝试将文档或docx转换为c#MVC应用程序中的pdf . 我知道我可以使用libreOffice来做到这一点 . 所以我创建了一个简单的批处理文件来获取2个变量,然后将它们运行到libreoffice的'soffice'无头转换为pdf .
所以这给了我这个代码 .
echo on
SET var1=%2
IF "%var1:~-1%"=="\" SET var1=%var1:~0,-1%
cd "C:\Program Files\LibreOffice 5\program\"
soffice --headless --convert-to pdf %1 --outdir %var1%
最初我认为问题出在我的MVC应用程序中,以及我调用此批处理脚本的方式 . 但我评论(REM)soffice并使用标准输出在bash中输出命令 .
var psi = new ProcessStartInfo("cmd.exe", "/k " + command);
//psi.CreateNoWindow = true;
psi.FileName = command;
psi.UseShellExecute = false;
psi.RedirectStandardError = true;
psi.RedirectStandardOutput = true;
psi.Arguments = string.Format("{0} {1}", fullPath2, tempPath);
var process = Process.Start(psi);
string output = process.StandardOutput.ReadToEnd();
string error = process.StandardError.ReadToEnd();
Trace.WriteLine(output);
Trace.WriteLine(error);
process.WaitForExit();
当我评论soffice线时 - 它击中了WaitForExit并且没有遇到任何问题(没有pdf转换,但脚本已退出) .
如果我不这样做,它似乎执行cmd甚至soffice命令,因为我可以在任务管理器中看到它们 - 但显然没有任何反应 .
另外,上面的代码在我执行c#命令行程序时起作用(我在两个实例中都硬编码了文件/命令行) . 当我作为在我的MVC应用程序中运行应用程序池的用户运行时,可执行文件也可以工作 .
无论我或我的appPool用户运行它,bash文件也可以运行文件'standalone' .
那是什么给了 - 为什么不运行 .
这是来自该跟踪的代码 - 所以bash脚本的作用 .
c:\windows\system32\inetsrv>echo on
c:\windows\system32\inetsrv>SET var1=C:\inetpub\xxxxxxxxx\Temp\
c:\windows\system32\inetsrv>IF "\" == "\" SET var1=C:\inetpub\xxxxxxxxx\Temp
c:\windows\system32\inetsrv>cd "C:\Program Files\LibreOffice 5\program\"
C:\Program Files\LibreOffice 5\program>soffice --headless --convert-to pdf C:\inetpub\xxxxxxxxx\Temp\636295920370843147.doc --outdir C:\inetpub\xxxxxxxxx\Temp
我有一种感觉,这与字符或其他东西有关,因为soffice会起火(可以在任务管理器中看到它) .
仅供参考,任何地方都没有空格或特殊字符 .
有任何想法吗?
更新这看起来是wait命令的问题 . 所以任何有用的帮助,我开始认为这可能是c#和libreoffice 5的问题 - 我已经看到了可以与libreoffice 4一起工作的例子 . 我猜我的挑战仍在继续......