样式未使用ITextSharp在PDF中实现[复制]

问题描述 投票:-1回答:1

这个问题在这里已有答案:

我正在尝试使用ITextSharp从HTML字符串生成PDF。但外观样式没有得到应用。我在stackoverflow上查看与它相关的帖子,但没有得到任何帮助。 Plz帮助我如何使用HTML UI中显示的实际样式转换pdf中的HTML。 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个回答
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>

我在浏览器中打开它:

enter image description here

然后我采取了iText 7pdfHTML add-on

我执行了以下代码行

HtmlConverter.ConvertToPdf(src, dest);

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

这就是生成的PDF的样子:

enter image description here

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

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

© www.soinside.com 2019 - 2024. All rights reserved.