使用 iTextSharp 问题创建和下载 (HTML 到) PDF
本文关键字:HTML PDF 下载 iTextSharp 问题 创建 使用 | 更新日期: 2023-09-27 18:32:48
我可以毫无问题地成功创建/生成PDF文件。但是当我尝试在生成后下载它时,执行被重定向到,
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
}
在按钮的单击事件中,我创建了按预期工作的PDF文件,如下所示。
using System.Data;
using iTextSharp.text;
using iTextSharp.text.html.simpleparser;
using iTextSharp.text.pdf;
using System.Net;
protected void Button2_Click(object sender, EventArgs e)
{
try
{
string htmlContent = "<div> PDF Code </div>"; // you html code (for example table from your page)
Document document = new Document();
string FileName = Guid.NewGuid().ToString();
PdfWriter.GetInstance(document, new FileStream("C:''...''...''PDF''" + FileName + ".pdf", FileMode.Create));
document.Open();
HTMLWorker worker = new HTMLWorker(document);
worker.Parse(new StringReader(htmlContent));
document.Close();
//To download same PDF I write below code
Response.Clear();
string pdfPath = Server.MapPath(@"~'PDF'" + FileName + ".pdf");
WebClient client = new WebClient();
Byte[] buffer = client.DownloadData(pdfPath);
Response.ContentType = "application/pdf";
Response.AddHeader("content-length", buffer.Length.ToString());
Response.BinaryWrite(buffer);
Response.Flush();
HttpContext.Current.ApplicationInstance.CompleteRequest();
////Response.End();
}
catch (Exception ex)
{
Response.Write(ex.Message.ToString());
}
}
此代码不会引发任何异常,而是重定向到上面的提及方法。请帮忙。
我想要的是按钮的点击事件,我想生成 PDF 并下载相同的内容。尝试了很多事情,但没有成功。
[...]CompleteRequest() 方法在被调用时不会结束执行。因此,如果这真的是您想要做的,那么Response.Redirect(string)将是您要走的路。
CompleteRequest()只是绕过了 Response.End() 方法,该方法生成了你提到的 ThreadAbortException,但至关重要的是 CompleteRequest() 刷新了响应缓冲区。这意味着 HTTP 302 重定向响应将发送到您调用 CompleteRequest() 的行的浏览器,这使您有机会在将响应发送给用户后执行不影响响应的操作。[...]
源
我最近能够通过向事件添加以下代码来解决我的问题page_load()
,
protected void Page_Load(object sender, EventArgs e)
{
ScriptManager scriptManager = ScriptManager.GetCurrent(this.Page);
scriptManager.RegisterPostBackControl(this.Button2);
//where button2 is the button by which pdf is generated and post back happens.
}
下面的新代码工作绝对正常。
protected void Button2_Click(object sender, EventArgs e)
{
try
{
string htmlContent = "<div> PDF Code </div>"; // you html code (for example table from your page)
Document document = new Document();
string FileName = Guid.NewGuid().ToString();
PdfWriter.GetInstance(document, new FileStream("C:''...''...''PDF''" + FileName + ".pdf", FileMode.Create));
document.Open();
HTMLWorker worker = new HTMLWorker(document);
worker.Parse(new StringReader(htmlContent));
document.Close();
Response.ContentType = "application/pdf";
Response.AppendHeader("Content-Disposition", "attachment; filename=" + FileName + ".pdf");
Response.TransmitFile(Server.MapPath(@"~'PDF'" + FileName + ".pdf"));
Response.End();
}
catch (Exception ex)
{
Response.Write(ex.Message.ToString());
}
}
在
点击事件中添加此代码
Response.ContentType = "application/pdf";
Response.AddHeader("content-disposition", "attachment;filename=Panel.pdf");
Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);
pnlPerson.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();