0
votes

I am trying to convert a link (e.g. 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.

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.

1

1 Answers

1
votes

You should point to the actual exe, not the installer. 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";