How to convert a protected HTML page (ASP.NET MVC 3) to PDF using wkhtmltopdf?

I would like to convert the HTML + CSS page to a PDF file. I tried wkhtmltopdf and I had a problem because the page I want to access requires authentication on the Site.

The page I would like to convert to PDF has the following URL: http: // [WEBSITE] / PDFReport / 33

If I try to access it without authentication, I will be redirected to the login page.

Therefore, when I use wkhtmltopdf, it converts my login page to PDF ...

The authentication method that I use in my ASP.NET MVC application is SimpleMembership:

[Authorize]
public ActionResult PDFReport(string id)
{
}

I execute wkhtmltopdf.exe with System.Diagnostics.Process:

FileInfo tempFile = new FileInfo(Request.PhysicalApplicationPath + "\\bin\\test.pdf");

StringBuilder argument = new StringBuilder();
argument.Append(" --disable-smart-shrinking");
argument.Append(" --no-pdf-compression");
argument.Append(" " + "http://[WEBSITE]/PDFReport/33");
argument.Append(" " + tempFile.FullName);

// to call the exe to convert
using (Process p = new System.Diagnostics.Process())
{
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.CreateNoWindow = true;
    p.StartInfo.FileName = Request.PhysicalApplicationPath + "\\bin\\wkhtmltopdf.exe";
    p.StartInfo.Arguments = argument.ToString();
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.RedirectStandardError = true;

    p.Start();
    p.WaitForExit();
}

, PDF ?

+3
1

. , WKHTMLTOPDF - Webkit (QT, , ), , , / cookie , .

:

`/path/wkhtmltopdf --cookie-jar my.jar --username myusername --password URL- mypassword

:

  • my.jar - jar, cookie.
  • username - name , myusername - post
  • password - name , mypassword - post
  • URL - URL-

, - , , HTTP-, . WKHTMLTOPDF , , --cookie-jar my.jar . !

, ( cookie, , ..). PHP CURL - , ASP.NET(, ? http://support.microsoft.com/kb/303436), , :

  • CURL
  • HTML-
  • ( base)
  • plain 'ol WKHTMLTOPDF

, , , , 0.10 WKHTMLTOPDF.

+1

All Articles