Simple webservice to generate pdf from html using wkhtml library. Basically there is only one file in this project, it takes in parameter an url and directly outputs the pdf content in the answer.
-
Package wkhtml : C++ lib which converts html into pdf. Uses WebKit as render engine. sudo apt-get install wkhtmltopdf
-
Package xvfb : X Server used by wkhtmltopdf. sudo apt-get install xvfb
-
(OPTIONNAL) Package Ghostscript : Ghostscript is mandatory for option cmyk=1 (CMYK pdf output) sudo apt-get install ghostscript
-
Get project on git git clone https://hg.prod.canaltp.fr/ctp/pdfGenerator.git
-
Run composer composer.phar install
-
Use it!
knplabs/knp-snappy: https://github.com/KnpLabs/snappy
Call index.php and give in parameter the url to convert into pdf. Example (without vhost):
https://localhost/pdfGenerator/web/index.php?url=https://www.example.org
You can also omit the scheme (AKA the protocol)
https://localhost/pdfGenerator/web/index.php?url=google.fr
If your url contains GET parameters, you need to encode it (using encodeURIComponent for instance) so it will look like this
https://localhost/pdfGenerator/web/index.php?url=https%3A%2F%2Fwww.phpbb.com%2Fabout%2Ffeatures%2F%3Ffrom%3Dsubmenu
Wkhtml has many parameters to refine the pdf generation as you can see here:
https://madalgo.au.dk/~jakobt/wkhtmltoxdoc/wkhtmltopdf-0.9.9-doc.html
All these parameters may be sent to the webservice:
https://localhost/pdfGenerator/web/index.php?url=google.fr&orientation=landscape&zoom=2
Optionnal url get parameters could be sent to pdfGenerator:
- margin : Will set CSS document sheet margin
- cmyk : When set to 1 or True, the returned pdf will use CMYK colorspace (usefull for offset or Web professionnal printers)
Additionnaly all wkhtmltopdf options are available (See https://wkhtmltopdf.org/usage/wkhtmltopdf.txt) Several options have an overloaded default value :
- lowquality : false
- disable-javascript : true
- disable-smart-shrinking : true
- print-media-type : true
By default there is no javascript to minimize resources consumption.You can re-enable it by setting disable-javascript to false.
Securize this webservice using apache vhost, limit to a specific range of ip for instance.
Check if the url exists and returns a valid http code? Add an option to enable javascript if needed?
- Vincent Degroote - [email protected]