我正在使用ItextSharp从我的aspx页面生成Pdf。我的页面包含大量图像但Itext sharp不会将图像渲染为PDF。
我认为问题在于从我的页面下载Html的方式。它只渲染Html而不是图像,因为图像需要时间来渲染。
protected void btnGenerateReport_Click(object sender, EventArgs e)
{
WebClient myClient = new WebClient();
string myPageHTML = null;
byte[] requestHTML;
// Gets the url of the page
string currentPageUrl = Common.GetAppSettings("SiteURL") + "client/ClientPDFHtml.aspx";
//UTF8Encoding utf8 = new UTF8Encoding();
// currentPageUrl = "http://www.yahoo.com";
//requestHTML = myClient.DownloadData(currentPageUrl);
//myPageHTML = utf8.GetString(requestHTML);
System.Net.WebRequest objRequest = System.Net.HttpWebRequest.Create(currentPageUrl);
StreamReader sr = new StreamReader(objRequest.GetResponse().GetResponseStream());
myPageHTML = sr.ReadToEnd();
sr.Close();
Document document = new Document();
PdfWriter.GetInstance(document, new FileStream(Request.PhysicalApplicationPath + "\\outfile1.pdf", FileMode.Create));
document.Open();
iTextSharp.text.html.simpleparser.StyleSheet styles = new iTextSharp.text.html.simpleparser.StyleSheet();
iTextSharp.text.html.simpleparser.HTMLWorker hw = new iTextSharp.text.html.simpleparser.HTMLWorker(document);
hw.Parse(new StringReader(myPageHTML));
document.Close();
Response.ClearContent();
Response.ClearHeaders();
Response.AddHeader("Content-Disposition", "inline;filename=outfile.pdf");
Response.ContentType = "application/pdf";
Response.WriteFile(Request.PhysicalApplicationPath + "\\outfile1.pdf");
Response.Flush();
Response.Clear();
}
以上代码在包含图像的项目中请求我的aspx页面。
<div>
<h1 style="font-family: arial; color: #00007B; text-align: center; font-size: 34pt">
Test Data
<br />
Text Paragraph</h1>
</div>
<div style="text-align: center;">
<img src="http://localhost:2244/assets/images/ReportImages/image00.png"
alt="image" height="150px" width="275px" />
</div>
这是html的一部分