繁体   English   中英

当从 c# 调用时,wkhtmltopdf 未将 html 字符串转换为 pdf

[英]wkhtmltopdf not converting html string to pdf when invoked from c#

我正在尝试在我的.net 5控制台应用程序中使用wkhtmltopdfhtml字符串转换为pdf 这是命令。

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执行字符串指定的命令,然后终止。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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