https://github.com/adewes/pdf-printer
A Python + PyQt script that generates PDFs from HTML templates
https://github.com/adewes/pdf-printer
html-to-pdf invoice-generator invoice-pdf jinja2 pdf-generation pyqt5 python3
Last synced: 6 months ago
JSON representation
A Python + PyQt script that generates PDFs from HTML templates
- Host: GitHub
- URL: https://github.com/adewes/pdf-printer
- Owner: adewes
- Created: 2016-08-23T15:35:03.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2018-11-25T13:06:51.000Z (almost 7 years ago)
- Last Synced: 2025-03-29T04:23:59.155Z (6 months ago)
- Topics: html-to-pdf, invoice-generator, invoice-pdf, jinja2, pdf-generation, pyqt5, python3
- Language: Python
- Size: 156 KB
- Stars: 9
- Watchers: 2
- Forks: 1
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Making HTML-based PDFs with Python + Qt
This repository contains code for generating PDFs from Jinja2 templates using
PyQT5's web rendering engine. It is very handy e.g. for generating PDF invoices
or other documents using templated HTML. It comes with a Docker image that makes
running it easy on servers without an X windows system.## Installation
To run the code, you need to install the following requirements:
* Python 3
* Jinja2
* Qt5 + PyQt5
* A minimal X Windows system, or XVFB with required X librariesAll instructions below assume you run a Debian/Ubuntu based system (there's a
dockerized version of the system that you can use as well, please look below).To install the required X window libraries:
sudo apt-get install \
libxcomposite1 \
libxcursor1 \
libxi6 \
libfontconfig \
libxrandr2 \
libasound2 \
libegl1 \
xvfb \
libnss3 \
libxtst6To install the Python dependencies:
pip3 install -r requirements.txt
## Usage
To generate a demo PDF using the `printer.py` script, simply run
make python-example
You can also run the printer using XVFB:
make python-example-xvfb
This will generate a PDF using the content in the `example` directory, storing
the output to `example/output/index.pdf`.## Docker Image
This repository includes a `Dockerfile` that allows you to build a Docker image
that can run the PDF printer (for better isolation / security). To build the
image locally, runmake docker-image
You can then run the PDF creator on the example data using the following command:
make docker-example
Have a look at the Makefile to see how to run the container. Basically,
it expects templates to be mounted under `/templates`, media under
`/media`, the context given as a JSON file located at `/context.json` and
a writeable output directory mounted at `/output`. It will then look for an
`index.html` file in the template directory and write PDF output to
`/output/index.pdf`. The UID/GID of the output file will be set to the values
specified using the `TARGET_UID` environment variable.## License
The code in this repository is in the public domain, you can use it without any
kind of attribution, in any way you please and at your own risk.