decktape icon indicating copy to clipboard operation
decktape copied to clipboard

Inconsistent image size between pdf and screenshots when viewport-relative sizes are used

Open DanySK opened this issue 5 years ago • 9 comments

I'm trying to correctly render in pdf this slide:

guide_10_1440x900

(snapshot generated by decktape)

Its resulting code is:

<section class="present" style="top: 0px; display: block;">
<h2 id="hi-res-default">Hi res, default</h2>
<img src="4k.jpeg" style="
    
    
    max-width: 95vw;
    max-height: 80vh;
    object-fit: contain;
">
</section>

When rendering in pdf, however, the vw/vh units seem to be broken and relative to something different than the page size, and images get rendered much smaller than they should be.

Here is the generated slide pdf.

DanySK avatar Aug 25 '20 11:08 DanySK

What presentation framework do you use? Could you try by increasing the viewport size with the --size option, e.g. --size='2048x1536' ?

astefanutti avatar Aug 25 '20 12:08 astefanutti

I'm using reveal.js. Yes I tried to change the viewport. It scales images, but font sizes as well, and text-wise the result is fine, what's in the browser gets reproduced on the pdf correctly. What I'd like is that what I see in the viewport in chrom(e/ium) is the same of what I see in a same-sized viewport when producing a pdf. Snapshots get generated correctly.

If it can be useful for reproducing the bug I can open the private repository where the issue appears (I currently worked it around by regex-matching my code and enlarging max-width and max-height when generating the pdf, but it's less than suboptimal.

DanySK avatar Sep 01 '20 15:09 DanySK

That seems like #151. Unfortunately, this is due to rendering discrepancies between Chromium Web engine and the PDF export component.

astefanutti avatar Sep 01 '20 17:09 astefanutti

So I guess this is something upstream has to fix. Could you open a bug report to them? I'll :+1: it immediately.

DanySK avatar Sep 01 '20 17:09 DanySK

A bug report has been open since 2017: https://github.com/puppeteer/puppeteer/issues/915

jonathan-g avatar Sep 01 '20 17:09 jonathan-g

ARGH, it's open since sooo long... And they tagged it as "Chromium", so I wonder if it's something that should get opened directly on chromium: https://bugs.chromium.org/p/chromium/issues/list.

DanySK avatar Sep 01 '20 18:09 DanySK

I don't know enough about the internals of Chromium to know how to make a good bug report for this.

But the good news is that there's a workaround: Chrome and Chromium seem to do fine at rendering reveal.js slides to pdf from a terminal command prompt, with none of the problems I see with puppeteer and decktape.

Basically you want to serve your slides to localhost:8000 and then execute (you can play around with whether you want to disable the GPU and how large you want to set the maximum time budget for rendering):

chrome --headless --run-all-compositor-stages-before-draw --disable-gpu --virtual-time-budget=10000 --print-to-pdf=my_slides.pdf http://localhost:8000?print-pdf

And it's easy enough to wrap this command line in a script to make things less cumbersome.

jonathan-g avatar Sep 01 '20 22:09 jonathan-g

@jonathan-g man you really saved my day. I enabled a double generation of my slides in CI, both via decktape and via chromium headless. They seem to work nicely.

DanySK avatar Sep 07 '20 08:09 DanySK

So happy I could help.

jonathan-g avatar Sep 07 '20 15:09 jonathan-g