Best way to convert an aspx page to pdf RRS feed

  • Question

  • User56700650 posted

    Does anyone know a good way to convert an aspx page to a pdf document?
    I have been playing around with iTextSharp but I can't find anything that would do that.

     Any thoughts?

    Sunday, May 4, 2008 8:58 PM

All replies

  • User56700650 posted

    So basically, based on the first link, you're creating the pdf file but where in your code snippet is the page being rendered as pdf?
    Is that where you're adding to the headers?

     Forgive my ignorance but I'm a bit confused by it.

    Sunday, May 4, 2008 9:48 PM
  • User1001868398 posted

    yes you are correct, whatever is displayed on the HTML will be on the PDF, you got it.

    Sunday, May 4, 2008 10:00 PM
  • User56700650 posted

    It appears to be working accept i'm getting an error when I try to open the file saying the file isn't in the proper format or it has been damaged (if sent in an email as an attachment).

    Any ideas as to the cause of this?

    Sunday, May 4, 2008 10:16 PM
  • User1001868398 posted

    Please paste the error and the file produced, what extension it is? pdf?

    Sunday, May 4, 2008 10:23 PM
  • User56700650 posted

    Here is the code.

    I don't know how to attach the pdf file that is generated.
    The error is being thrown by adobe reader.\

    It states : adobe reader could not open 'test[1].pdf' because it is either not supported file type or because the file has been damaged (for example it was sent as an email attachment and wasn't correctly decoded).

    in page_load 

                string name = "test.pdf";
                FileStream fs = new FileStream ( Server.MapPath("~/") + name, FileMode.Create, FileAccess.Write );
                StreamWriter sw = new StreamWriter ( fs, System.Text.Encoding.GetEncoding ( "gb2312" ) );
                sw.WriteLine ( "Hello World" );
                sw.Close ( );
                // If inside of a page:
                Response.AddHeader ( "Content-Disposition", "attachment; filename=" + Server.UrlEncode ( name ) );
                Response.ContentType = "application/pdf";
                //Response.AddHeader ( "Content-Type", "binary/octet-stream" ); 
                //Response.AddHeader ( "Content-Disposition", "attachment; filename=" + downloadName + "; size=" + downloadBytes.Length.ToString ( ) ); 
                //Response.Flush ( ); 
                //Response.BinaryWrite ( downloadBytes ); 
                Response.Flush ( ); 
                Response.End ( );
                Response.WriteFile ( name );
                Response.End ( ); 


    Sunday, May 4, 2008 10:45 PM
  • User1001868398 posted

    Clear all headers before creating the file 


    Let me know if that helps

    Sunday, May 4, 2008 10:53 PM
  • User56700650 posted

    Nope same thing.

    Sunday, May 4, 2008 11:00 PM
  • User56700650 posted

    Any ideas?


    Wednesday, May 7, 2008 1:01 PM
  • User1001868398 posted

    Sorry, I run out of ideas, I haven't use that code for 2 years now, I'll refresh the code and run it in my Vista to see the results, please do send me the code it you want alpascual at g mail.com

    Thursday, May 8, 2008 12:36 AM
  • User2056512907 posted

    hi mate,

    Open the file using memorystrem object.

    Get the frame count of the file.

    You can then loop through the pages or myou can save the entire file in pdf format.

    No need for third party software.

    cheers mate,

       vijay [:)]




    Thursday, May 8, 2008 12:57 AM
  • User56700650 posted

    That sounds great Vijay.
    I don't have much experience with MemoryStreams or frame counts.
    Could you provide the code snippet for that?

    Thursday, May 8, 2008 7:43 AM
  • User719430396 posted


    you got any solution for converting a aspx page to pdf? in your code in which line r u giving source aspx file name or path?

    i tried to play with pdfsharp library, it is generating pdf file, but can not open the same file. if you got any solution please let me know.


    Friday, March 6, 2009 2:16 PM
  • User-1379735347 posted

    There is api with source code example that converts .net data to a pdf. Not the exact aspx page though so it may not be exactly what you need. It is located at www.datatabletopdf.com if wanted.

    Friday, March 6, 2009 10:38 PM
  • User-1329889962 posted

     http://hamang.net/2008/08/14/html-to-pdf-in-net/ surf this link i convert asp.net to pdf file succesfully.

    Saturday, December 19, 2009 11:31 PM
  • User1370752239 posted

    Even I am working on the same and tried using itextsharp but couldnt find solution. Please help me.

    On my aspx page I have data populated from database based on querystring. Now I want to disply this page as pdf. How do I do this?

    The above links you provided are not working. Can you please provide the right links?


    Monday, March 8, 2010 3:23 PM
  • User1414385962 posted

       public override void VerifyRenderingInServerForm(Control control)

       public override void VerifyRenderingInServerForm(Control control)



    place this code in ur page.. and see it works or not.

    Wednesday, May 12, 2010 3:14 AM
  • User-787943944 posted

    Yes This is Very Good Link

    Its working Fine

    Wednesday, July 21, 2010 7:55 AM
  • User12962599 posted


    it is possible to convert aspx page to pdf using iTextSharp.

    first you add reference to your application (itextSharp.dll)

    then. add

    using iTextSharp.text;
    using iTextSharp.text.pdf;
    using iTextSharp.text.html;
    using iTextSharp.text.html.simpleparser;

    after adding namespaces write this code on button click

    string attachment = "attachment; filename=" + filename+ ".pdf";
    Response.AddHeader("content-disposition", attachment);
    Response.ContentType = "application/pdf";
    StringWriter stw = new StringWriter();
    HtmlTextWriter htextw = new HtmlTextWriter(stw);
    .AddStyleAttribute("font-size", "7pt");
    .AddStyleAttribute("color", "Black");

    Panel_Name.RenderControl(htextw);//Name of the Panel
    Document document = new Document();
    = new Document(PageSize.A4, 5, 5, 15, 5);
    FontFactory.GetFont("Arial", 50, iTextSharp.text.BaseColor.BLUE);
    PdfWriter.GetInstance(document, Response.OutputStream);

    StringReader str = new StringReader(stw.ToString());
    HTMLWorker htmlworker = new HTMLWorker(document);


    then copy and paste this code in some where on code

    public override void VerifyRenderingInServerForm(Control control)


    Nidhin Das K (Assyst International)

    Tuesday, July 19, 2011 8:10 AM
  • User1176631705 posted


    I am able to convert the aspx to pdf using itextSharp but the css alignement is not correct.

    The entire page alignment is disturbed.

    Please help.



    Wednesday, October 5, 2011 11:21 PM