如何从ASP.NET/MVO应用程序中的模板生成pdf invoces

问题描述:

ASP.NET/Mono MVC2应用程序用于创建和发送PDF格式的发票,订单和其他文档。如何从ASP.NET/MVO应用程序中的模板生成pdf invoces

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

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

如何使用免费软件实现这一点?应该有报告生成器或应用程序中的东西,并创建PDF格式的文件与设计布局的标志。

我有同样的需求,并且真的很难找到一个理想的解决方案。我目前使用iTextSharp,这似乎是一个流行的选择。它可以让你呈现一个PDF格式的HTML页面。我发现它对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"); 
    } 

我会说一定要使用,而不是HTMLWorker的XMLWorkerHelper。我认为你必须单独下载XML工作。 Here is a link to the download page.

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

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