2017-09-29 86 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