如何将渲染的HTML页面转换为PDF
问题描述:
大家好,
我有一个.aspx页面,其中包含图像,表内容和长字符串.
喜欢它包含该网站的所有详细信息.现在,我需要将此页面html(如渲染.aspx文件后,我们将其转换为html页面)转换为PDF.
谁能帮我这个.我在google中搜索n发现了一些示例,但是所有这些仅是几行,如5到10行普通字符串并将其转换为pdf.
但是我想将整个页面的内容转换为pdf.
预先感谢.
Hi All,
I have an .aspx page with image, table contents and long strings.
Like it contains all the details of the site. Now I need to convert this page html(as after rendering .aspx file we will get it as html page) to PDF.
Can anyone help me with this. I searched in google n found some examples but all those are only some few line like 5 - 10 lines of normal string and converting it to pdf.
But i wanted to convert the contents of the whole page in pdf.
Thanks in advance.
答
我不认为您应该将其称为渲染",但我认为我理解得对:您可能将HTML文本表示为它出现在来自服务器的HTTP响应中.
我建议使用开源产品iText,更确切地说,使用其.NET端口称为iTextSharp:
http://en.wikipedia.org/wiki/IText [ http://itextpdf.com/ [ ^ ],
http://sourceforge.net/projects/itextsharp/ [
I don''t think you should call it "rendering", but I think I understand you right: you probably mean the HTML text as it appears in the HTTP response which comes from the server.
I would recommend to use the Open-Source product iText, more exactly, its .NET port called iTextSharp:
http://en.wikipedia.org/wiki/IText[^],
http://itextpdf.com/[^],
http://sourceforge.net/projects/itextsharp/[^].
The reference to original Java site of iText is not redundant: I found that it''s hard to find documentation and samples in C# (at the moment I looked for last time), so you would be better off with Java documentation and samples, which is not a big problem as it all looks very similar to C#, and the syntax differences is not a big deal.—SA
使用iTextSharp将html转换为pdf
Converting html to pdf using iTextSharp
using System.IO;
using iTextSharp;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using iTextSharp.text.html.simpleparser;
protected void btn1_Click(object sender, EventArgs e)
{
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);
this.Page.RenderControl(hw);
StringReader sr = new StringReader(sw.ToString());
Document pdfDoc = new Document(PageSize.A4,35f,0f,0f,0f);
HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
//Defining tables
//PdfPTable termsTable = new PdfPTable(2);
//termsTable.SetWidths(new int[2] { 2, 5 });
//Applying Styles
StyleSheet ST = new StyleSheet();
FontFactory.Register("c:\\windows\\fonts\\Corbel.ttf");
ST.LoadTagStyle("body", "face", "Corbel");
ST.LoadTagStyle("body", "size", "12pt");
ST.LoadTagStyle(HtmlTags.P, "face", "Corbel");
ST.LoadTagStyle(HtmlTags.P, "size", "10pt");
ST.LoadTagStyle(HtmlTags.H2, "face", "Corbel");
ST.LoadTagStyle(HtmlTags.H2, "size", "12pt");
ST.LoadTagStyle(HtmlTags.H2, "align", "center");
ST.LoadTagStyle(HtmlTags.TD, "width", "10%");
htmlparser.SetStyleSheet(ST);
//Writing to PDF
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();
}