开发者

HTML to PDF Solution (Handling Content, Headers and Footers)

开发者 https://www.devze.com 2023-04-09 22:35 出处:网络
I\'m trying to create a \"report\" by generating a PDF based on HTML. At first, I simply attempted to write raw encoded HTML to a document and then print that document using Javascript. However, this

I'm trying to create a "report" by generating a PDF based on HTML.

At first, I simply attempted to write raw encoded HTML to a document and then print that document using Javascript. However, this gave me little to no control involving headers and footers.

I attempted using thead and tfoot elements, which worked reasonably well in most browsers, however I wasn't able to get the formatting that I was looking for.

Currently - I am trying to work on a server-side solution using iTextSharp in MVC3, however I am a bit lost as to how to proceed, having not worked with iTextSharp much.

Input and Description of Output:

There will be 4 items used in creating the Report:

  • Report Content (which is currently encoded HTML, as I am unsure if decoding will change any formatting)
  • Report Title (will simply be the name of the PDF generated)
  • Report Header (will be displayed at the upper-left of each page)
  • Report Footer (will be displayed at the lower-left of each page)

Controller Action:

//This will be accessed by a jQuery Post
[HttpPost]
public FileStreamResult GeneratePDF(string id)开发者_如何学Go
{
      //Grab Report Item
      ReportClass report = reportingAgent.GetReportById(id);

      Document doc = new Document();

      //Do I need to decode the HTML or is it possible to use the encoded HTML?

      //Adding Headers / Footers

      //Best method of returning the PDF?
}


iTextSharp cannot convert HTML to PDF. It's not what it was designed to do. It was designed to create PDF files from scratch, not converting between various formats into PDF. If you want to convert HTML into PDF you could for example use the the flying-saucer library which is based on iText. I have blogged about how this could be done in .NET using IKVM.NET Bytecode Compiler (ikvmc.exe).

So your controller action might look something along the lines of:

[HttpPost]
public FileStreamResult GeneratePDF(string id)
{
    ReportClass report = reportingAgent.GetReportById(id);
    return PdfResult(report.Html);
}

where PdfResult could be a custom action result taking the raw HTML and outputting the PDF into the response stream:

public class PdfResult : ActionResult
{
    private readonly string _html;
    public PdfResult(string html)
    {
        _html = html;
    }

    public override void ExecuteResult(ControllerContext context)
    {
        var response = context.HttpContext.Response;
        response.ContentType = "application/pdf";
        var builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        using (var bais = new ByteArrayInputStream(Encoding.UTF8.GetBytes(_html)))
        using (var bao = new ByteArrayOutputStream())
        {
            var doc = builder.parse(bais);
            var renderer = new ITextRenderer();
            renderer.setDocument(doc, null);
            renderer.layout();
            renderer.createPDF(bao);
            var buffer = bao.toByteArray();
            response.OutputStream.Write(buffer, 0, buffer.Length);
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消