Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flexbox wkhtmltopdf Rendering Issue

I have a page that I am turning into a pdf with wkhtmltopdf. I have a 3 column layout and it works well in Chrome but the pdf is incorrectly generated. Is there an alternative to flexbox that would give the same view or a way to make flexbox work in wkhtmltopdf? Modernizr did not help. Thanks.

HTML:

<div class="header">
  <div id="name" class="center">
    <h2>
      Centered Text
    </h2>
  </div>
  <div id="links" class="left">
    <h3>
    Left Line 1
    <br>
    Left Line 2
    </h3>
  </div>
  <div id="contact" class="right">
    <h3>
    Right Line 1
    <br>
    Right Line 2
    </h3>
  </div>
</div>
</div class="clear"></div>

CSS:

.header {
  margin-top: 0;
  margin-bottom: 2px;
  display: flex;
  justify-content: space-between;
}

.center {
  order: 2;
  text-align: center;
}

.left {
  order: 1;
  float: left;
  text-align: left;
}

.right {
  order: 3;
  float: right;
  text-align: right;
}

.clear:before,
.clear:after {
  flex-basis: 0;
  order: 1;
}

.clear {
  clear: both;
}
like image 845
Alec Fenichel Avatar asked Nov 18 '15 16:11

Alec Fenichel


People also ask

Does wkhtmltopdf support flexbox?

Flexbox seems to be broken in wkhtmltopdf. Here's a test case, which renders correctly in current versions of Chrome, Firefox, and Internet Explorer. wkhtmltopdf renders it as if the flex properties were not present. Tested with the 0.12 Windows 32-bit download as found on http://wkhtmltopdf.org/ (wkhtmltox-win32_0.

Does Dompdf support Flex?

Dompdf does not support flexbox as of this writing (current release is 0.8. 4). Refer to issue 971 for more information. You can generate that layout with a variety of stylings, but if your content goes beyond a page in length you'll run into some quirkiness around the Dompdf rendering process.


2 Answers

wkhtmltopdf uses an old version of WebKit and so you must use the original Flexbox spec, which is nowhere near as powerful but can still do some of the same effects.

Note, also, that you'll have to prefix lots of properties with -webkit-.

like image 130
TALlama Avatar answered Oct 14 '22 04:10

TALlama


This specific comment in that issue brings a solution that worked for me: https://github.com/wkhtmltopdf/wkhtmltopdf/issues/1522#issuecomment-848651693

"to make work flex on wkhtmltopdf you need to use display: -webkit-box; for flex and -webkit-box-pack: justify; for justify-content: space-between;. So we should use old flex syntax"

like image 4
Kleber NG Avatar answered Oct 14 '22 04:10

Kleber NG