CSS3 multi-column layout converting to PDF using snappy (wkhtmltopdf wrapper)
Asked Answered
A

4

6

I'm trying to generate 3 column PDF (using barryvdh/laravel-snappy - wkhtmltopdf). Because the text which is going to fill these columns is of unknown length I have to use some mechanism allowing the text fill the columns freely. So I tried to use this CSS:

.threecol {
            -webkit-column-count: 3; /* Chrome, Safari, Opera */
            -moz-column-count: 3; /* Firefox */
            column-count: 3;
        }

This works great on a web page but I can't convert it to PDF. It doesn't show any errors. It simply converts the page with one column. Any suggestions? Is what I want possible to do with wkhtmltopdf? Or maybe should I use some other library?

Afterguard answered 1/5, 2016 at 16:4 Comment(8)
I was also trying TCPDF which can interpret HTML with method WriteHTML(). But it doesn't work too...Afterguard
What does "can't convert" mean? Is wkhtmltopdf throwing an error, or isn't the text distributed between the columns?Legation
"can convert" means that the PDF doesn't have 3 columns but only one like the css was not there. No error is generated.Afterguard
Have you tried giving it a fixed width, to avoid sizing problems with the viewport?Legation
No I haven't. But it just looks like the column thing was ignored. All the text is there and everything seems to be OK except the text is in one not 3 columns. The question is whether wkhtmltopdf can do it but I do something wrong or it's just impossible. And maybe there is any other way to do this what I need.Afterguard
Did you sort this out? I have tried to convert a bootstrap 4 column grid to pdf using wkhtmltopdf, and mine has just output in 1 column as wellFibril
Any luck ever since?Luiseluiza
Nothing unfortunately :(Afterguard
O
3

This feature isn't available in the Webkit version used in wkhtmltopdf. Looks like we have to use tables, with manual flow.

Onus answered 5/1, 2018 at 13:54 Comment(1)
Can you show what a table with manual flow would look like (in the code)?Pollaiuolo
C
2

I'm two years late but hopefully this is helpful for someone. After a couple days searching the net and trying to get a multi-column / multi-page pdf going with Wkhtmltopdf this is what I came up with... works great and I haven't seen the solution around. It requires each column having a set width and height. The columns are cloned, it's contents position are offset to show the next span of text. Repeat until the entirety of the content has been displayed.

https://jsfiddle.net/pa2k6wj5/1/

HTML

<section class="parent">
    <div class="column">
        <div class="content">
            (longform text goes here...)
        </div>
    </div>
</section>

CSS

.column {
    position: relative;
    width: 3in;
    height: 5in;
    display: inline-block;
    overflow: hidden;
    vertical-align: top;
}
.content {
    width: 3in;
}

JS

// get elements
var parent = document.querySelector('.parent');
var column = document.querySelector('.column');
var content = document.querySelector('.content');

// calculate height values of column and it's content
var columnHeight = column.offsetHeight;
var contentHeight = content.offsetHeight;

// create an array of offset values
var offsetValues = [];
for (var i = columnHeight; i < contentHeight; i+= columnHeight) {
    offsetValues.push(i);
}

// create a new column for each offset value
offsetValues.forEach( function(offsetValue, i) {

    // init clone and add classes
    var cloneColumn = document.createElement('div');
    var cloneContent = document.createElement('div');
    cloneColumn.classList.add('column');
    cloneContent.classList.add('content');

    // populate the DOM
    cloneContent.innerHTML = content.innerHTML;
    cloneColumn.appendChild(cloneContent);
    parent.appendChild(cloneColumn); 

    // apply position and offset styles
    cloneContent.style.position = 'relative';
    cloneContent.style.top = '-' + offsetValue + 'px';
});
Capitation answered 10/7, 2019 at 18:12 Comment(0)
S
0

try this brother.. :)

answer from docs: https://github.com/wkhtmltopdf/wkhtmltopdf/issues/1872#issuecomment-50742814

/* docs: https://github.com/wkhtmltopdf/wkhtmltopdf/issues/1872#issuecomment-50742814 */
.container-two-row {
  /* You *must* define a fixed height which is
     large enough to fit the whole content,
     otherwise the layout is unpredictable. */
  height: 28em;
  /* Width and count aren't respected, but you
     have to give at least some dummy value (??). */
  -webkit-columns: 0 0;
  /* This is the strange way to define the number of columns:
     50% = 2 columns, 33% = 3 columns 25% = 4 columns */
  width: 50%; 
  /* Gap and rule do work. */
  -webkit-column-gap: 1px;
  -webkit-column-rule: 1px solid black;
  text-align: left;
}
.height-35em{
  height: 35em;
}
.mt-15{
  margin-top: 15px;
}
 <div class="container-two-row height-35em mt-15">
            <strong>An Article</strong>
            <div>
            1. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
            tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
            quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
            consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
            cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
            proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
            </div>
            <div>
            2. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
            tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
            quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
            consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
            cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
            proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
            </div>
            <div>
            3. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
            tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
            quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
            consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
            cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
            proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
            </div>
            <div>
            4. Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod
            tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam,
            quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo
            consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse
            cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non
            proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
            </div>
        </div>
Sessile answered 13/6, 2022 at 6:46 Comment(0)
C
-1

I don't have any problem to generate a PDF with multicolumns with Snappy.

In your class, try to add a width property like this :

.threecol {
        -webkit-column-count: 3; /* Chrome, Safari, Opera */
        -moz-column-count: 3; /* Firefox */
        column-count: 3;
        /* This is the strange way to define the number of columns:
        50% = 2 columns, 33% = 3 columns 25% = 4 columns */
        width: 33%;
    }
Convergent answered 5/9, 2018 at 13:35 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.