首页 > Web开发 > 详细

网页转换为PDF

时间:2020-07-07 15:03:04      阅读:75      评论:0      收藏:0      [点我收藏+]

引用NuGet包 OpenHtmlToPdf

 1         protected void btnExportPDF_Click(object sender, EventArgs e)
 2         {
 3             using (WebClient wc = new WebClient())
 4             {
 5                 wc.Encoding = Encoding.UTF8;
 6                 string url = Request.Url.ToString();
 7                 url = url.Replace(Request.Url.AbsolutePath, "/Print.aspx");
 8                 string html = wc.DownloadString(url);
 9 
10                 var document = Pdf.From(html)
11                     .OfSize(OpenHtmlToPdf.PaperSize.LetterRotated)
12                     .WithGlobalSetting("margin.top", "0.4cm");
13                 if (IntPtr.Size == 4)
14                 {
15                     document = document.WithObjectSetting("load.zoomFactor", "1.5");
16                 }
17                 var result = document.Content();
18 
19                 HttpContext.Current.Response.Clear();
20                 HttpContext.Current.Response.AddHeader("content-disposition", "attachment;filename=PDF" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".pdf");
21                 HttpContext.Current.Response.ContentType = "application/octet-stream";
22                 HttpContext.Current.Response.BinaryWrite(result);
23                 HttpContext.Current.Response.End();
24             }
25         }

 

网页转换为PDF

原文:https://www.cnblogs.com/Mel-Hu-95/p/13260599.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!