Chromiumのheadless環境でPDFがうまく出力されなくなって困った

最近のスライドは Asciidoc で書いて,Reveal.js のhtml に変換して発表,そのhtml をChrome のheadless モードでPDF に変換していました.
こけれを行っていた端末を変更したせいかこれがうまくいかなくなりました.

引数はこんな感じで

               chromium --user-data-dir=$TMPDIR --headless --disable-gpu --print-to-pdf="${PDF}" "$URL?print-pdf"; \

出力されるPDFはCSSが足りないようなものや,真っ白なものになります.

revealjs2pdf01
revealjs2pdf02

make から呼んでいるので手動でコマンドラインから実行しても同じ, --headless オプションを削って手動で Ctrl + p でPDF 出力すれば問題なく出力されます.

Firefox にはPDF 出力オプションはないのですが画像出力ができるので画像書き出しをしてみると求める結果になりました.

$ firefox --screenshot /tmp/page.png --window-size=1280 "file:///`pwd`/slide.html#print-pdf"

各ページをこれで画像に出力して結合してPDFにすることも可能ですが,文字情報が消えてしまいます…….

wkhtmltopdf も試すと以下のようなコマンドで近いものが出来ますがサイズや余白,フォントが違います.もう少し頑張れば良く出来そう?

$ wkhtmltopdf --enable-local-file-access --orientation Landscape --page-size B5 --margin-top 0 --margin-bottom 0 --margin-right 0 --margin-left 0 "file://`pwd`/slide/slide.html?print-pdf" /tmp/slide.pdf

検索して以下のページを見つけました.

こちらを参考にChromium に --run-all-compositor-stages-before-draw--virtual-time-budget を指定するようにして書き出せるようになりました. --virtual-time-budget の値については環境により変える必要があるかもしれません.

$ chromium --headless --run-all-compositor-stages-before-draw --virtual-time-budget=9999999 --print-to-pdf=/tmp/slide.pdf file://`pwd`/slide/slide.html?print-pdf

Makefile は以下のように変更.

-               chromium --user-data-dir=$TMPDIR --headless --disable-gpu --print-to-pdf="${PDF}" "$URL?print-pdf"; \
+               chromium --user-data-dir=$TMPDIR --headless --run-all-compositor-stages-before-draw --virtual-time-budget=999999 --print-to-pdf="${PDF}" "$URL?print-pdf"; \

書き出せるようになりました :)

環境
$ dpkg-query -W chromium firefox-esr wkhtmltopdf
chromium        107.0.5304.110-2
firefox-esr     102.5.0esr-1
wkhtmltopdf     0.12.6-2+b1
$ lsb_release -dc
No LSB modules are available.
Description:    Debian GNU/Linux bookworm/sid
Codename:       bookworm
$ arch
x86_64

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

To respond on your own website, enter the URL of your response which should contain a link to this post's permalink URL. Your response will then appear (possibly after moderation) on this page. Want to update or remove your response? Update or delete your post and re-enter your post's URL again. (Find out more about Webmentions.)