将iTextSharp HTML5中的图像渲染为PDF

时间:2017-09-29 21:19:15

标签: html5 itext pdf-generation asp.net-core-mvc .net-4.7

目前我有一个使用.NET 4.7框架的MVC Core v2.0.0应用程序。在此我正在使用iTextSharp尝试将HTML转换为PDF。如果我将图像添加到html中,我会收到以下异常“请求页面1,但文档只有0页”。

我尝试过使用完整网址和base64编码内容。

<img src=\"http://localhost:4808/images/sig1.png\">

<img src=\"data:application/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAIAAAD8GO2jAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAAEnQAABJ0Ad5mH3gAAADsSURBVEhLtc2NbcMgAERhz9JtImXK1gtkq46RU58K9CX&#x2B;KWDpswLhdLd83dZi&#x2B;fyerx24ZEMD4cQgtcOhEfnUjj&#x2B;hEfyoHTU0opzUjvLar72oHW2gh&#x2B;5qhzL/4/v0Dd9/qB3KnOX7L7VDmVN8b6gdyuDjcZf6Wk/vqB08qXHLwUCoHWrZcTwQaoeKthwPkFM7SsuOvQFF1Q5lXm0OKAe1QxlZ6mm3ulA7lGnVgfPUDmWKnoFQO5RB50CoHcpE/0CoHcoMDYTa0QZGB0LtKK8TBkLt4GnOQKgd&#x2B;X/aQKgdMwdC7TF5IC4fiDpwW590JX1NuZQyGwAAAABJRU5ErkJggg==\" alt=\"test.png\">

这是执行转换的辅助方法

public static Stream GeneratePDF(string html, string css = null)
{
    MemoryStream ms = new MemoryStream();

    //HttpRenerer.PdfSharp implemenation
    //PdfSharp.Pdf.PdfDocument pdf =
    //    TheArtOfDev.HtmlRenderer.PdfSharp.PdfGenerator.GeneratePdf(html, PdfSharp.PageSize.Letter, 40);
    //pdf.Save(ms, false);

    try
    {
        //iTextSharp implementation
        //Create an iTextSharp Document which is an abstraction of a PDF but **NOT** a PDF
        using (Document doc = new Document(PageSize.LETTER))
        {
            //Create a writer that's bound to our PDF abstraction and our stream
            using (PdfWriter writer = PdfWriter.GetInstance(doc, ms))
            {
                writer.CloseStream = false;

                if (string.IsNullOrEmpty(css))
                {
                    //XMLWorker also reads from a TextReader and not directly from a string
                    using (StringReader srHtml = new StringReader(html))
                    {
                        doc.Open();
                        iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, srHtml);
                        doc.Close();
                    }
                }
                else
                {
                    using (MemoryStream msCss = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(css)))
                    using (MemoryStream msHtml = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(html)))
                    {
                        doc.Open();
                        iTextSharp.tool.xml.XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, msHtml, msCss);
                        doc.Close();
                    }
                }
            }
        }
    }
    catch (Exception ex)
    {
        ms.Dispose();
        throw ex;
    }

    //I think this is needed to use the stream to generate a file
    ms.Position = 0;
    return ms;
}

这里我正在调用我的帮助方法来生成静态PDF文档。

public async Task<IActionResult> TestGenerateStaticPdf()
{
    //Our sample HTML and CSS
    example_html = "<!doctype html><head></head><body><h1>Test Report</h1><p>Printed: 2017-09-29</p><table><tbody><tr><th>User Details</th><th>Date</th><th>Image</th></tr><tr><td>John Doe</td><td>2017-09-29</td><td><img src=\"http://localhost:4808/images/sig1.png\"></td></tr></tbody></table></body>";
    example_css = "h1 {color:red;} img {max-height:180px;width:100%;page-break-inside:avoid;} table {border-collapse:collapse;width:100%;} table, th, td {border:1px solid black;padding:5px;page-break-inside:avoid;}";

    System.IO.Stream stream = ControllerHelper.GeneratePDF(example_html, example_css);

    return File(stream, "application/pdf", "Static Pdf.pdf");
}

1 个答案:

答案 0 :(得分:0)

事实证明,iTextSharp不尊重doctype而是强制XHTML。根据XHTML,图像标签需要关闭。如果你使用它似乎生成没有例外。但是,我无法获取base64编码的内容进行渲染。在这种情况下没有错误,但图像没有出现。