ASP.Net页面导出为pdf

我的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.0rn");    
sb.Append("X-Document-Type: Worksheetrn");    
sb.Append("Content-Type: multipart/related; boundary="----=mtrSystem"rnrnrn");    
sb.Append("------=mtrSystemrn");    
sb.Append("Content-Type: text/html; charset="utf-8"rn");    
sb.Append("<html xmlns:o="urn:schemas-microsoft-com:office:office"rn");    
sb.Append("xmlns:x="urn:schemas-microsoft-com:office:pdf">rnrnrn");     
sb.Append("------=mtrSystemrn");    
sb.Append("Content-ID: baiduimgrn");    
sb.Append("Content-Transfer-Encoding: base64rn");    
sb.Append("Content-Type: image/pngrnrn");
我可以用这种方式将我的ASP.Net页面导出为PDF吗?     
已邀请:
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确实需要花钱,但对于我上一个项目来说这是一笔合理的费用。您可以使用试用版来确定您需要的内容。     
我不完全确定你要做什么,但一般来说我强烈建议使用PDFSharp在代码中创建PDF文档......     

要回复问题请先登录注册