2017-09-29 82 views
0

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

我曾尝试使用完整的URL和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"); 
} 

回答

0

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

+0

阅读[pdfHTML教程简介](https://developers.itextpdf.com/content/itext-7-converting-html-pdf-pdfhtml)。您正在使用旧技术将HTML转换为PDF。 iText 7 + pdfHTML插件支持未关闭的标签,并且还支持开箱即用的base64图像。查看常见问题解答条目[可以pdfHTML将Base64图像渲染为PDF?](https://developers.itextpdf.com/content/itext-7-converting-html-pdf-pdfhtml/chapter-7-frequently-asked-questions-about -pdfhtml/can-pdfhtml-render-base64-images-pdf)升级,你的问题就解决了。 –

+0

我通过nuget添加了这个不是支持的分发方法吗?我会认为它会使用最新的。 – Swazimodo