Calling Wkhtmltopdf to Generate Pdf from Html

Using wkhtmltopdf to generate PDF from HTML

These are just methods. You must put them into a class. So add something like "class MyClass { // yourcode here } around it

wkhtmltopdf - convert html code to pdf directly in C#

wkhtmltopdf is a free tool, but it's not written in .NET and it could be kind of hard to integrate into your application.

you can take a look at iTextSharp, which is free, but cannot handle any kind of html, or you can take a look at commercial tools to convert html to pdf, like ExpertPDF or ABCpdf, that can handle any html/css.

convert html into pdf using wkhtmltopdf in nodejs

You can use wkhtmltopdf module to convert HTML files or strings to PDFs. Call whtmltopdf function with either a URL or an inline HTML string, and it returns a stream that you can read from or pipe to wherever you like (e.g. a file, or an HTTP response).

Here is the step by step guide on Ubuntu 13.10 Desktop(64 bit).

1.Install the wkhtmltopdf command line tool on your system:

$ sudo apt-get install wkhtmltopdf

2.Create the project directory html2pdf and install the wkhtmltopdf module:

$ mkdir html2pdf && cd html2pdf
$ npm install wkhtmltopdf

3.Edit the app.js file and input the following code:

var wkhtmltopdf = require('wkhtmltopdf');
wkhtmltopdf('<h1>Test</h1><p>Hello world</p>', {output: 'out.pdf'});

4.Run the application and a PDF file named out.pdf will be created in the html2pdf directory.

$ node app.js


For Windows:

1.Download wkhtmltopdf command line tool. Note: There are two versions of wkhtmltopdf.exe. One is for 32 bit Windows and the other is for 64 bit Windows.

2.Run the downloaded exe file and install wkhtmltopdf to specified directory, e.g., c:\wkhtmltopdf.

3.Create the project directory html2pdf and install the wkhtmltopdf module:

C:\> md html2pdf
C:\> cd html2pdf
C:\html2pdf> npm install wkhtmltopdf

4.Edit the app.js file and input the following code:

var wkhtmltopdf = require('wkhtmltopdf');
wkhtmltopdf.command = 'c:/wkhtmltopdf/bin/wkhtmltopdf.exe';
wkhtmltopdf('<h1>Test</h1><p>Hello world</p>', {output: 'out.pdf'});

5.After running the application, a PDF file named out.pdf will be created in the html2pdf directory.

C:\html2pdf> node app.js

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

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 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 = " gogl.pdf";

using (var process = Process.Start(pi))

(answer repeated from

Generate PDF using wkhtmltopdf.exe in c#

I think if I understood correctly this is because wkhtmltopdf that is calling the page is not logged in. Wkhtmltopdf is kind of like creating a new incognito browser window without any login cookies/session so the page correctly thinks it's not logged in. You could maby check by debugging the request that the server gets when wkhtmltopdf calls it.

If this is the issue it can be difficult to solve. The solution depends on your login system and what you can do to go around the issue. If you can duplicate the login by using cookies you could maby set the login cookie yourself, see for more info on how to set cookie.

Another option would be to first create a request from a system that returns the logged in HTML and then save that to a file/stream and feed that file/stream to wkhtmltopdf (I'm guessing you could do that using HttpContext.Current.Request or something, I don't know).

Another workaround would be to create a duplicate page of the logged in page that looks exactly like the logged in page but really isn't - this page would just be used to fool wkhtmltopdf. Something like and then use that by calling something like if(url.ToLower() == "") {url=""; }. This could be a security risk depending on what information is shown though.

Hope this helps!

Related Topics

Leave a reply