from reportlab.pdfgen import canvas from reportlab.lib.pagesizes import letter from reportlab.lib.units import mm from reportlab.lib.units import inch #I'll be generating code39 barcodes, others are available from reportlab.graphics.barcode import code39 import sys, qrcode def make_qr(text): e = qrcode.Encoder() image = e.encode(text, mode=e.mode.ALNUM, eclevel=e.eclevel.L,width = 200) image.save('out.png') return 'out.png' def make_label(canvas,shift,content): marg = 0.2 sh = shift * inch * 5.5 c.setFont("Helvetica", 14) #border c.rect(0,sh,(8 - 2.5 * marg)*inch,(5.5 - 2.5 * marg)*inch, fill=0) #ide de la caja barcode=code39.Extended39(content['BoxID'] ,barWidth=0.5*mm,barHeight=20*mm) barcode.drawOn(canvas,100*mm,sh + 100*mm) c.drawString(105 * mm ,sh + 90 * mm,'Content:' + content['Content']) c.drawString(105 * mm ,sh + 85 * mm,'ID:' + content['BoxID']) #qr image = make_qr("{'email':'" + content['email'] +"'," + "'boxid':'"+content['BoxID'] + "','" + "'contents':'" + content['Content'] + "'" + "'}") c.drawImage(image, 10, sh + 10, width=None,height=None,mask=None) # origen c.drawString(10 * mm ,sh + 120 * mm,content['Name']) c.drawString(10 * mm ,sh + 115 * mm,content['FromStreet']) c.drawString(10 * mm ,sh + 110 * mm,content['FromCity']) c.drawString(10 * mm ,sh + 105 * mm,content['FromCountry']) #destino c.setFont("Helvetica", 18) c.drawString(75 * mm ,sh + (121 - 50) * mm,content['Name']) c.drawString(75 * mm ,sh + (114 - 50) * mm,content['ToStreet']) c.drawString(75 * mm ,sh + (108 - 50) * mm,content['ToCity']) c.drawString(75 * mm ,sh + (102 - 50) * mm,content['ToCountry']) print 'Encoding' c=canvas.Canvas("example.pdf",pagesize=letter) c.translate(0.4 * inch,0.30 * inch) d = {'Name':'Pinky y Cerebro', 'FromStreet':'1 Abedules', 'FromCity':'Santiago', 'FromCountry':'CL', 'ToStreet':'10 Central', 'ToCountry':'Spain', 'ToCity':'Alicante', 'email':'pinky@gmail.com', } d['Content']='Libros y documentos' d['BoxID'] = '0005' make_label(c,0,d) d['Content']='Mixed Prototyping Electronics' d['BoxID'] = '0006' make_label(c,1,d) c.showPage() c.save()