Search code examples
javaflexboxpdf-generationflying-sauceropenpdf

FlyingSaucer with openpdf doesn't render flex box correctly


I want to render a PDF document using latest FLyingSaucer

build.gradle:

implementation 'org.xhtmlrenderer:flying-saucer-pdf-openpdf:9.1.22'

Input HTML document (almost a copy from W3 schools):

<!DOCTYPE html>
<html>
<head>
<style>
.flex-container {
  display: flex;
  background-color: DodgerBlue;
}

.flex-container > div {
  background-color: #f1f1f1;
  margin: 10px;
  padding: 20px;
  font-size: 30px;
}
</style>
</head>
<body>

<h1>Create a Flex Container</h1>

<div class="flex-container">
  <div>1</div>
  <div>2</div>
  <div>3</div>  
</div>

</body>
</html>

A sample code (as JUnit test):

class FlexTest {

  @Test
  void flexTest() throws FileNotFoundException, IOException {

    final String htmlContent =
        IOUtils.toString(new FileInputStream("input.html"), Charset.defaultCharset());

    ITextRenderer renderer = new ITextRenderer();
    renderer.setDocumentFromString(htmlContent);
    renderer.layout();
    renderer.createPDF(new FileOutputStream("output.pdf"));
  }
}

It works fine except the display: flex is not rendered correctly. It looks like it's rendered as a regular <div>:

enter image description here

Is the flex box supported by FlyingSaucer/openpdf? I didn't find any note that it isn't.


Solution

  • Flying-saucer doesn't support Flex, and will likely never support it.

    The CSS supported by flying-saucer is limited to CSS 2.1 and most of CSS paged media.