使用 PDFSharp 从 HTML 模板生成 PDF 并发送到浏览器
Generate PDF with PDFSharp from HTML template and send to browser
我目前正在为我为工作制作的网站编写一个补充。它从 HTML 模板生成 PDF,然后将其提供给浏览器以便打印出来。
我创建了一个完美运行的小测试。我 运行 遇到的问题是当我编写一个更完整的测试时,单击生成按钮时没有任何反应。在页面加载时的第一页中,PDF 被创建并显示在浏览器中。在第二页上,我什么也没得到,也没有错误消息,这使得故障排除变得困难。两个页面的代码几乎相同,所以我真的很困惑。
我将 post 我的代码的两个版本。希望你们能弄清楚发生了什么。
工作页面
<%@ 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.
它工作正常
我目前正在为我为工作制作的网站编写一个补充。它从 HTML 模板生成 PDF,然后将其提供给浏览器以便打印出来。
我创建了一个完美运行的小测试。我 运行 遇到的问题是当我编写一个更完整的测试时,单击生成按钮时没有任何反应。在页面加载时的第一页中,PDF 被创建并显示在浏览器中。在第二页上,我什么也没得到,也没有错误消息,这使得故障排除变得困难。两个页面的代码几乎相同,所以我真的很困惑。
我将 post 我的代码的两个版本。希望你们能弄清楚发生了什么。
工作页面
<%@ 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.
它工作正常