Dark Mode

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

opencodigos/DjangoExportPDF

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

1 Commit

Repository files navigation

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

myapp/urls.py

from django.urls import path
from myapp import views

urlpatterns = [
...
path('export-pdf/', views.export_pdf, name='export-pdf'),
]

myapp/templates/export-pdf.html

Lista de Produtos {% for product in products %} {% endfor %}
# Nome Preco Descricao Imagens
{{ product.id }} {{ product.name|upper }} {{ product.price }} {{ product.description }} {% for el in product.products.all %} {{el.id}} {% endfor %}
{% 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 %}
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
ExportPDF"><a class="btn btn-danger" href="{% url 'export-pdf' %}?obj={{request.GET.obj}}">ExportPDF</a>

About

Exportar dados do modelo para PDF utilizando python e framework Django.

Topics

Resources

Readme

Stars

Watchers

Forks

Releases

No releases published

Packages

Contributors