Skip to content

Instantly share code, notes, and snippets.

@akihiromukae
Last active August 29, 2020 05:47
Show Gist options
  • Save akihiromukae/288b163d538d45a197b3f1b54ef385e8 to your computer and use it in GitHub Desktop.
Save akihiromukae/288b163d538d45a197b3f1b54ef385e8 to your computer and use it in GitHub Desktop.
wkhtmltopdfをAlpine linuxに入れてPDF出力する
まずはここのDockerfileを参考にするだけだとxvfbが足りなくて動かないので16行目に追加し、
https://github.com/LoicMahieu/alpine-wkhtmltopdf/blob/master/Dockerfile
以下のものも参考にdbusはdbusパッケージにあるdbus-genuuidを実行しておかないと、xvfb実行時にエラーになるので35行目に追加。
http://kernhack.hatenablog.com/entry/2016/05/28/101626
日本語ならIPAFontも入れると良いが、ググればすぐにでてくるので割愛。
これだけだと、CSSが効かない状態でPDF出力されるので別途対応が必要。ヘッドレスブラウザが必要?
FROM alpine:3.3
RUN apk add --no-cache \
xvfb \
# Additionnal dependencies for better rendering
ttf-freefont \
fontconfig \
dbus \
xvfb \
&& \
# Install wkhtmltopdf from `testing` repository
apk add qt5-qtbase-dev \
wkhtmltopdf \
--no-cache \
--repository http://dl-3.alpinelinux.org/alpine/edge/testing/ \
--allow-untrusted \
&& \
# Wrapper for xvfb
mv /usr/bin/wkhtmltopdf /usr/bin/wkhtmltopdf-origin && \
echo $'#!/usr/bin/env sh\n\
Xvfb :0 -screen 0 1024x768x24 -ac +extension GLX +render -noreset & \n\
DISPLAY=:0.0 wkhtmltopdf-origin $@ \n\
killall Xvfb\
' > /usr/bin/wkhtmltopdf && \
chmod +x /usr/bin/wkhtmltopdf \
dbus-genuuid
@akihiromukae
Copy link
Author

akihiromukae commented Jan 18, 2018

# IPA font
RUN cd && wget https://oscdl.ipa.go.jp/IPAfont/ipag00303.zip \
    && unzip ipag00303.zip \
    && mkdir -p /usr/share/fonts/ipa \
    && cp ipag00303/ipag.ttf /usr/share/fonts/ipa \
    && fc-cache -fv

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment