Generating files in bulk screws up the layout

106 views
Skip to first unread message

Christian Vermeulen

unread,
Mar 13, 2021, 5:55:36 AM3/13/21
to wkhtmltopdf General
Hi all,

I really hope I can find some guidance here. We have implemented wkhtmltopdf in our Symfony project using KnpSappy. Everything works fine and documents are generated beautifully. But only as long as we generate just 1 per request.

Due to updates in out layout, I created a command to re-generate our documents. In this case the layout is all screwed up, almost like the css is not loaded correctly. I have validated it is specifically when generating multiple files (and writing to disk) because when I just do 1 document on CLI the layout is fine. Then i do 10 documents in loop on CLI and the layout is screwed.

What I tried so far:
- Add all css inline in the head
- Add up to 5 second timeouts between each generate
- Looked through Google, StackOverflow, Github Issues (for both Snappy and wkhtmltopdf)

Our version is 0.12.5 incl. QT patch.

Hopefully someone out there has any idea what is causing this..

Kind regards,
Christian Vermeulen

Christian Vermeulen

unread,
Mar 13, 2021, 6:00:39 AM3/13/21
to wkhtmltopdf General
I've also just realised, only the lást document in the loop is generated correctly. All the others are not...

George Reynolds

unread,
Mar 13, 2021, 6:12:30 AM3/13/21
to wkhtmltop...@googlegroups.com
hi

Can you share your code? I can have a quick look at it.

Can you echo or print out the exact wkhtmltopdf command each time it iterates to see if anything inconsistent? Also just try one of those  commands on it's own.


Note we're just the community here to help and not officially from wkhtmltopdf


--
===================================================
You received this message because you are subscribed to the "wkhtmltopdf General" group.
To post to this group, send email to wkhtmltop...@googlegroups.com
To unsubscribe from this group, send email to wkhtmltopdf-gen...@googlegroups.com
 
For more options, visit this group at
http://groups.google.com/group/wkhtmltopdf-general?hl=en?hl=en
---
You received this message because you are subscribed to the Google Groups "wkhtmltopdf General" group.
To unsubscribe from this group and stop receiving emails from it, send an email to wkhtmltopdf-gen...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/wkhtmltopdf-general/a568d23f-c725-442c-8559-91b12fc18c5an%40googlegroups.com.

Christian Vermeulen

unread,
Mar 13, 2021, 6:46:06 AM3/13/21
to wkhtmltop...@googlegroups.com, George Reynolds
Hi Georgy,

I have dumped the resulting command in my loop, which is as follows:

^ "/bin/wkhtmltopdf --lowquality --margin-bottom '28mm' --margin-left '12.7mm' --margin-right '12.7mm' --margin-top '108mm' --page-size 'A4' --footer-html '/tmp/knp_snappy604ca42537cad8.41582781.html' --header-font-size '9' --header-html '/tmp/knp_snappy604ca425381664.17807617.html' --header-spacing '35' '/tmp/knp_snappy604ca425376a93.90581590.html' '/tmp/knp_snappy604ca425379d27.84192902.pdf'"
^ "/bin/wkhtmltopdf --lowquality --margin-bottom '28mm' --margin-left '12.7mm' --margin-right '12.7mm' --margin-top '108mm' --page-size 'A4' --footer-html '/tmp/knp_snappy604ca4278bbf83.36217007.html' --header-font-size '9' --header-html '/tmp/knp_snappy604ca4278c01f7.01412639.html' --header-spacing '35' '/tmp/knp_snappy604ca4278b7080.67871534.html' '/tmp/knp_snappy604ca4278b7804.67510287.pdf'"
^ "/bin/wkhtmltopdf --lowquality --margin-bottom '28mm' --margin-left '12.7mm' --margin-right '12.7mm' --margin-top '108mm' --page-size 'A4' --footer-html '/tmp/knp_snappy604ca429d13ff6.49480072.html' --header-font-size '9' --header-html '/tmp/knp_snappy604ca429d16b02.79007078.html' --header-spacing '35' '/tmp/knp_snappy604ca429d10551.15502426.html' '/tmp/knp_snappy604ca429d10c60.64803963.pdf'"
^ "/bin/wkhtmltopdf --lowquality --margin-bottom '28mm' --margin-left '12.7mm' --margin-right '12.7mm' --margin-top '108mm' --page-size 'A4' --footer-html '/tmp/knp_snappy604ca42bf0f520.81006821.html' --header-font-size '9' --header-html '/tmp/knp_snappy604ca42bf121c0.58055908.html' --header-spacing '35' '/tmp/knp_snappy604ca42bf0b1a4.97984239.html' '/tmp/knp_snappy604ca42bf0b9d6.69117414.pdf'"
^ "/bin/wkhtmltopdf --lowquality --margin-bottom '28mm' --margin-left '12.7mm' --margin-right '12.7mm' --margin-top '108mm' --page-size 'A4' --footer-html '/tmp/knp_snappy604ca42e35e5d2.87116495.html' --header-font-size '9' --header-html '/tmp/knp_snappy604ca42e362745.68628164.html' --header-spacing '35' '/tmp/knp_snappy604ca42e3591d8.37035366.html' '/tmp/knp_snappy604ca42e359e44.86383899.pdf'"
^ "/bin/wkhtmltopdf --lowquality --margin-bottom '28mm' --margin-left '12.7mm' --margin-right '12.7mm' --margin-top '108mm' --page-size 'A4' --footer-html '/tmp/knp_snappy604ca4307233c7.92844580.html' --header-font-size '9' --header-html '/tmp/knp_snappy604ca430726ac1.01641554.html' --header-spacing '35' '/tmp/knp_snappy604ca43071f457.12291005.html' '/tmp/knp_snappy604ca43071fc24.67774604.pdf’"

