网页转换为PDF
Posted mel-hu-95
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了网页转换为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 }
以上是关于网页转换为PDF的主要内容,如果未能解决你的问题,请参考以下文章