Django Export To PDF
Nesse tutorial vamos exportar os dados de uma tabela para PDF.
myapp/views.py
import datetime
from django.http import HttpResponse
import tempfile
from django.template.loader import render_to_string
import weasyprint
def export_pdf(request):
products = Product.objects.all() # lista todos os produtos
html_index = render_to_string('export-pdf.html', {'products': products})
weasyprint_html = weasyprint.HTML(string=html_index, base_url='http://127.0.0.1:8000/media')
pdf = weasyprint_html.write_pdf(stylesheets=[weasyprint.CSS(string='body { font-family: serif} img {margin: 10px; width: 50px;}')])
response = HttpResponse(content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename=Products'+str(datetime.datetime.now())+'.pdf'
response['Content-Transfer-Encoding'] = 'binary'
with tempfile.NamedTemporaryFile(delete=True) as output:
output.write(pdf)
output.flush()
output.seek(0)
response.write(output.read())
return response
from django.http import HttpResponse
import tempfile
from django.template.loader import render_to_string
import weasyprint
def export_pdf(request):
products = Product.objects.all() # lista todos os produtos
html_index = render_to_string('export-pdf.html', {'products': products})
weasyprint_html = weasyprint.HTML(string=html_index, base_url='http://127.0.0.1:8000/media')
pdf = weasyprint_html.write_pdf(stylesheets=[weasyprint.CSS(string='body { font-family: serif} img {margin: 10px; width: 50px;}')])
response = HttpResponse(content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename=Products'+str(datetime.datetime.now())+'.pdf'
response['Content-Transfer-Encoding'] = 'binary'
with tempfile.NamedTemporaryFile(delete=True) as output:
output.write(pdf)
output.flush()
output.seek(0)
response.write(output.read())
return response
myapp/urls.py
from django.urls import path
from myapp import views
urlpatterns = [
...
path('export-pdf/', views.export_pdf, name='export-pdf'),
]
from myapp import views
urlpatterns = [
...
path('export-pdf/', views.export_pdf, name='export-pdf'),
]
myapp/templates/export-pdf.html
Lista de Produtos
{% endblock %}">{% extends 'base.html' %}
{% block content %}
<h1>Lista de Produtosh1>
<table class="table">
<thead>
<tr>
<th scope="col">#th>
<th scope="col">Nometh>
<th scope="col">Precoth>
<th scope="col">Descricaoth>
<th scope="col">Imagensth>
tr>
thead>
<tbody>
{% for product in products %}
<tr>
<th scope="row">{{ product.id }}th>
<th scope="row">{{ product.name|upper }}th>
<th scope="row">{{ product.price }}th>
<th scope="row">{{ product.description }}th>
<th scope="row">
{% for el in product.products.all %}
<img src="{{el.image.url}}" alt="{{el.id}}" class="mx-2" width="50">
{% endfor %}
th>
tr>
{% endfor %}
tbody>
table>
{% endblock %}
| # | Nome | Preco | Descricao | Imagens |
|---|---|---|---|---|
| {{ product.id }} | {{ product.name|upper }} | {{ product.price }} | {{ product.description }} |
{% for el in product.products.all %}
|
{% block content %}
<h1>Lista de Produtosh1>
<table class="table">
<thead>
<tr>
<th scope="col">#th>
<th scope="col">Nometh>
<th scope="col">Precoth>
<th scope="col">Descricaoth>
<th scope="col">Imagensth>
tr>
thead>
<tbody>
{% for product in products %}
<tr>
<th scope="row">{{ product.id }}th>
<th scope="row">{{ product.name|upper }}th>
<th scope="row">{{ product.price }}th>
<th scope="row">{{ product.description }}th>
<th scope="row">
{% for el in product.products.all %}
<img src="{{el.image.url}}" alt="{{el.id}}" class="mx-2" width="50">
{% endfor %}
th>
tr>
{% endfor %}
tbody>
table>
{% endblock %}
ExportPDF"><a class="btn btn-danger" href="{% url 'export-pdf' %}">ExportPDF</a>
Django ExportPDF com Filtro
def export_pdf(request):
obj = request.GET.get('obj')
# products = Product.objects.filter(name__icontains=obj) # lista todos os produtos
print(obj)
if obj:
products = Product.objects.filter(name__icontains=obj)
else:
products = Product.objects.all()
html_index = render_to_string('export-pdf.html', {'products': products})
weasyprint_html = weasyprint.HTML(string=html_index, base_url='http://127.0.0.1:8000/media')
pdf = weasyprint_html.write_pdf(stylesheets=[weasyprint.CSS(string='body { font-family: serif} img {margin: 10px; width: 50px;}')])
response = HttpResponse(content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename=Products'+str(datetime.datetime.now())+'.pdf'
response['Content-Transfer-Encoding'] = 'binary'
with tempfile.NamedTemporaryFile(delete=True) as output:
output.write(pdf)
output.flush()
output.seek(0)
response.write(output.read())
return response
obj = request.GET.get('obj')
# products = Product.objects.filter(name__icontains=obj) # lista todos os produtos
print(obj)
if obj:
products = Product.objects.filter(name__icontains=obj)
else:
products = Product.objects.all()
html_index = render_to_string('export-pdf.html', {'products': products})
weasyprint_html = weasyprint.HTML(string=html_index, base_url='http://127.0.0.1:8000/media')
pdf = weasyprint_html.write_pdf(stylesheets=[weasyprint.CSS(string='body { font-family: serif} img {margin: 10px; width: 50px;}')])
response = HttpResponse(content_type='application/pdf')
response['Content-Disposition'] = 'attachment; filename=Products'+str(datetime.datetime.now())+'.pdf'
response['Content-Transfer-Encoding'] = 'binary'
with tempfile.NamedTemporaryFile(delete=True) as output:
output.write(pdf)
output.flush()
output.seek(0)
response.write(output.read())
return response
ExportPDF"><a class="btn btn-danger" href="{% url 'export-pdf' %}?obj={{request.GET.obj}}">ExportPDF</a>