I am trying to create pdf from html page using pdfkit
inside Flask
application, but I have troubles to load the static files (stylesheets) when using pdfkit
.
I've tried to come up with minimal example. I've got this file structure
App
|_ static
| |- style.css
|_ templates
| |- base.html
|_ pdfs
| |- file.pdf
|_ application.py
Inside application.py
:
import flask
import pdfkit
app = flask.Flask(__name__)
@app.route('/')
def index():
page = flask.render_template('base.html')
pdfkit.from_string(page, 'pdfs/file.pdf')
return page
@app.route('/download', methods=['POST'])
def download():
if flask.request.method == 'POST':
flask.send_from_directory(
directory='pdfs', filename='file.pdf', as_attachment=True)
else:
flask.redirect(flaks.url_for('index'))
if __name__ == '__main__':
app.run()
Stylesheet just adds red background to the td elements:
td {
background-color: red;
}
And finally the base.html
:
<!DOCTYPE html>
<html>
<head>
<title>Test App</title>
<link href={{ url_for('static', filename='style.css') }} rel='stylesheet'>
</head>
<body>
<div id='to_pdf'>
<table>
<tr>
<th>Firstname</th>
</tr>
<tr>
<td>Jill</td>
</tr>
<tr>
<td>Eve</td>
</tr>
</table>
<form action='/download' method='POST'>
<button type="submit" value="Print" />PRINT</form>
</form>
</div>
</body>
</html>
Sorry for the loads of code, but it is actually pretty basic. All I want is to have button, which once clicked downloads a pdf file (this pdf is html file converted to pdf.
It sort of works, but the console output is the following:
Loading pages (1/6)
Warning: Failed to load file:///static/style.css (ignore)
Counting pages (2/6)
Resolving links (4/6)
Loading headers and footers (5/6)
Printing pages (6/6)
Done
The problem
pdfkit
, which basically invokes wkhtmltopdf
cannot find the stylesheet files inside static
folder. The application, where I have this problem is more robust, uses boostrap etc. and having the pdf output badly formatted is very undesirable..