Giter Site home page Giter Site logo

docker-wkhtmltopdf's Introduction

// 2020-08-31: Merged PR from "bonashen" https://github.com/bonashen, adding example code for using options // 2019-10-02: Reverting back to python2 due to errors, no time for fixing these // 2019-09-02: Build using v0.12.5-1 from DEB-package, updated to Ubuntu 18.04, updated to python3 due to end-of-support python2 // 2018-03-08: Build using v0.12.4 from binary instead of 0.12.2 using apt-get // 2018-04-16: Moved to https://hub.docker.com/r/oberonamsterdam/wkhtmltopdf/, adding github repo for Dockerfile

Based on https://hub.docker.com/r/openlabs/docker-wkhtmltopdf-aas/

Changes:

  • Using Ubuntu 18.04 instead of 14.04
  • Adding XVFB/libfontconfig to correctly render external fonts
  • Updated WKHTMLTOPDF to 0.12.5-1
  • Updated to Python3

Running the container

This will create a service on port (only advised for testing-purpose)

docker run -d -p <port>:80 oberonamsterdam/wkhtmltopdf

On production it's advised to run container as a service and expose to your own containers only.

Using the webservice

There are multiple ways to generate a PDF of HTML using the service: uploading a HTML file

This is a convenient way to use the service from command line utilities like curl.

curl -X POST -vv -F 'file=@path/to/local/file.html' http://<docker-host>:<port>/ -o path/to/output/file.pdf

where:

docker-host is the hostname or address of the docker host running the container
port is the public port to which the container is bound to.

Example PHP code using options

	'margin-top' => '25',
	'margin-left' => '25',
	'margin-right' => '25',
	'margin-bottom' => '25',
	'page-size' => 'A4',
	'footer-spacing' => '5',
	'footer-font-size' => 6,
	'footer-right' => 'Page [page] of [topage]',
];

$html = file_gets_content('test.html');
$data = json_encode([
   'contents' => base64_encode($html),
   'options' => $options,
]);

$ch = curl_init();
curl_setopt_array($ch, [
   CURLOPT_URL => 'http://<docker-host>:<port>',
   CURLOPT_POST => true,
   CURLOPT_RETURNTRANSFER => true,
   CURLOPT_HTTPHEADER => ['Content-Type: application/json'],
   CURLOPT_POSTFIELDS => $data,
]);

$result = curl_exec($ch);

$result will contain the binary PDF output.

Using images in your HTML

  1. Use absolute URLs to your images and make sure these URLs are accessible for the wkhtmltopdf-container
  2. Mount your files into the wkhtmltopdf-container and use local images: https://stackoverflow.com/questions/16627310/wkhtmltopdf-not-loading-local-css-and-images

docker-wkhtmltopdf's People

Contributors

bonashen avatar helox avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

docker-wkhtmltopdf's Issues

Docker build error

Hi,
I try to do a docker build . but

Selecting previously unselected package wkhtmltox.
(Reading database ... 5985 files and directories currently installed.)
Preparing to unpack wkhtmltox_0.12.5-1.bionic_amd64.deb ...
Unpacking wkhtmltox (1:0.12.5-1.bionic) ...
dpkg: dependency problems prevent configuration of wkhtmltox:
wkhtmltox depends on libxrender1; however:
Package libxrender1 is not installed.
wkhtmltox depends on xfonts-base; however:
Package xfonts-base is not installed.

dpkg: error processing package wkhtmltox (--install):
dependency problems - leaving unconfigured
Errors were encountered while processing:
wkhtmltox

Have you got an idea ?
Thanks

Options replace array not converted correctly

mikehaertl/phpwkhtmltopdf#107

When adding an options array like:
$options = array( 'header-html' => $header_url, // new File(file_get_contents($header_url), '.html'), 'footer-html' => $footer_url, //new File(file_get_contents($footer_url), '.html'), 'replace' => array( 'username' => 'My username' ), );

The replace gets added to the command like:
--replace "{u’“’”’username’“‘”’: u’“’”’My username’“‘”’}"

UnboundLocalError: local variable 'options' referenced before assignment

web_1 | [2018-10-23 03:16:07 +0000] [1] [INFO] Starting gunicorn 19.7.1
web_1 | [2018-10-23 03:16:07 +0000] [1] [INFO] Listening at: http://0.0.0.0:80 (1)
web_1 | [2018-10-23 03:16:07 +0000] [1] [INFO] Using worker: sync
web_1 | [2018-10-23 03:16:07 +0000] [8] [INFO] Booting worker with pid: 8
web_1 | [2018-10-23 03:20:06 +0000] [8] [ERROR] Error handling request /
web_1 | Traceback (most recent call last):
web_1 | File "/usr/local/lib/python2.7/dist-packages/gunicorn/workers/sync.py", line 135, in handle
web_1 | self.handle_request(listener, req, client, addr)
web_1 | File "/usr/local/lib/python2.7/dist-packages/gunicorn/workers/sync.py", line 176, in handle_request
web_1 | respiter = self.wsgi(environ, resp.start_response)
web_1 | File "/usr/local/lib/python2.7/dist-packages/werkzeug/wrappers.py", line 308, in application
web_1 | resp = f(*args[:-2] + (request,))
web_1 | File "/app.py", line 48, in application
web_1 | if options:
web_1 | UnboundLocalError: local variable 'options' referenced before assignment

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.