我目前正在为一个为工作而创建的网站编写一个附件。它从HTML模板生成PDF,然后将其提供给浏览器,以便打印出来
我创建了一个非常有效的小测试。我遇到的问题是,当我编写一个更完整的测试时,当我单击generate按钮时,什么都不会发生。在页面加载的第一个页面中,会创建PDF并显示在浏览器中。在第二页上,我什么也没有得到,也没有错误消息,这使得故障排除变得困难。这两页的代码几乎完全相同,所以我真的很困惑到底发生了什么。
我将发布两个版本的代码。希望你们能弄清楚发生了什么。
工作页面
<%@ Page Language="C#" %>
<!DOCTYPE html>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
string filePath = Server.MapPath("/test.pdf");
string html = "<h1>Hello World</h1>";
PdfSharp.Pdf.PdfDocument my_pdf = TheArtOfDev.HtmlRenderer.PdfSharp.PdfGenerator.GeneratePdf(html, PdfSharp.PageSize.Letter);
my_pdf.Save(filePath);
byte[] docStream = System.IO.File.ReadAllBytes(filePath);
Response.ClearContent();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "inline; filename=test.pdf");
Response.AddHeader("Content-Length", docStream.GetLength(0).ToString());
Response.BinaryWrite(docStream);
Response.End();
System.IO.File.Delete(filePath);
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
</div>
</form>
</body>
</html>
非工作页面
<%@ Page Language="C#" %>
<!DOCTYPE html>
<script runat="server">
protected void Page_Load(object sender, EventArgs e)
{
frmMain.Style.Add(HtmlTextWriterStyle.Width, "900px");
frmMain.Style.Add(HtmlTextWriterStyle.MarginLeft, "auto");
frmMain.Style.Add(HtmlTextWriterStyle.MarginRight, "auto");
tblForm.Style.Add(HtmlTextWriterStyle.MarginRight, "auto");
tblForm.Style.Add(HtmlTextWriterStyle.MarginLeft, "auto");
}
protected void generate_pdf(object sender, EventArgs e)
{
string html_page = System.IO.File.ReadAllText(Server.MapPath("/nice_letter.html"));
string filePath = Server.MapPath($"/{RandomString(10, true)}.pdf");
html_page = html_page.Replace("{{letter_date}}", txtLetterDate.Text);
html_page = html_page.Replace("{{recipient_name}}", txtRecipientName.Text);
html_page = html_page.Replace("{{patient_name}}", txtPatientName.Text);
html_page = html_page.Replace("{{appointment_date}}", txtAppointmentDate.Text);
PdfSharp.Pdf.PdfDocument my_pdf = TheArtOfDev.HtmlRenderer.PdfSharp.PdfGenerator.GeneratePdf(html_page, PdfSharp.PageSize.Letter);
my_pdf.Save(filePath);
byte[] docStream = System.IO.File.ReadAllBytes(filePath);
Response.ClearContent();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition", "inline; filename=letter.pdf");
Response.AddHeader("Content-Length", docStream.GetLength(0).ToString());
Response.BinaryWrite(docStream);
Response.End();
System.IO.File.Delete(filePath);
}
public string RandomString(int size, bool lowerCase = false)
{
Random _random = new Random();
var builder = new StringBuilder(size);
char offset = lowerCase ? 'a' : 'A';
const int lettersOffset = 26;
for (var i = 0; i < size; i++)
{
var @char = (char)_random.Next(offset, offset + lettersOffset);
builder.Append(@char);
}
return lowerCase ? builder.ToString().ToLower() : builder.ToString();
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="frmMain" runat="server">
<center><h1>Nice Letter</h1></center>
<asp:ScriptManager ID="smMain" runat="server"></asp:ScriptManager>
<asp:UpdatePanel ID="upMain" runat="server">
<ContentTemplate>
<table id="tblForm" runat="server">
<tr>
<td><asp:Label ID="lblLetterDate" Text="Letter Date: " runat="server"></asp:Label></td>
<td><asp:TextBox ID="txtLetterDate" Width="150" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td><asp:Label ID="lblRecipientName" Text="Recipient: " runat="server"></asp:Label></td>
<td><asp:TextBox ID="txtRecipientName" Width="300" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td><asp:Label ID="lblPatientName" Text="Patient Name: " runat="server"></asp:Label></td>
<td><asp:TextBox ID="txtPatientName" Width="300" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td><asp:Label ID="lblAppointmentDate" Text="Appointment Date: " runat="server"></asp:Label></td>
<td><asp:TextBox ID="txtAppointmentDate" Width="150" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td></td>
<td><asp:Button ID="cmdCreatePDF" runat="server" Text="Create PDF" OnClick="generate_pdf" /></td>
</tr>
</table>
</ContentTemplate>
</asp:UpdatePanel>
</form>
</body>
</html>
我解决了这个问题。显然,这是因为我通过更新面板进行ajax请求。没有ajax,它工作得很好。