0

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



Pandas
  • 29
  • 9
  • Can you share the entire error page to get more info? In any case, the error says that your are passing a string, not a file to read. Maybe you can solve it just adding this: response['Content-Transfer-Encoding'] = 'binary' – LaCharcaSoftware Feb 21 '22 at 10:54
  • Do you want to download a pdf file from your server or do you want to convert the web page to a pdf file and download it? – Sandil Ranasinghe Feb 21 '22 at 11:28
  • Of course, because you are not using FileWrapper function in a right way. You are passing just a string with a filename to FileWrapper. This has no sense. What exactly is the content of your PDF? You can find some examples of FileWrapper here: https://www.programcreek.com/python/example/78219/wsgiref.util.FileWrapper – LaCharcaSoftware Feb 21 '22 at 11:29
  • On my html page " results.html " i want to add a button to download the web page to pdf and download it How is it possible, please ? What i have to write in the fonction" download_pdf" in the file views.py? .html : Download pdf – Pandas Feb 21 '22 at 11:39
  • 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"), ] – Pandas Feb 21 '22 at 11:40
  • views.py: def download_pdf(request): return response – Pandas Feb 21 '22 at 11:41
  • the content of my PDF page is the content of my html page : "results.html" . Iwant to convert the web page to a pdf file and download it – Pandas Feb 21 '22 at 11:42
  • try this[reply](https://stackoverflow.com/a/1377652/16502488) from stack overflow good luck with your project – Audrey Sep 08 '22 at 11:58

1 Answers1

0

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.

sauerburger
  • 4,569
  • 4
  • 31
  • 42