1

I am trying to use ITextSharp to generate PDF from HTML string. But looks styles are not getting applied. I looks at the posts related to it on stackoverflow but did not get any help from them. Plz help me how to convert HTML in pdf with the actual styles that shows in HTML UI. CSS included in style tag. Here is my C# code

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;
        }

Here is code with HTMl string and 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());
        }
0

1 Answer 1

2

This is a known problem with old versions of iText for .NET.

Over two years ago, we abandoned the name iTextSharp in favor of the name iText for .NET.

I've taken your 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>

An I've opened it in a browser:

enter image description here

Then I've taken iText 7 and the pdfHTML add-on.

I executed the following line of code

HtmlConverter.ConvertToPdf(src, dest);

In this line src refers to the HTML file, and dest refers to the PDF that will be created based on the HTML.

This is what the resulting PDF looks like:

enter image description here

As you can see, the styles are respected. Regarding the font family: you can get one of the fonts with a higher preference by creating a ConverterProperties object that gives the converter access to a font repository. For more info about that feature, please read chapter 6 of the PDF to HTML tutorial.

Summarized: your problem will go away once you upgrade to iText 7 + the pdfHTML add-on. You are using the old XMLWorkerHelper that, as explained in the introduction of the HTML to PDF tutorial, is no longer supported.

3
  • Thanks, Could you provide C# code to generate pdf from html string that includes css? Sep 11, 2018 at 10:43
  • @DalvirSaini If src is a string that includes CSS, and dest is a stream (e.g. a MemoryStream), then you only need one line of code: HtmlConverter.ConvertToPdf(src, dest); Sep 11, 2018 at 12:13
  • @DalvirSaini That's a separate question completely unrelated to HTML to PDF conversion. Please post it as a separate question. Sep 12, 2018 at 5:42

Not the answer you're looking for? Browse other questions tagged or ask your own question.