我尝试使用 itextsharp 从 html 页面视图创建一个 pdf 文件。
此代码块有效,但我从 url 获取 html 代码字符串,我想将此字符串转换为。因此,我可以通过提供 url 来将任何站点视图转换为 pdf。谢谢..
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=TestPage.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
WebClient wc = new WebClient();
string htmlText = wc.DownloadString("http://");
this.Page.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4, 10f, 10f, 100f, 0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();