Created
June 14, 2016 18:18
-
-
Save ejherran/2c56ee1e5ca3668c1613639b746a9e8e to your computer and use it in GitHub Desktop.
Generar multiples códigos QR en formato PDF, comprimir el contenido en un fichero ZIP y descargarlo en modo web con DJango - Python 3
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
# Python 3.5 | |
# Django 1.9.7 | |
from reportlab.pdfgen import canvas | |
from reportlab.graphics.shapes import Drawing | |
from reportlab.graphics.barcode.qr import QrCodeWidget | |
from reportlab.graphics import renderPDF | |
from io import BytesIO, StringIO | |
from zipfile import ZipFile, ZIP_DEFLATED | |
from django.http import HttpResponse | |
# Create your views here. | |
def index(rq): | |
# Crear las response para descargar el archivo zip | |
response = HttpResponse(content_type='application/zip') | |
response['Content-Disposition'] = 'attachment; filename="loteqr.zip"' | |
# Crear el fichero CSV para almacenar la lista de todos los ficheros pdf | |
fcsv = StringIO() | |
lista = [] | |
# Crear el fichero zip contenedor | |
fzip = BytesIO() | |
archive = ZipFile(fzip, 'w', ZIP_DEFLATED) | |
for i in range(50): | |
# Crear el fichero pdf | |
fpdf = BytesIO() | |
# Crear el area de dibujo y vincularla al fichero pdf | |
p = canvas.Canvas(fpdf) | |
# Cargar la informacion del codigo qr | |
qrw = QrCodeWidget('it-sbox.com/teo/'+str(i+1)+'/ZmIwYjgzZmI0MDBkMGU5YzgxMjAwMWMxZmE3M2FjYjBjZTc3YmMxN2U3NmY4MzFjYTI4MjY4ZjZmMGU3Y2NiYWMyZGYyYTcyMGJiZDljODAxMjY3Y2ZhNmQ2MzIyNDdiNDc2Y2ZhMTczMDFlYWIxNzlhMDlhZDhlNjQxMDg0MGE=') | |
# Calcular el ancho y alto del codigo qr | |
b = qrw.getBounds() | |
w = b[2] - b[0] | |
h = b[3] - b[1] | |
# Ajustar el tamaño del canvas al tamaño del codigo | |
p.setPageSize((w + 2, h + 2)) | |
# Dibujar el codigo qr | |
d = Drawing() | |
d.add(qrw) | |
renderPDF.draw(d, p, 1, 1) | |
# Crear la pagina y guardar el contenido del pdf | |
p.showPage() | |
p.save() | |
# Agregar el fichero pdf al contenedor zip | |
archive.writestr('qrcode'+str(i+1)+'.pdf', fpdf.getvalue()) | |
fpdf.close() | |
# Agregar el nombre del fichero pdf a la lista | |
lista.append('qrcode'+str(i+1)+'.pdf') | |
# Convertir la lista de ficheros en formato texto y escribirla en el fichero CSV | |
lista = "\n".join(lista) | |
fcsv.write(lista) | |
# Agregar la lista (CSV) al contenedor zip | |
archive.writestr('lote.csv', fcsv.getvalue()) | |
fcsv.close() | |
# Cerrar el fichero zip | |
archive.close() | |
# Obtener el contenido del fichero zip | |
fzip.flush() | |
rzip = fzip.getvalue() | |
fzip.close() | |
# Cargar el contenido del fichero zip en la response | |
response.write(rzip) | |
# Enviar la response con el fichero zip para ser descargada. | |
return response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment