简体   繁体   English

无法使用wkhtmltopdf从HTML生成PDF

[英]Unable to generate PDF from HTML using wkhtmltopdf

I am trying to convert a link (eg www.google.com ) into PDF using wkhtmltopdf (see Calling wkhtmltopdf to generate PDF from HTML ) but in this I am getting the error that pdf is not generated but the code is working fine and returns 0 as respone. 我正在尝试使用wkhtmltopdf将链接(例如www.google.com )转换为PDF(请参阅调用wkhtmltopdf从HTML生成PDF ),但是在这种情况下,我得到的错误是未生成pdf,但是代码可以正常工作并返回0作为响应。

Code: 码:

public static bool HtmlToPdf(string Url, string outputFilename)
{
    // assemble destination PDF file name
    string filename = @"C:\Users\Documents\visual studio 2012\Projects\PenchKin\PenchKin\Setup\test.pdf";

    // get proj no for header
    //Project project = new Project(int.Parse(outputFilename));

    var p = new System.Diagnostics.Process();
    p.StartInfo.FileName = @"C:\Users\documents\visual studio 2012\Projects\PenchKin\PenchKin\Setup\wkhtmltopdf-0.9.9-installer.exe";

    string switches = "--print-media-type ";
    switches += "--margin-top 4mm --margin-bottom 4mm --margin-right 0mm --margin-left 0mm ";
    switches += "--page-size A4 ";
    switches += "--no-background ";
    switches += "--redirect-delay 100";

    p.StartInfo.Arguments = switches + " " + Url + " " + filename;
    p.StartInfo.UseShellExecute = false; // needs to be false in order to redirect output
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.RedirectStandardError = true;
    p.StartInfo.RedirectStandardInput = true; // redirect all 3, as it should be all 3 or none
    p.StartInfo.WorkingDirectory = p.StartInfo.FileName;

    p.Start();

    // read the output here...
    string output = p.StandardOutput.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 (not sure about other values, I want a better way to confirm this)
    return (returnCode == 0 || returnCode == 2);
}

Please help to me solve this issue. 请帮我解决这个问题。

You should point to the actual exe , not the installer. 您应该指向实际的exe ,而不是安装程序。 Replace 更换

p.StartInfo.FileName = @"C:\Users\documents\visual studio 2012\Projects\PenchKin\PenchKin\Setup\wkhtmltopdf-0.9.9-installer.exe";

by 通过

p.StartInfo.FileName = @"C:\path\to\binary\wkhtmltopdf.exe";

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

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