ASP.Net 页面导出为 pdf

发布于 2024-10-30 02:45:11 字数 1193 浏览 4 评论 0原文

我在将 ASP.Net 导出为 PDF 时遇到了困难。下面是我的代码。请帮忙。

Response.Clear();    
Response.Buffer = true;    
Response.Charset = Encoding.UTF8.HeaderName;    
Response.ContentEncoding = Encoding.UTF8;

Response.Write(string.Format("<meta http-equiv=Content-Type content=text/html;charset={0}>", Encoding.UTF8.HeaderName));

Response.ContentType = "application/pdf";    
Response.Headers.Add("Content-disposition", "attachment; filename=pdffilename.pdf");

StringBuilder sb = new StringBuilder();    
sb.Append("MIME-Version: 1.0\r\n");    
sb.Append("X-Document-Type: Worksheet\r\n");    
sb.Append("Content-Type: multipart/related; boundary=\"----=mtrSystem\"\r\n\r\n\r\n");    
sb.Append("------=mtrSystem\r\n");    
sb.Append("Content-Type: text/html; charset=\"utf-8\"\r\n");    
sb.Append("<html xmlns:o=\"urn:schemas-microsoft-com:office:office\"\r\n");    
sb.Append("xmlns:x=\"urn:schemas-microsoft-com:office:pdf\">\r\n\r\n\r\n");     
sb.Append("------=mtrSystem\r\n");    
sb.Append("Content-ID: baiduimg\r\n");    
sb.Append("Content-Transfer-Encoding: base64\r\n");    
sb.Append("Content-Type: image/png\r\n\r\n");

我可以使用这种方式将 ASP.Net 页面导出为 PDF 吗?

I got stuck with my ASP.Net export to PDF. Below are my codes. Please help.

Response.Clear();    
Response.Buffer = true;    
Response.Charset = Encoding.UTF8.HeaderName;    
Response.ContentEncoding = Encoding.UTF8;

Response.Write(string.Format("<meta http-equiv=Content-Type content=text/html;charset={0}>", Encoding.UTF8.HeaderName));

Response.ContentType = "application/pdf";    
Response.Headers.Add("Content-disposition", "attachment; filename=pdffilename.pdf");

StringBuilder sb = new StringBuilder();    
sb.Append("MIME-Version: 1.0\r\n");    
sb.Append("X-Document-Type: Worksheet\r\n");    
sb.Append("Content-Type: multipart/related; boundary=\"----=mtrSystem\"\r\n\r\n\r\n");    
sb.Append("------=mtrSystem\r\n");    
sb.Append("Content-Type: text/html; charset=\"utf-8\"\r\n");    
sb.Append("<html xmlns:o=\"urn:schemas-microsoft-com:office:office\"\r\n");    
sb.Append("xmlns:x=\"urn:schemas-microsoft-com:office:pdf\">\r\n\r\n\r\n");     
sb.Append("------=mtrSystem\r\n");    
sb.Append("Content-ID: baiduimg\r\n");    
sb.Append("Content-Transfer-Encoding: base64\r\n");    
sb.Append("Content-Type: image/png\r\n\r\n");

Can i use this way to export my ASP.Net page to PDF?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

听,心雨的声音 2024-11-06 02:45:11

Html-to-pdf.net 允许将 html 转换为 pdf。要从 asp.net 页面获取 html,请使用以下代码:

StringWriter sw = new StringWriter();
Server.Execute("PageToConvert.aspx", sw);
string htmlCodeToConvert = sw.GetStringBuilder().ToString();

然后将 html 传递到 pdf 生成器:

public byte[] GetPdfBytesFromHtmlString (string htmlString)

然后您可以将字节保存到响应中以发送到客户端,或作为本地文件保存在服务器上。

编辑:

需要记住的一点是,html-to-pdf 确实 需要花钱,但对于我的上一个项目来说,这是一笔合理的开支。您可以使用试用版来确定您需要什么。

Html-to-pdf.net allows for converting html to pdf. TO get the html from an asp.net page, use the following code:

StringWriter sw = new StringWriter();
Server.Execute("PageToConvert.aspx", sw);
string htmlCodeToConvert = sw.GetStringBuilder().ToString();

Then pass the html to the pdf generator:

public byte[] GetPdfBytesFromHtmlString (string htmlString)

You can then save the bytes to the response to send to client, or save on the server as a local file.

EDIT:

Something to keep in mind, html-to-pdf does cost money, but for my last project it was a justified expense. You can use the trial version to figure out what you needd.

浅忆流年 2024-11-06 02:45:11

我不完全确定你想要做什么,但一般来说我强烈推荐 PDFSharp 用于创建 PDF 文档代码...

I'm not entirely sure what you're trying to do, but generally I can highly recommend PDFSharp for creating PDF documents in code...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文