Django App – Button Download home page to pdf
Question:
i am working on an django App
On my html page " results.html "
i want to add a button to download the page to pdf
How is it possible, please ?
What i have to write in the fonction" download_pdf" in the file views.py?
.html :
<a class="btn btn-primary" href="{% url 'download_pdf' %}" role="button">Download pdf</a>
urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.home),
path('views.download_my_pdf', views.download_pdf, name="download_pdf"),
]
views.py :
def download_pdf(request):
return response
Answers:
You can convert a HTML page to a PDF on-the-fly and send its result as a response in Django with Django Weasyprint. E.g., have a look at the examples with WeasyTemplateResponseMixin
.
I ran into the same issue and found a great way to hand html to pdf conversion using a reply in another stack over flow answer try this
i am working on an django App
On my html page " results.html "
i want to add a button to download the page to pdf
How is it possible, please ?
What i have to write in the fonction" download_pdf" in the file views.py?
.html :
<a class="btn btn-primary" href="{% url 'download_pdf' %}" role="button">Download pdf</a>
urls.py
from django.urls import path
from . import views
urlpatterns = [
path('', views.home),
path('views.download_my_pdf', views.download_pdf, name="download_pdf"),
]
views.py :
def download_pdf(request):
return response
You can convert a HTML page to a PDF on-the-fly and send its result as a response in Django with Django Weasyprint. E.g., have a look at the examples with WeasyTemplateResponseMixin
.
I ran into the same issue and found a great way to hand html to pdf conversion using a reply in another stack over flow answer try this