Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

224
Vistas
Django convert current page to PDF on button click

I am using Django 1.10 and python 3.4 on windows. I set up pdfkit and it works perfectly from command line. I want to use it in the site on a button to return the current page as a pdf attachment without redirecting anywhere(if possible). The page to convert is a report which uses a user specified date as url.

Currently the button redirects by appending the current url with "?mybtn=Generate+PDF#"

I have seen some solutions using jQuery or javascript, but I only really know python well. I will learn those eventually, but for now I would like a quicker solution where I understand what's going on.

views.py

class FlashReport(generic.View):
template_name = "reports_flash_report.html" 

def get(self, request, year, month, day):
    report_data = get_flash_report_data(int(year),int(month),int(day))
    return render(request, 'reports_flash_report.html', report_data)

def generate_PDF(request):
    path = request.get_full_path()
    pdf = pdfkit.from_url(path, False)
    response = HttpResponse(pdf, content_type='application/pdf')
    response['Content-Disposition'] = 'attachment; filename="flash_report.pdf"'
    return response

reports_flash_report.html

<div>
    <form action="" method="get">
        <input type="submit" class="btn" value="Generate PDF" name="mybtn" />
    </form>
</div>

urls.py

url(r'^flash_report/(?P<year>[0-9]{4})-(?P<month>[0-9]{2})-(?P<day>[0-9]{2})/$', views.FlashReport.as_view(), name="flash_report"),
about 4 years ago · Santiago Trujillo
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda