网页转换为PDF

2021-03-04 14:27

阅读:420

标签: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


评论


亲,登录后才可以留言!