【问题标题】:Styles are not getting Implemented in PDF using ITextSharp [duplicate]样式未使用 ITextSharp 在 PDF 中实现 [重复]
【发布时间】:2019-02-15 14:29:50
【问题描述】:

我正在尝试使用 ITextSharp 从 HTML 字符串生成 PDF。但是外观样式没有得到应用。我在 stackoverflow 上查看了与它相关的帖子,但没有从他们那里得到任何帮助。 请帮助我如何将 pdf 中的 HTML 转换为 HTML UI 中显示的实际样式。 CSS 包含在样式标签中。 这是我的 C# 代码

public static string GeneratePDF(string html, string cssText="")
        {
            try
            {
               // var cssText = "";
                Guid random_guid;
                random_guid = Guid.NewGuid();
                string fileName = random_guid.ToString() + ".pdf";
                string filename_with_folder = @"Temp\" + fileName;
                string fullFilePath = System.IO.Path.Combine(HttpContext.Current.Request.PhysicalApplicationPath, filename_with_folder);
                using (var memoryStream = new MemoryStream())
                {
                    var doc = new Document();
                    PdfWriter writer = PdfWriter.GetInstance(doc, new FileStream(fullFilePath, FileMode.Create));
                    doc.Open();
                    using (var cssMemoryStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(cssText)))
                    {
                        using (var htmlMemoryStream = new MemoryStream(System.Text.Encoding.UTF8.GetBytes(html)))
                        {
                            XMLWorkerHelper.GetInstance().ParseXHtml(writer, doc, htmlMemoryStream, cssMemoryStream);
                        }
                    }
                    doc.Close();
                    fileName = Utility.UploadFileToAzure(fullFilePath, fileName, "application/pdf ");
                    return fileName;
                }
            }
            catch (Exception ex)
            {

                ExceptionHandler.LogError(ex);
            }
            return string.Empty;
        }

这是带有 HTMl 字符串和 css 的代码

   public void test()
        {
            StringBuilder sb = new StringBuilder();
            StringBuilder css = new StringBuilder();
            css.Append(@"<style type='text/css'>

        body{font-family: Segoe, 'Segoe UI', 'DejaVu Sans', 'Trebuchet MS', Verdana, 'sans-serif'; background: #f2f2f2; margin: 0; padding: 0; font-size: 20px; color: #565656;}
        .outerpdfboxCont{width: 100%; background: #fff; margin: 0 auto; padding: 15px 30px; max-width: 80%; display: table;box-shadow: 1px 1px 2px #ddd;}   

        .blueBorderLine{background: #0A82B4; height: 10px; width: 100%; margin: 10px auto;}

        .workingWrapperboxPdf{margin: 10px 0; padding: 10px 0px; display: table;width: 100%; }
        .workingWrapperboxPdf h2{font-size: 36px; font-weight: 500; margin: 40px 0 0; line-height: 40px; color: #000;}
        .workingWrapperboxPdf h3{font-size: 18px; font-weight: 300;line-height: 20px;}

        .darkB{color: #000!important; font-weight: 500!important;}
        .DataboxH{margin: 20px 0; display: table;width: 100%;}
        .border-heading{border-bottom: 1px solid #ddd; font-size: 30px!important; line-height: 35px!important; color: #000!important;width: 100%; padding-bottom: 10px;}

    </style>");
            sb.Append(@"<body>
<div class='outerpdfboxCont'>   
    <div class='blueBorderLine'></div>  

<div class='workingWrapperboxPdf'>  

    <h3>Dalvir Singh</h3>   
    <h3 class='darkB'>India</h3>

    <div class='DataboxH'>
        <h2>Summery Of the PDF</h2>
        <h3 class='darkB'>Summery of pdf will go here</h3>
        <h3>PDF created on 2018-6-12</h3>
    </div>
</div>  
</div>
</body>");
GeneratePDF(sb.ToString(),css.ToString());
        }

【问题讨论】:

    标签: c# itext pdf-generation xmlworker


    【解决方案1】:

    这是旧版本 iText for .NET 的一个已知问题。

    两年多以前,我们放弃了 iTextSharp 这个名称,转而使用 iText for .NET 这个名称。

    我已经获取了你的 HTML:

    <html>
    <head>
    <style type='text/css'>
    
            body{font-family: Segoe, 'Segoe UI', 'DejaVu Sans', 'Trebuchet MS', Verdana, 'sans-serif'; background: #f2f2f2; margin: 0; padding: 0; font-size: 20px; color: #565656;}
            .outerpdfboxCont{width: 100%; background: #fff; margin: 0 auto; padding: 15px 30px; max-width: 80%; display: table;box-shadow: 1px 1px 2px #ddd;}   
    
            .blueBorderLine{background: #0A82B4; height: 10px; width: 100%; margin: 10px auto;}
    
            .workingWrapperboxPdf{margin: 10px 0; padding: 10px 0px; display: table;width: 100%; }
            .workingWrapperboxPdf h2{font-size: 36px; font-weight: 500; margin: 40px 0 0; line-height: 40px; color: #000;}
            .workingWrapperboxPdf h3{font-size: 18px; font-weight: 300;line-height: 20px;}
    
            .darkB{color: #000!important; font-weight: 500!important;}
            .DataboxH{margin: 20px 0; display: table;width: 100%;}
            .border-heading{border-bottom: 1px solid #ddd; font-size: 30px!important; line-height: 35px!important; color: #000!important;width: 100%; padding-bottom: 10px;}
    
        </style>
    </head>
    <body>
    <div class='outerpdfboxCont'>   
        <div class='blueBorderLine'></div>  
    
    <div class='workingWrapperboxPdf'>  
    
        <h3>Dalvir Singh</h3>   
        <h3 class='darkB'>India</h3>
    
        <div class='DataboxH'>
            <h2>Summery Of the PDF</h2>
            <h3 class='darkB'>Summery of pdf will go here</h3>
            <h3>PDF created on 2018-6-12</h3>
        </div>
    </div>  
    </div>
    </body>
    </html>
    

    我已经在浏览器中打开了它:

    然后我拿了iText 7 和pdfHTML add-on。

    我执行了以下代码行

    HtmlConverter.ConvertToPdf(src, dest);
    

    在这一行中,src 指的是 HTML 文件,dest 指的是将基于 HTML 创建的 PDF。

    生成的 PDF 如下所示:

    如您所见,风格受到尊重。关于字体系列:您可以通过创建ConverterProperties 对象来获得具有更高偏好的字体之一,该对象使转换器可以访问字体存储库。有关该功能的更多信息,请阅读chapter 6 of the PDF to HTML tutorial。

    总结:一旦您升级到 iText 7 + pdfHTML 插件,您的问题就会消失。您正在使用旧的 XMLWorkerHelper,如 the introduction of the HTML to PDF tutorial 中所述,不再受支持。

    【讨论】:

    • 谢谢,您能否提供 C# 代码以从包含 css 的 html 字符串生成 pdf?
    • @DalvirSaini 如果src是一个包含CSS的字符串,而dest是一个流(例如MemoryStream),那么你只需要一行代码:HtmlConverter.ConvertToPdf(src, dest);跨度>
    • @DalvirSaini 这是一个与 HTML 到 PDF 转换完全无关的单独问题。请将其作为单独的问题发布。
    猜你喜欢
    • 2013-08-10
    • 2012-08-18
    • 2017-10-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-05-19
    • 2016-11-14
    相关资源
    最近更新 更多