Evert
Evert

Reputation: 11

ASP.Net C# generate and download PDF File

I'm trying to generate and download a pdf file. It keeps saying that the file type is wrong or that the file is damaged.

Here is my code

 protected void downloadFile(int index)
    {

        string strContent = "<html> <body> <div> <b> Tjeu! </b> </div> <div> <INPUT TYPE=CHECKBOX NAME=\"maillist\" disabled=\"disabled\">Yes! Put me on the list! </div> </body> </html>";
        string attach = "attachment; filename=wordtest.pdf";
        HttpContext.Current.Response.Clear();
        HttpContext.Current.Response.Charset = "";
        HttpContext.Current.Response.ContentType = System.Net.Mime.MediaTypeNames.Application.Pdf;

        HttpContext.Current.Response.AddHeader("content-disposition", attach);

        HttpContext.Current.Response.Write(strContent);
        HttpContext.Current.Response.End();
        HttpContext.Current.Response.Flush();
    }

Thx for the help ;)

Upvotes: 1

Views: 8662

Answers (1)

Grant Thomas
Grant Thomas

Reputation: 45058

Because strContent is an HTML string, not the contents of a PDF file.

PDF has a format of its own and isn't so easily created by the means you expect to use here, and, in fact, even dedicating yourself to programming a 'PDF Writer' would be quite a feat of work - there are already people who have worked / are working on this, so, if you really need PDF, I'd suggest you use a third-party library.

Here's one, for example: http://sourceforge.net/projects/pdfsharp/

Upvotes: 2

Related Questions