Flask-WkHTMLtoPDF allows you to easily convert JavaScript dependent Flask templates into PDFs with wkhtmltopdf. For complete documentation, please visit http://flask-wkhtmltopdf.readthedocs.org/en/latest/?badge=latest.
Install the extension with one of the following commands:
$ easy_install flask-wkhtmltopdf
or alternatively if you have pip installed:
$ pip install flask-wkhtmltopdf
You will also need to install the WkHTMLtoPDF command line library from http://wkhtmltopdf.org/downloads.html. Alternatively, you can install from the command line for unix based machines by finding the appropriate distrubution on http://download.gna.org/wkhtmltopdf/0.12/0.12.2.1/. For instance, for linux:
$ wget http://download.gna.org/wkhtmltopdf/0.12/0.12.2.1/wkhtmltox-0.12.2.1_linux-precise-amd64.deb
$ sudo dpkg -i wkhtmltox-0.12.2.1_linux-precise-amd64.deb
First setup in your main Flask file.
from flask_wkhtmltopdf import Wkhtmltopdf
app = Flask(__name__)
wkhtmltopdf = Wkhtmltopdf(app)
Then add these to your app's config
WKHTMLTOPDF_BIN_PATH = r'C:\Program Files\wkhtmltopdf\bin' #path to your wkhtmltopdf installation.
PDF_DIR_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), 'static', 'pdf')
To convert a flask template into a PDF, simply use the render_template_to_pdf() function.
render_template_to_pdf('test.html', download=True, save=False, param='hello')
Contributions are more than welcomed. Please follow these steps:
- Fork this repository
- Make your changes
- Test your changes locally:
$ python setup.py test
- Submit a pull request