网页转换为PDF
引用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 }