Generating Dynamic PDFs With CSS In Flask – Bootstrap Example


[ad_1]
https://i.ytimg.com/vi/Jg20ZFqF7BE/hqdefault.jpg



In the first video, I just showed you how to generate plain PDF files with no styling. In this video, I’ll continue by showing you how to include CSS within your PDF file to make it look much better.

You’ll be using the Python library called PDFKit to do this. Also, I’ll take a Bootstrap example and convert it to a PDF.

Join my free course on the basics of Flask-SQLAlchemy: https://prettyprinted.com/flasksql

Watch the first video here: https://youtu.be/C8jxInLM9nM

To download a Flask cheat sheet, go here: http://prettyprinted.com/flaskcheatsheet/

Documentation for PDFKit and wkhtmltopdf:

https://pypi.python.org/pypi/pdfkit

https://github.com/JazzCore/python-pdfkit/wiki/Using-wkhtmltopdf-without-X-server

Original source


10 responses to “Generating Dynamic PDFs With CSS In Flask – Bootstrap Example”

  1. Thanks, it works perfectly without CSS.
    Unfortunately, with styling i get an Internal Server Error. 🙁

    Errors.log : IOError: [Errno 2] No such file or directory: 'main.css'

  2. IOError: No wkhtmltopdf executable found: "C:Program Fileswkhtmltopdfinwkhtmltopdf"
    If this file exists please check that this process can read it. Otherwise please install wkhtmltopdf – https://github.com/JazzCore/python-pdfkit/wiki/Installing-wkhtmltopdf
    Old friend, any idea?
    my code:

    @app.route('/user/<string:username>/curriculum', methods=['GET'])
    def curriculum(username):
    rendered = render_template('user/pdfcv.html')

    path_wkthmltopdf = 'C:Program Fileswkhtmltopdfbinwkhtmltopdf.exe'
    config = pdfkit.configuration(wkhtmltopdf=path_wkthmltopdf)
    #pdfkit.from_url(url='http://127.0.0.1:8000/', output_path=pdffilepath,configuration=config)
    pdf = pdfkit.from_string(rendered, False, config=config)

    response = make_response(pdf)
    response.headers['Content-Type'] = 'application/pdf'
    response.headers['Content-Disposition'] = 'inline; filename=output.pdf'

    return response

Leave a Reply