关于我想怎么做,我有一些一般性的想法。
我想要做的是:创建一个非常简单的前端CMS系统,其中报告将从模板生成,使用jQuery(拖放等),报告中包含的数据将占位符被导入例如名称,地址等。有权访问数据的不同用户可以更改此数据。
我想我需要将这个HTML转换为xsl-fo格式,然后将其生成为PDF,因为xsl-fo将为我提供在PDF上自定义显示数据的主要优势,即数据将显示我想要的方式。这也使我能够使用xslt(或其他东西?)在xsl-fo中进行查找,以导入最新更新的数据库值。实际上从xsl-fo转换为PDF的工具看起来很符合我的账单:fo.net。最终我需要使用一些代码,但我可以避免它,我想要。
记住:
问题:
页面上的HTML + CSS可能非常复杂/灵活,所以我可能需要使用jQuery将CSS内联添加到元素中,因此我考虑使用XSL-FO,因为我可以生成可以读取的标签这些数据以某种方式将其放在PDF上,在回答我的问题时请记住这一点(如果你选择的话!):)
我发现PDFsharp和MigraDoc非常适合pdf生成。
我创建了一个pdf实用程序......
using System;
using System.IO;
using System.Web;
using System.Web.Mvc;
using PdfSharp.Pdf;
//Controller for a PdfResult
namespace Web.Utilities
{
public class PdfResult : ActionResult
{
public String Filename { get; set; }
protected MemoryStream pdfStream = new MemoryStream();
public PdfResult(PdfDocument doc)
{
Filename = String.Format("{0}.pdf", doc.Info.Title);
doc.Save(pdfStream, false);
}
public PdfResult(String pdfpath)
{
/* optional if requried ToString save ToString file System */
throw new NotImplementedException("PdfResult is just an example and does not serve files from the filesystem.");
}
public override void ExecuteResult(ControllerContext context)
{
context.HttpContext.Response.Clear();
context.HttpContext.Response.ContentType = "application/pdf";
context.HttpContext.Response.AddHeader("Content-Disposition", "attachment; filename=" + Filename); // specify filename
context.HttpContext.Response.AddHeader("content-length", pdfStream.Length.ToString());
context.HttpContext.Response.BinaryWrite(pdfStream.ToArray());
context.HttpContext.Response.Flush();
pdfStream.Close();
context.HttpContext.Response.End();
}
}
}
然后你可以在控制器中渲染pdf视图......
public ActionResult Download()
{
Document document = new Document();
document.Info.Title = "Hello";
Section section = document.AddSection();
section.AddParagraph("Hello").AddFormattedText("World", TextFormat.Bold);
PdfDocumentRenderer renderer = new PdfDocumentRenderer();
renderer.Document = document;
renderer.RenderDocument();
return new PdfResult(renderer.PdfDocument);
}
我发现这是一个非常简洁,易于控制的方法,将pdf放入mvc。
为了回答我自己的问题,我决定使用Fo.NET,它是Apache的Fop.Net的C#实现。我将动态生成我的XML文件,然后将此文档转换为XSL:Fo xml文件,然后发送以创建PDF。
我已经成功地做到了这一点,这将使我能够在将来抛弃Fo.Net并获得另一个软件,甚至可以根据需要编写我自己的软件。希望在接下来的几个月里,我会更加坚定地回答我的选择实际上是多么灵活。 :)
我将使用jQuery和jQuery UI处理前端。