Fork me on GitHub
2016-11-24

Using wkhtmltopdf in Docker with Gitlab CI

Running wkhtmltopdf in a headless CI environment

When running a build with Gitlab CI, normally there is no X Server running in the container under test. Since we wanted to use the wkhtmltopdf binary in conjunction with a PHP application and there were tests for the PDF generation, we came up with the following solution:

  1. install a current version of wkhtmltopdf from their downloads page
  2. install xvfb, which is a virtual framebuffer as a replacement for the X-Server
  3. use a wrapper to run wkhtmltopdf through xvfb

Here are the relevant parts of the .gitlab-ci.yml file:

image: cedricziel/docker-php-tools:7.0

variables:
  SYMFONY_ENV: test
  WKHTMLTOPDF_BIN: /usr/local/bin/wkhtmltopdf-wrapper

before_script:
  - apt-get purge -y wkhtmltopdf && apt-get update 
  - apt-get install -y build-essential xorg libssl-dev libxrender-dev wget gdebi xvfb
  - wget https://bitbucket.org/wkhtmltopdf/wkhtmltopdf/downloads/wkhtmltox-0.13.0-alpha-7b36694_linux-jessie-amd64.deb
  - gdebi --n wkhtmltox-0.13.0-alpha-7b36694_linux-jessie-amd64.deb
  - echo "xvfb-run -a -s \"-screen 0 640x480x16\" wkhtmltopdf \"\$@\"" > /usr/local/bin/wkhtmltopdf-wrapper && chmod +x /usr/local/bin/wkhtmltopdf-wrapper

The before_script purges any existing package with the name wkhtmltopdf before getting the dependencies and installing a current version from the a downloaded package source.

No Comments