Search code examples
c#wkhtmltopdf

How to use wkHtmltoPdf to generate PDf files from statcic html files in C#


Can anyone suggest how to use wkhtmltopdf in C# Console application to generate PDF files from static html files?

wkhtmltopdf - http://code.google.com/p/wkhtmltopdf/

I have tried downloading the ibwkhtmltox-0.11.0_rc1 Windows Static Library (i368) but cant include its dll into my c# console app.

Any code samples will be helpful


Solution

  • If you can I would suggest using the exe - I think it's simpler.

    For an example, check out the Derp class in another answer of mine regarding how to run the wkhtmltopdf exe from C#. Or try something like the untested code below (your true code will be more complicated, this is just a demo/POC). Just replace google.com with the HTML page you want - you can use local files as well.

    var pi = new ProcessStartInfo(@"c:\wkhtmltopdf\wkhtmltopdf.exe");
    pi.CreateNoWindow = true;
    pi.UseShellExecute = false;
    pi.WorkingDirectory = @"c:\wkhtmltopdf\";
    pi.Arguments = "http://www.google.com gogl.pdf";
    
    using (var process = Process.Start(pi))
    {
        process.WaitForExit(99999);
        Debug.WriteLine(process.ExitCode);
    }
    

    (answer repeated from https://stackoverflow.com/a/11992062/694325)