使用 ASP.net、C#、MVC 在模板中生成 pdf
Posted
技术标签:
【中文标题】使用 ASP.net、C#、MVC 在模板中生成 pdf【英文标题】:generate pdf in template using ASP.net, C#, MVC 【发布时间】:2012-01-16 00:33:59 【问题描述】:我是 MVC 的初学者,需要在提供的模板上生成 PDF 发票。在进行了一些谷歌搜索之后,现在我能够生成 pdf 但不能在模板中生成。任何机构都可以帮助我解决这个问题。我在下面写我的代码:
public ActionResult pdfStatement(string InvoiceNumber)
InvoiceNumber = InvoiceNumber.Trim();
ObjectParameter[] parameters = new ObjectParameter[1];
parameters[0] = new ObjectParameter("InvoiceNumber", InvoiceNumber);
var statementResult = _db.ExecuteFunction<Models.Statement>("uspInvoiceStatement", parameters);
Models.Statement statement = statementResult.SingleOrDefault();
return ViewPdf("Invoice", "pdfStatement", statement);
public class PdfViewController : Controller
private readonly htmlViewRenderer htmlViewRenderer;
private readonly StandardPdfRenderer standardPdfRenderer;
public PdfViewController()
this.htmlViewRenderer = new HtmlViewRenderer();
this.standardPdfRenderer = new StandardPdfRenderer();
protected ActionResult ViewPdf(string pageTitle, string viewName, object model)
// Render the view html to a string.
string htmlText = this.htmlViewRenderer.RenderViewToString(this, viewName, model);
// Let the html be rendered into a PDF document through iTextSharp.
byte[] buffer = standardPdfRenderer.Render(htmlText, pageTitle);
// Return the PDF as a binary stream to the client.
return new BinaryContentResult(buffer, "application/pdf");
public class BinaryContentResult : ActionResult
private readonly string contentType;
private readonly byte[] contentBytes;
public BinaryContentResult(byte[] contentBytes, string contentType)
this.contentBytes = contentBytes;
this.contentType = contentType;
public override void ExecuteResult(ControllerContext context)
var response = context.HttpContext.Response;
response.Clear();
response.Cache.SetCacheability(HttpCacheability.Public);
response.ContentType = this.contentType;
using (var stream = new MemoryStream(this.contentBytes))
stream.WriteTo(response.OutputStream);
stream.Flush();
【问题讨论】:
如前所述,iText 可能是要考虑的库。您可能希望查看一个地理工具来定义现有 PDF 的边界,您可以在其中编写以后的动态数据:github.com/applicius/dhek。 【参考方案1】:我会推荐iTextSharp 库。
使用库从 c# 中查看这个 tutorial on how to populate a pdf。在 Adobe Acrobat 中创建包含字段的 pdf,然后使用代码填充字段。
// Open the template pdf
PdfReader pdfReader = new PdfReader(Request.MapPath("~/assets/form.pdf"));
PdfStamper pdfStamper = new PdfStamper(pdfReader, Response.OutputStream);
pdfStamper.FormFlattening = true; // generate a flat PDF
// Populate the fields
AcroFields pdfForm = pdfStamper.AcroFields;
pdfForm.SetField("InvoiceRef", "00000");
pdfForm.SetField("DeliveryAddress", "Oxford Street, London");
pdfForm.SetField("Email", "anon@anywhere.com");
pdfStamper.Close();
【讨论】:
【参考方案2】:这不是问题的答案,而是我在 asp.net 中生成 pdf 的经验。也许它会节省你的时间。 不幸的是,我没有找到足够好的免费工具来生成 pdf 文件。
我尝试使用 HtmlViewRenderer,但它不适用于复杂的 CSS。 比我找到pdfsharp。有很多关于它使用 *** 的好文章。
这有助于我创建发票 - 简单的表格,但我想警告您,您会手动添加行。而且该代码看起来不太好。它也不适用于复杂的 CSS。
现在我们使用PdfCrowd 来制作彩色的大型报告。 这是一项将您的 html 呈现为 pdf 的服务。它运行完美,但不是免费的,最便宜的计划每年花费 10 美元。
在官方网站上,您可以找到 .net 库和 ASP.NET 示例。
【讨论】:
【参考方案3】:试试这个ejsreport
它使用 JSON 和 javascript,但可以完成工作。
我一直在尝试在 MVC 中寻找更简单的报告工具,这是我找到的最好的。
【讨论】:
以上是关于使用 ASP.net、C#、MVC 在模板中生成 pdf的主要内容,如果未能解决你的问题,请参考以下文章
在 ASP.NET MVC 中向控制器传递多个参数;此外,在 LINQ-to-SQL 中生成动态查询
在哪里可以找到在 ASP .NET MVC2 中实现密码恢复的 C# 示例代码