Skip to content

Instantly share code, notes, and snippets.

@drilla
Last active August 20, 2020 13:30
Show Gist options
  • Save drilla/19df4d7402aa7cc289e4352d9d3875f7 to your computer and use it in GitHub Desktop.
Save drilla/19df4d7402aa7cc289e4352d9d3875f7 to your computer and use it in GitHub Desktop.
Using ELIXIR,
I need to print pdf from html "AS IS". with all background CSS images included an so on
- use https://github.com/gutschilla/elixir-pdf-generator
- use head-listed css styles
- use chromium render
in your docker image you have to install
- nodejs
- puppeteer
- chrome-headless pdf renderer
- chromium (in case you app says "cannot find chrome executable" and stuff)
- you must use in <body> style for correct background-image representation
because default media = print, and it has a limitations
body {
-webkit-print-color-adjust: exact;
}
- you can use also png and svg images
add to your dockerfile
#node js
RUN apt-get install -y curl bash ca-certificates openssl coreutils python2 make gcc g++ grep util-linux binutils findutils && apt-get update
ENV NVM_DIR /root/.nvm
# lts release
ENV NODE_VERSION 12.18.3
#very important!!
ENV npm_config_user=root
#install puppeteer and headless
RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash \
&& . $NVM_DIR/nvm.sh \
&& nvm install $NODE_VERSION \
&& nvm alias default $NODE_VERSION \
&& nvm use default \
&& npm -g install chrome-headless-render-pdf puppeteer
# for puppeteer. doesnt work without it, i dont know why
RUN apt-get install -y chromium
in case you want to use WKHTMLTOPDF (chrome is a way better)
- do not use complex "background" url() + top + left and so on
use separate background-image: ... and background-position: ...
- use command line options
--encoding UTF-8
--enable-local-file-access
--javascript-delay 3000 (in case you have js)
--keep-relative-links (can be useful)
--orientation landscape
add to your Dockerfile
#you can try to use newer release of wkhtml
RUN curl -L https://github.com/wkhtmltopdf/wkhtmltopdf/releases/download/0.12.4/wkhtmltox-0.12.4_linux-generic-amd64.tar.xz | tar -xJ
RUN cp wkhtmltox/bin/wk* /usr/local/bin/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment