开发者

ASP.Net C# generate and download PDF File

开发者 https://www.devze.com 2023-03-01 01:45 出处:网络
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.

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> 开发者_如何学Python</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 ;)


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/

0

精彩评论

暂无评论...
验证码 换一张
取 消