I'm currently building a web application using Flask
. My supervisor requested that one feature should be to save the current page as a PDF (for printing). To this end I'm using pdfkit
. Before anyone proposes to use weasyprint
instead: I tried installing it for 2 hours to no avail, so I've abandoned that. I'm using the Bootstrap
css and its JavaScript to make the application a bit more pleasant to look upon.
I'm using the below file structure (as proposed by a tutorial to Flask
)
+-- name_of_app
| +-- static
| +-- css
| +-- css files
| +-- js
| +-- javascript files
| +-- templates
| +-- .html templates
| +-- __init__.py
| +-- config.py
| +-- forms.py
| +-- views.py
+-- run.py
To just try pdfkit
out, I generate PDFs every time I generate a page, i.e. in all of the functions which are routed in views.py
. All the pages are based on the following template
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<link href="static\css\bootstrap.css" rel="stylesheet" media="screen">
<link href="static\css\bootstrap-theme.css" rel="stylesheet">
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script src="static\js\bootstrap.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
{% if title %}
<title>{{ title }} - Timeseries Project</title>
{% else %}
<title>Welcome to the Timeseries Project</title>
{% endif %}
</head>
<body>
<div class="navbar navbar-default">
<div class="navbar-inner">
<a class="navbar-brand active" href="/">Home</a>
<a class="navbar-brand" href="/">Save as PDF</a>
</div>
</div>
{% block content %}{% endblock %}
</body>
</html>
The web pages load beautifully using the provided css and JS files. However, pdfkit
outputs the following warnings
Warning: Failed to load file:///C:/Users/my_user/AppData/Local/Temp/static/css/bootstrap.css (ignore)
Warning: Failed to load file:///C:/Users/my_user/AppData/Local/Temp/static/css/bootstrap-theme.css (ignore)
Warning: Failed to load file:///C:/Users/my_user/AppData/Local/Temp/static/js/bootstrap.js (ignore)
The issue I'm trying to resolve is therefore: How do I get pdfkit
to look for static
in the folder where one of its methods was executed (i.e. views.py
), and not in AppData
?