Search code examples
itext7

Flexbox Usage Partially(?) Works In iTextPDF


I am using ITextPDF to create a report. And since the report is going to be a very complex one, I want to use an HTML template which I am creating to create the pdf. But when it comes to flex-box, it only partially works.

What I mean with partially is, when I use below code;

private string HTML = $@"
<!DOCTYPE html>
<html lang=""tr"">
<head>
    <meta charset = ""UTF-8"">
    <meta http-equiv=""X-UA-Compatible"" content=""IE=edge"">
    <meta name = ""viewport"" content=""width=device-width, initial-scale=1.0"">
    <title>Print</title>
    <link rel=""stylesheet"" href=""https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css"">
</head>
<body>
    <div class=""d-flex align-items-center justify-content-center bg-primary"">
        <div>abcçde</div>
        <div>ABCÇDEF</div>
    </div>
</body>
</html>
";
public void GeneratePDF()
{
    using(var memoryStream = new MemoryStream())
    {
        using (var pdfWriter = new PdfWriter(Guid.NewGuid().ToString()+".pdf"))
        {
            pdfWriter.SetCloseStream(false);
            HtmlConverter.ConvertToPdf(this.HTML, pdfWriter);
            var pdfStream = pdfWriter.GetOutputStream();
            pdfStream.Position = 0;
            pdfStream.CopyTo(memoryStream);
            var result = memoryStream.ToArray();
            pdfStream.Dispose();
        }
    }
}

The justify-content-center works on-point as can be seen below; enter image description here

But when I change it to justify-content-between it doesn't work; enter image description here

I also tried to give each div float values, without adding bootstrap.min.css and it doesn't work that way either.

Why would it work with justify-content-center and not justify-content-between?


Solution

  • As mentioned in one of the question's comments, as for now iText provides only initial flex support. Specifying justify-content-between class actualy means setting justify-content:space-between, and thie value is not supported: you could see it from the log message which is thrown while processing such an html:

    com.itextpdf.html2pdf.css.apply.util.FlexApplierUtil WARN Flex related property justify-content: space-between is not supported yet.
    

    As a workaround, one indeed could use floats. For example, divs of the following html are processed as expected:

    <!DOCTYPE html>
    <html lang="tr">
     <body>
      <div>
       <div style="float: left;">abcçde</div>
       <div style="float: right">ABCÇDEF</div>
      </div>
     </body>
    </html>
    

    enter image description here