[英]Convert panel HTML to PDF using C# ASP.NET
I have a basic form with mostly textboxes and a few checkboxes and radio buttons.我有一个基本的表单,主要是文本框和一些复选框和单选按钮。 Pretty much when a user fills it out and hits submit, I need it to convert and display a PDF with the same structure and offer a print option.
几乎当用户填写并点击提交时,我需要它来转换和显示具有相同结构的 PDF 并提供打印选项。 I have searched online and I have tried most of the options but nothing works.
我在网上搜索并尝试了大部分选项,但没有任何效果。
I don't expect this to be too difficult, but I am fairly new to C# and can't figure out how to make an HTML panel into a PDF and print it.我不认为这会太难,但我对 C# 相当陌生,无法弄清楚如何将 HTML 面板制作成 PDF 并打印出来。 Any help would be appreciated, thanks in advance!
任何帮助将不胜感激,提前致谢!
Here is the HTML for one of the labels and textboxes and the submit button:这是标签和文本框之一以及提交按钮的 HTML:
<div>
<asp:Label ID="lblDate" runat="server" Text="Date:"></asp:Label>
<asp:TextBox ID="txtDate" runat="server"></asp:TextBox>
</div>
<asp:Button ID="SubmitButton" runat="server" Text="Button" OnClick="btnSubmit"/>
Once clicked, the .cs
page will go through this:单击后,
.cs
页面将执行以下操作:
litDate.Text = "Date: " + txtDate.Text + "<br />";
and update the panel to display the value:并更新面板以显示值:
<asp:Panel ID="PDFPanel" runat="server" Visible="false">
<asp:Literal ID="litDate" runat="server"></asp:Literal>
</asp:Panel>
I am not sure if the panel is needed, but this is how I was able to be sure I was getting the value back.我不确定是否需要面板,但这就是我能够确定我收回价值的方式。 Is there a way to go straight from submit to PDF and print?
有没有办法直接从提交到 PDF 并打印?
Use something like http://code.google.com/p/wkhtmltopdf/使用类似http://code.google.com/p/wkhtmltopdf/ 的东西
This roughly outlines how to do it: https://stackoverflow.com/a/18767473/181771这大致概述了如何做到这一点: https : //stackoverflow.com/a/18767473/181771
I made an asp.net website demo taking the contents of a panel or div and generating a pdf from the html.我制作了一个 asp.net 网站演示,获取面板或 div 的内容并从 html 生成 pdf。 This is using NReco.PdfGenerator (another wkhtmltopdf wrapper):
这是使用 NReco.PdfGenerator(另一个 wkhtmltopdf 包装器):
https://github.com/jay8anks/Asp.net-HtmlToPdf_NReco.PdfGenerator https://github.com/jay8anks/Asp.net-HtmlToPdf_NReco.PdfGenerator
Yeah, it's a webform, but anyone sharp enough to do mvc should be able to adapt it fairly easily.是的,这是一个网络表单,但任何足够敏锐的人都应该能够很容易地适应它。
Basically, it is using javascript to get the html between a panel (div) tag and store it in a hiddenfield.基本上,它使用 javascript 来获取面板 (div) 标签之间的 html 并将其存储在隐藏字段中。 Pretty straight forward, except if you want to have css or images, you can't use a relative url for them.
非常简单,除非您想要使用 css 或图像,否则不能为它们使用相对 url。 I actually just embedded the css inline as a string, but there are other ways of doing it.
我实际上只是将 css 内联嵌入为一个字符串,但还有其他方法可以做到这一点。
Then in the codebehind, this gets the html and generates a pdf from it:然后在代码隐藏中,这将获取 html 并从中生成一个 pdf:
protected void SaveHtmlToPdf()
{
string htmlOutput = Server.UrlDecode(HiddenField1.Value);
htmlOutput = string.Join(" ", System.Text.RegularExpressions.Regex.Split(htmlOutput, @"(?:\r\n|\n|\r|\t)"));
htmlOutput = htmlOutput.Replace("\"", "'");
string headerStyle = HeaderStyle();
string finalHtml = headerStyle + htmlOutput;
var strWr = new StringWriter();
var htmlWr = new HtmlTextWriter(strWr);
// base.Render(htmlWr);
var htmlToPdf = new HtmlToPdfConverter();
string filename = (orderId + ".pdf");
string filepath = "~/sales_pdfs";
string combinedFilePath = Path.Combine(Server.MapPath(filepath), filename).ToString();
for (int i = 1; i <= NumberOfRetries; ++i)
{
try
{
htmlToPdf.GeneratePdf(finalHtml, null, combinedFilePath);
break; // When done we can break loop
}
catch (IOException e)
{
// You may check error code to filter some exceptions, not every error
// can be recovered.
if (i <= NumberOfRetries)
{
Thread.Sleep(DelayOnRetry);
}
ltMsg.Text = "There was a problem creating the PDF";
}
}
}
This puts a pdf in a directory so it can be downloaded, but there are other ways that could be handled, as well.这会将 pdf 放在一个目录中,以便可以下载它,但还有其他方法可以处理。 We actually wanted a copy of the PDF that someone was generating, so this was the direction we went in.
我们实际上想要一份有人生成的 PDF 的副本,所以这就是我们的方向。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.