【问题标题】:how to generate pdf invoces from templates in ASP.NET/Mono MVC application如何从 ASP.NET/Mono MVC 应用程序中的模板生成 pdf 发票
【发布时间】:2026-01-17 15:50:01
【问题描述】:

ASP.NET / Mono MVC2 应用程序用于以 PDF 格式创建和邮寄发票、订单和其他文档。

每个客户都需要不同的文档布局和文档中的徽标。

这个布局应该很容易定制。

如何使用免费软件实现这一点?应用程序中应该有报告生成器或其他东西,可以从设计的布局中创建带有徽标的 pdf 文件。

【问题讨论】:

    标签: asp.net-mvc asp.net-mvc-2 mono pdf-generation report


    【解决方案1】:

    我也有同样的需求,并且一直在努力寻找一个理想的解决方案。我目前正在使用iTextSharp,这似乎是一个流行的选择。它使您可以将 html 页面呈现为 pdf。我发现它对 css 样式的支持有限,这会让你很难得到你想要的东西。下面是一些如何在 mvc 中使用它的示例代码:

        public ActionResult CreatePdfFromView(YourType yourViewData)
        {
            var htmlViewRenderer = new HtmlViewRenderer();
            string htmlText = htmlViewRenderer.RenderViewToString(this, "YourViewName", yourViewData);
    
            byte[] renderedBuffer;
    
            using (var outputMemoryStream = new MemoryStream())
            {
                using (Document document = new Document())
                {
                    PdfWriter writer = PdfWriter.GetInstance(document, outputMemoryStream);
                    writer.CloseStream = false;
                    document.Open();
    
                    iTextSharp.text.Image pic = iTextSharp.text.Image.GetInstance(Server.MapPath("/Content/img/Your_Logo.png"));
                    pic.ScaleToFit(190, 95);
                    pic.SetAbsolutePosition(200, 730);
                    document.Add(pic);
    
                    try
                    {
                        StringReader sr = new StringReader(htmlText);
                        XMLWorkerHelper.GetInstance().ParseXHtml(writer, document, sr);
                    }
                    catch (Exception e)
                    {
                        throw;
                    }
                }
    
                renderedBuffer = new byte[outputMemoryStream.Position];
                outputMemoryStream.Position = 0;
                outputMemoryStream.Read(renderedBuffer, 0, renderedBuffer.Length);
            }
    
            return new BinaryContentResult(renderedBuffer, "application/pdf");
        }
    

    我会说确保使用 XMLWorkerHelper 而不是 HTMLWorker。我认为您必须单独下载 XML 工作器。 Here is a link to the download page..

    我用过的另一个,更多用于创建 excel 报告的是DoodleReport。这要简单得多。基本上是表格数据转储。不知道你是否也可以做图标。

    希望对您有所帮助。好奇看看有没有人有更好的建议。

    【讨论】:

      最近更新 更多