簡體   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