Code Monkey home page Code Monkey logo

djangoexportpdf's Introduction

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

{% extends 'base.html' %}

{% block content %}
	<h1>Lista de Produtos</h1>  
    
    <table class="table"> 
        <thead>
            <tr>
                <th scope="col">#</th>
                <th scope="col">Nome</th>
                <th scope="col">Preço</th>
                <th scope="col">Descrição</th>
                <th scope="col">Imagens</th>
            </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 %}
<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
<a class="btn btn-danger" href="{% url 'export-pdf' %}?obj={{request.GET.obj}}">ExportPDF</a>

djangoexportpdf's People

Contributors

eticialima avatar

Stargazers

Renan avatar Marcelo Dev avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.