def draw_label_top_bc(label, width, height, obj): # Just convert the object to a string and print this at the bottom left of # the label. label.add( shapes.String(2, 2, str(obj[0]), fontName="Helvetica", fontSize=12)) label.add( shapes.String(2, 13, str(obj[1]), fontName="Helvetica", fontSize=12)) label.add( shapes.String(2, 24, str(obj[2]), fontName="Helvetica", fontSize=12)) label.add(shapes.Image(2, 46, 180, 40, make_barcode(obj[3]))) # label.add(shapes.String(38, 30, str(obj[3]), fontName="3of9", fontSize=30)) label.add( shapes.String(2, 35, str(obj[4]), fontName="Helvetica", fontSize=12))
def draw_label_plate(label, width, height, obj): # Generate QR Code for labels qr_barcode = mktemp(suffix='.png') qrcode.make(str(obj['ID'])).save(qr_barcode, "png") label.add(shapes.Image(3, 0, 44, 44, qr_barcode)) label.add( shapes.String(45, 16, str(obj['ID']), fontName="Courier", fontSize=16))
def generate(img_path, pdf_width_mm, pdf_height_mm, pdf_path): """ Convert an input image (*) into a PDF output document, having the specified size in mm. (*) given as an input file path; the caller may have to store it in a temporary file. """ mm2point = lambda x: x * 0.0393701 * 72 w, h = mm2point(pdf_width_mm), mm2point(pdf_height_mm) #print w, h img = shapes.Image(0, 0, w, h, img_path) page = shapes.Drawing(w, h) page.add(img) renderPDF.drawToFile(page, pdf_path) return
def write_content(label, width, height, filepath): # Create a shape for the QR code image and position it im = Image.open(filepath) imwidth, imheight = im.size a = shapes.Image(((width / 2.0) - (imwidth / 2.0)), 45, imwidth, imheight, filepath) # Create a human-readable label and position it human_readable = os.path.splitext(os.path.split(filepath)[1])[0] b = shapes.String(width / 2.0, 15, human_readable, textAnchor="middle") # Create a space to write in a donor name c = shapes.String(30, (height - 30), "Donor name: ") label.add(a) label.add(b) label.add(c)
def draw_label(label, width, height, obj): # Just convert the object to a string and print this at the bottom left of # the label. Prenom = obj[0] Nom = obj[1] Entreprise = obj[2] Email = obj[3] Tel = obj[4] # Measure the width of the first name and shrink the font size until it fits. prenom_font_size = 14 text_width = width - 90 prenom_width = stringWidth(Prenom, "Helvetica", prenom_font_size) while prenom_width > text_width: prenom_font_size *= 0.8 prenom_width = stringWidth(Prenom, "Helvetica", prenom_font_size) label.add(shapes.String(10, 90, Prenom.decode('utf-8').title(), fontName="Helvetica-Bold", fontSize=prenom_font_size)) # Measure the width of the last name and shrink the font size until it fits. nom_font_size = 14 text_width = width - 90 nom_width = stringWidth(Nom, "Helvetica", nom_font_size) while nom_width > text_width: nom_font_size *= 0.8 nom_width = stringWidth(Nom, "Helvetica", nom_font_size) label.add(shapes.String(10, 60, Nom.decode('utf-8').upper(), fontName="Helvetica-Bold", fontSize=nom_font_size)) # Measure the width of the entreprise and shrink the font size until it fits. entreprise_font_size = 14 text_width = width - 5 Entreprise_width = stringWidth(Entreprise, "Helvetica", entreprise_font_size) while Entreprise_width > text_width: entreprise_font_size *= 0.8 Entreprise_width = stringWidth(Entreprise, "Helvetica", entreprise_font_size) label.add(shapes.String(10, 20, Entreprise, fontName="Helvetica", fontSize=entreprise_font_size)) # Insertion du QRCode NomComplet= Prenom + " " + Nom vcard="BEGIN:VCARD\nN:" + NomComplet + "\nORG:" + Entreprise + "\nEMAIL:" + Email + "\nTEL;TYPE=WORK:" + Tel + "\nEND:VCARD" img = qrcode.make(vcard) label.add(shapes.Image(190,35,80,80,img))
def _draw_label(label, width, height, obj): item, project = obj #qrcode = qr.QrCodeWidget('s0inv.de/'+str(item.id)) long_id = str(item.id).rjust(5, '0') barcode = createBarcodeDrawing('Code128', value=long_id, barHeight=30, humanReadable=True, quiet=False, width=70) barcode.translate(26, 0) label.add(barcode) qrcode = createBarcodeDrawing('QR', value='s0inv.de/' + str(item.id), barHeight=50) qrcode.translate(90, -2) label.add(qrcode) if project and project.logourl: urllib.request.urlretrieve(project.logourl, 'tmp/logo') logo = shapes.Image(0, 2, 40, 40, 'tmp/logo') label.add(logo)
25, corner_radius=2, background_filename=file1) process_sheet(specs, "page_background_file.pdf") # Option two: background from a ReportLab image. # Note we just load it from file, but you could do something fancier... # The size parameters don't matter as pylabels will scale it to fit the page. specs = labels.Specification(210, 297, 2, 8, 90, 25, corner_radius=2, background_image=shapes.Image( 0, 0, 750, 1055, file2)) process_sheet(specs, "page_background_image.pdf") # Option three: use a ReportLab drawing. # Again, this will be automatically scaled so choose the size to suit you. # Using the size of the page sounds like a sensible option. bg = shapes.Drawing(width=210.5, height=297) bg.add(shapes.String(105, 50, "My cool background", textAnchor="middle")) bg.add(shapes.Wedge(10, 155, 95, 30, 90, fillColor=colors.green)) specs = labels.Specification(210, 297, 2, 8, 90, 25, corner_radius=2,
def write_data(label, width, height, data): (num1, str1, str2, str3, str4) = get_labels_from_data(data) pad = 10 # section 1 : barcode D = Drawing(width, height) d = createBarcodeDrawing('Code128', value=num1, barHeight=0.4 * inch, humanReadable=True, quiet=False) #d = createBarcodeDrawing('I2of5', value=the_num, barHeight=10*mm, humanReadable=True) barcode_width = d.width barcode_height = d.height #d.rotate(-90) #d.translate( - barcode_height ,pad) # translate d.translate(width - barcode_width - pad / 2.0, 0) # translate #pprint(d.dumpProperties()) #D.add(d) #label.add(D) label.add(d) rect = shapes.Rect(0, pad, barcode_width + pad, barcode_height + pad) rect.fillColor = None rect.strokeColor = random.choice((colors.blue, colors.red, colors.green)) #rect.strokeWidth = d.borderStrokeWidth #label.add(rect) # section 2 : room number #the_text = "gr" + str(data['youngest_child_grade']) + " rm" + str(data['youngest_child_room']) #label.add(shapes.String(15, height-15, the_text, fontName="Judson Bold", fontSize=8, textAnchor="start")) # section2: family name # Measure the width of the name and shrink the font size until it fits. font_name = "Judson Bold" font_name = "PermanentMarker" # Measure the width of the name and shrink the font size until it fits. # try out 2 options and select the one that gives a taller font text_width_limit = width - barcode_width - pad text_height_limit = height / 2.0 s1 = fit_text_in_area(str1, font_name, text_width_limit, text_height_limit) text_width_limit = width - pad text_height_limit = height - barcode_height s2 = fit_text_in_area(str1, font_name, text_width_limit, text_height_limit) if (s1.fontSize >= s2.fontSize): s = s1 else: s = s2 s.x = pad / 2.0 s.y = height - s.fontSize + pad / 2.0 s.textAnchor = "start" label.add(s) family_name_height = get_font_height(s.fontSize, str1) family_name_width = stringWidth(str1, font_name, s.fontSize) # section3: parent names text_width_limit = width - barcode_width - 2 * pad text_height_limit = (height - family_name_height) / 2.0 font_name = "Judson Bold" s = fit_text_in_area(str2, font_name, text_width_limit, text_height_limit) s.x = pad / 2.0 s.y = height - family_name_height - s.fontSize + pad / 2.0 s.textAnchor = "start" label.add(s) parent_name_height = get_font_height(s.fontSize, str2) # section4: child's names text_width_limit = width - barcode_width - 2 * pad text_height_limit = height - family_name_height - parent_name_height font_name = "Judson Bold" s = fit_text_in_area(str3, font_name, text_width_limit, text_height_limit) s.x = pad / 2.0 s.y = height - family_name_height - parent_name_height - s.fontSize + pad / 2.0 s.textAnchor = "start" label.add(s) child_name_height = s.fontSize # section 4 : label number font_name = "Judson Bold" font_size = 5 s = shapes.String(width, height - font_size, str4, fontName=font_name, fontSize=font_size, textAnchor="end") #s.x = width #s.y = 0 #s.textAnchor = "start" #label.add(s) # section 5 : logo s = shapes.Image(0, 0, 25, 25, "logo.jpg") s.x = width - barcode_width - (barcode_width - 25) / 2.0 + 1 s.y = height - pad - 15 # enough space? if ((width - family_name_width - pad) > barcode_width): label.add(s)