码迷,mamicode.com
首页 > Web开发 > 详细

网页转换为PDF

时间:2020-07-07 15:32:59      阅读:93      评论:0      收藏:0      [点我收藏+]

标签:htm   ptr   ota   int   bsp   prot   ring   http   doc   

引用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

标签:htm   ptr   ota   int   bsp   prot   ring   http   doc   

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

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!