Convert Aspx to PDF

Introduction:
Now days many reporting is done on asp.net page and many times we need to convert these pages to PDF file. below is simple solution. Suggested solution can convert following to PDF.


  1. Asp.net aspx page to PDF.
  2. PHP to PDF. (By hosting this code as separate web application) 
  3. HTML to PDF.
  4. URL to PDF and many more....


Solution:

For this you have to include "wkhtmltopdf.exe" and some other files into your asp.net project.(These files are available in sample project attached.)

After that you have to add a new page into your web application. this page just revive the URL of page to be converted into PDF. Code should be placed into pageload event of new added page and its simple.this page will convert the URL page into PDF file and download the PDF file on client system.

Code :

protected void Page_Load(object sender, EventArgs e)
{
        String URL = Request.QueryString["URL"].ToString();
        string args = string.Format("\"{0}\" - ", );
        var startInfo = new ProcessStartInfo(Server.MapPath("wkhtmltopdf.exe"), args)
        {
            UseShellExecute = false,
            RedirectStandardOutput = true
        };

        startInfo.CreateNoWindow = true;
        var proc = new Process { StartInfo = startInfo };
        proc.Start();

        string output = proc.StandardOutput.ReadToEnd();
        byte[] buffer = proc.StandardOutput.CurrentEncoding.GetBytes(output);
        proc.WaitForExit();
        proc.Close();
        Response.ContentType = "application/pdf";
        Response.AddHeader("Content-Disposition", "attachment; filename=myfile.pdf");
        Response.BinaryWrite(buffer);
        Response.End();
}

Click here to Download sample project...

Follow us on below channels to stay updated for new post and tutorials:
Blog: http://easytutorials4all.blogspot.com
Google+ : https://plus.google.com/u/0/112994256924713874536
Facebook Page : https://facebook.com/EasyTutorials4All
Youtube: https://www.youtube.com/channel/UCod8G-TeTUTz9DjaqK1C5-g

Comments

Popular posts from this blog

Encrypt data in browser with Javascript and Decrypt on server with asp.net c#