The following project is an example of running wkhtmltopdf
locally to generate PDF reports.
If you want to read more about wkhtmltopdf
the documentation is available at wkhtmltopdf.org and the github repo is wkhtmltopdf/wkhtmltopdf
.
Follow the below steps to get your environment setup.
- Download and Install Install Docker Desktop for Mac by following the instructions.
- Clone the repo in your home directory by running the following commands:
cd ~
git clone git@github.com:KFoxder/ycharts_pdf_generator.git
Once you have run the Environment Setup. We can run wkhtmltopdf
. There are 2 main ways you will want to run wkhtmltopdf
.
One way to run wkhtmltopdf
is using a static HTML file with markup and styling. The below command will render the example HTML file included as a PDF.
cd ~/ycharts_pdf_generator
docker-compose run ycharts_pdf_generator wkhtmltopdf --dpi 300 --page-size Letter "/tmp/ycharts_pdf_generator/input_html/test.html" /tmp/ycharts_pdf_generator/output_pdfs/`date +%F%T`.pdf
After running the above command you should see a PDF file in the ~/ycharts_pdf_generator/output_pdfs
directory.
The second way to run wkhtmltopdf
is using a URL.
The below example assumes you are running a local development server on localhost
. If you want to generate a PDF from the URL localhost:8080/fs.html
you would run the below command.
cd ~/ycharts_pdf_generator
docker-compose run ycharts_pdf_generator wkhtmltopdf --dpi 300 --page-size Letter "`ipconfig getifaddr en0`:8080/fs.html" /tmp/ycharts_pdf_generator/output_pdfs/`date +%F%T`.pdf
If you want to generate a PDF from a public URL (ie. https://www.google.com/
) you can use the following command.
cd ~/ycharts_pdf_generator
docker-compose run ycharts_pdf_generator wkhtmltopdf --dpi 300 --page-size Letter "https://www.google.com/" /tmp/ycharts_pdf_generator/output_pdfs/`date +%F%T`.pdf
The below example assumes you are running a local development server on localhost
. If you want to generate a PDF from the URL localhost:8080/fs.html
you would run the below command. If you want the pdf generator to use CSS that is using the @media print
you must pass the --print-media-type
flag.
cd ~/ycharts_pdf_generator
docker-compose run ycharts_pdf_generator wkhtmltopdf --print-media-type --dpi 300 --page-size Letter "`ipconfig getifaddr en0`:8080/fs.html" /tmp/ycharts_pdf_generator/output_pdfs/`date +%F%T`.pdf
You will notice that wkhtmltopdf
will display CSS slightly different than the browser which is expected. Under the hood it uses the Qt Webkit engine.