1

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 ;)

1
  • 1
    When you open an existing PDF file in notepad (for instance) you're not going to see HTML. Renaming an HTML file to .PDF does not make it a PDF. Commented Apr 25, 2011 at 13:35

1 Answer 1

2

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/

Sign up to request clarification or add additional context in comments.

3 Comments

How does a content of a pdf file looks like? I have to generate checkboxes and other html stuff
Use some thirdparty tools to create the PDF file. iTextSharp is a good pick.itextsharp.com
@Evert: Is there a requirement to use PDF? Can't you use HTML?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.