This looks pretty ok to me. For a second I thought maybe the temple was the issue, but I see the tempfiles have specific hash, so they can not really interfere with one-another.

BTW: I’ve also realised, it is only the FIRST document which is generated correctly, everything after that isn’t.

Here is a code snippet of the actual pdf part:
$fileName = sprintf('%s.pdf', (string) $document->getDocumentNumber());

$cssFiles = $this->encore->getEntrypointLookup('_default')->getCssFiles('pdf');
$css = '';
foreach ($cssFiles as $cssFile) {
$css .= file_get_contents($this->projectDir.'/public'.$cssFile);
}

$html = $this->twig->render('sales/pdf/index.html.twig', ['document' => $document, 'css' => $css]);
$header = $this->twig->render('sales/pdf/header.html.twig', ['document' => $document, 'css' => $css]);
$footer = $this->twig->render('snippets/pdf-footer.html.twig', ['document' => $document, 'css' => $css]);


$pdfOptions = [
'page-size' => 'A4',
'header-html' => $header,
'header-font-size' => 9,
'header-spacing' => 35,
'footer-html' => $footer,
'margin-top' => '108mm',
'margin-bottom' => '28mm',
'margin-left' => '12.7mm',
'margin-right' => '12.7mm',
];

$pdf = $this->pdf->getOutputFromHtml($html, $pdfOptions);

if ($save === true) {
$this->pathBuilder->setCompanyId((string) $document->getCompanyId());
$this->uploadFile->saveNewFile($this->pathBuilder, $document->getType(), $fileName, $pdf);
}
I have a console command which retrieves all documents from the database and calls this code for every document in order to generate all the pdf’s. As for the css, I get the css files from Encore and then get their contents to build up a css string which is added in a <style> tag in the <head> of the html files.

I have also attached a correct (OF-20-0019), and incorrect (OF20-0018) file so you can see the difference. Considering the fonts are still generated I’m not sure CSS is the problem here.

Thanks for checking this out!

Gr. Chris
You received this message because you are subscribed to a topic in the Google Groups "wkhtmltopdf General" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/wkhtmltopdf-general/S2N-jZyVsQU/unsubscribe.
To unsubscribe from this group and all its topics, send an email to wkhtmltopdf-gen...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/wkhtmltopdf-general/CABXUiUSSKOKQrVnMMj7pkaJGqF-OHgOphuTGV-de-jKXugieBQ%40mail.gmail.com.
OF20-0018.pdf
OF20-0019.pdf

Christian Vermeulen

unread,
Mar 25, 2021, 3:36:36 AM3/25/21
to wkhtmltopdf General
So, we have found the problem.. It was not with WKHTMLTOPDF but with Symfony Encore... They have some unexpected behavior when including assets.
It turns out, once you get the array of css files it only returns every resource once per request! Because of this, we only got back the css for the first PDF document.
For the second PDF document this would not return any css anymore, because it "had already been loaded".

So the fix was to add a reset to the encore cache.

Thanks for the help anyway! Much appreciated.

Kind regards,
Chris

George Reynolds

unread,
Mar 25, 2021, 4:52:43 AM3/25/21
to wkhtmltop...@googlegroups.com
ah so I was sort of right about something being cached but the problem wasn't with wkhtmltopdf... good stuff 



Reply all
Reply to author
Forward
0 new messages