c# - 从 c# 调用时,wkhtmltopdf 未将 html 字符串转换为 pdf
问题描述
我正在尝试将html
字符串转换为在我的控制台应用程序中pdf
使用。这是命令。wkhtmltopdf
.net 5
echo | set /p="<h3>test</h3>" | "C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe" -s A4 - "C:\Users\xxxx\Desktop\test.pdf"
当我在命令提示符下运行并获得 pdf 文件时,上述命令有效。但是当我在控制台应用程序中以编程方式运行相同的命令时,这没有任何作用。
这是我尝试过的代码,
string arguments = $@"echo | set /p=""<h3>test</h3>"" | ""C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe"" -s A4 - ""C:\Users\xxxx\Desktop\test.pdf""";
var p = new System.Diagnostics.Process()
{
StartInfo =
{
FileName = "cmd.exe",
Arguments = arguments,
UseShellExecute = false, // needs to be false in order to redirect output
RedirectStandardOutput = true,
RedirectStandardError = true,
RedirectStandardInput = true, // redirect all 3, as it should be all 3 or none
WorkingDirectory = Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)),
//WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden
}
};
p.Start();
// read the output here...
var output = p.StandardOutput.ReadToEnd();
var errorOutput = p.StandardError.ReadToEnd();
// ...then wait n milliseconds for exit (as after exit, it can't read the output)
p.WaitForExit(60000);
// read the exit code, close process
int returnCode = p.ExitCode;
p.Close();
// if 0 or 2, it worked so return path of pdf
if ((returnCode == 0) || (returnCode == 2))
return outputFolder + outputFilename;
else
throw new Exception(errorOutput);
请协助我缺少什么。
解决方案
我弄清楚了问题所在。
/C
当我们以编程方式运行命令时,看起来我们需要添加到命令的开头。
string arguments = $@"/C echo | set /p=""<h3>test</h3>"" | ""C:\Program Files\wkhtmltopdf\bin\wkhtmltopdf.exe"" -s A4 - ""C:\Users\xxxx\Desktop\test.pdf""";
原因:
/C
执行字符串指定的命令,然后终止。
推荐阅读
- ios - UIScrollView 中的 UIView 中的 UITableView。滚动表格视图和滚动滚动视图时
- python-3.x - 为什么我的 Odoo 13 实例仅使用调试资产运行?
- python - 如何从 REST API 导入数据和包
- java - JAVA - 如何按对象元素对 ArrayList 进行排序?
- python - 如何调整多边形的大小以适合画布?Python Tkinter
- python - PYTHON Tensorflow,文本分析:文件中的非 ASCII 字符“\xc3”
- php - 使用php从网站链接中提取数字
- html - 在居中的图像/视频下直接对齐文本
- google-data-studio - 数据工作室 - 比较不同列的数据
- django - django mongoDB boolean field get query has DATABASE ERROR