def barChart(daten,Versuch,Phaenomene,path=None,vMin=1,vMax=6): """ Plots data to a Drawing and returns the Drawing """ #Festlegen der Gesamtgröße in Pixel d = Drawing(500,160) #Daten für das Diagramm #daten = [(10,6,8)] #Anlegen des Diagramms diagramm = HorizontalBarChart() #Positionierung und Größe des Diagramms diagramm.x = 10 diagramm.y = 30 diagramm.height = 100 diagramm.width = 400 #Hinzufügen der Daten #diagramm.reversePlotOrder = 1 diagramm.data = daten #Y-Achse (in ReportLab „valueAxis“) formatieren diagramm.valueAxis.valueMin = vMin diagramm.valueAxis.valueMax = vMax diagramm.valueAxis.valueStep = 1 #X-Achse (in ReportLab „categoryAxis“) formatieren diagramm.categoryAxis.categoryNames = Phaenomene #Diagramm zeichnen d.add(diagramm) if not path == None: Versuch = path + Versuch renderPM.drawToFile(d, Versuch + ".png", 'PNG') #d = Paragraph(d, centered) d.scale(0.8,0.8) return d
def barcode(value): "generate a barcode for the provided value" barcode = createBarcodeDrawing("Code128", value=str(value)) scale = 30 / barcode.height drawing = Drawing(barcode.width * scale, 30) drawing.scale(scale, scale) drawing.add(barcode, name='barcode') data = b64encode(renderPM.drawToString(drawing, fmt='PNG')) return mark_safe('<img src="data:image/png;base64,{}">'.format(data))
def test2(self): "Test scaled Image shape adding it to a PDF page." inPath = IMAGENAME img = Image(0, 0, 110, 44, inPath) d = Drawing(110, 44) d.add(img) d.translate(120, 0) d.scale(2, 2) IMAGES.append(d)
def test2(self): "Test scaled Image shape adding it to a PDF page." inPath = IMAGENAME img = Image(0, 0, 110, 44, inPath) d = Drawing(110, 44) d.add(img) d.translate(120, 0) d.scale(2, 2) self.IMAGES.append(d)
def get_barcode(value, width, barWidth = 0.05 * units.inch, fontSize = 20, humanReadable = False): barcode = createBarcodeDrawing('Code128', value = value, barWidth = barWidth, fontSize = fontSize, humanReadable = humanReadable) drawing_width = width barcode_scale = drawing_width / barcode.width drawing_height = 40 drawing = Drawing(drawing_width, drawing_height) drawing.scale(barcode_scale, barcode_scale) drawing.add(barcode, name='barcode') return drawing
def test3(self): "Test rotated Image shape adding it to a PDF page." inPath = IMAGENAME img = Image(0, 0, 110, 44, inPath) d = Drawing(110, 44) d.add(img) d.translate(420, 0) d.scale(2, 2) d.rotate(45) IMAGES.append(d) IMAGES.append(None) # used to indicate last test
def get_barcode(self, value, width, barWidth = 0.05 * units.inch, fontSize = 12, humanReadable = True): # El valor por default de fontSize=60 barcode = createBarcodeDrawing('Code128', value = value, barWidth = barWidth, fontSize = fontSize, humanReadable = humanReadable) drawing_width = width barcode_scale = drawing_width / barcode.width drawing_height = barcode.height * barcode_scale drawing = Drawing(drawing_width, drawing_height) drawing.scale(barcode_scale, barcode_scale) drawing.add(barcode, name='barcode') barcode_encode = b64encode(renderPM.drawToString(drawing, fmt = 'PNG')) barcode_str = '<img style="width:320px;height:80px;" src="data:image/png;base64,{0} : ">'.format(barcode_encode) return barcode_str
def get_barcode(value, width, humanReadable = True): #value = value.encode('ascii', 'ignore') value = unidecode(value) barcode = createBarcodeDrawing('Code128', value = value, humanReadable = humanReadable, fontSize = 8) drawing_width = width barcode_scale = drawing_width / barcode.width drawing_height = barcode.height * barcode_scale drawing = Drawing(drawing_width, drawing_height) drawing.scale(barcode_scale, barcode_scale) drawing.add(barcode, name='barcode') return drawing
def get_barcode(value, width, humanReadable=True): #value = value.encode('ascii', 'ignore') value = unidecode(value) barcode = createBarcodeDrawing('Code128', value=value, humanReadable=humanReadable, fontSize=8) drawing_width = width barcode_scale = drawing_width / barcode.width drawing_height = barcode.height * barcode_scale drawing = Drawing(drawing_width, drawing_height) drawing.scale(barcode_scale, barcode_scale) drawing.add(barcode, name='barcode') return drawing
def get_gs1_128_barcode_image(self, value, width, barWidth=0.05 * units.inch, fontSize=30, humanReadable=True): barcode = createBarcodeDrawing('Code128', value=value, barWidth=barWidth, fontSize=fontSize, humanReadable=humanReadable) drawing_width = width barcode_scale = drawing_width / barcode.width drawing_height = barcode.height * barcode_scale drawing = Drawing(drawing_width, drawing_height) drawing.scale(barcode_scale, barcode_scale) drawing.add(barcode, name='barcode') data = b64encode(renderPM.drawToString(drawing, fmt='PNG')) return data
class calTarget: fontName = 'Helvetica' fontSize = 12 margins = (0.5 * inch, 0.5 * inch) def __init__(self, gridCount, gridSpacing=None, invert=False): self.gridCount = gridCount gridSpacing = 25 if gridSpacing is None else gridSpacing self.gridSpacing = gridSpacing * mm if hasattr( gridSpacing, "__len__") else (gridSpacing * mm, gridSpacing * mm) if invert: self.colorBackground = black self.colorForeground = white else: self.colorBackground = white self.colorForeground = black ps = self.get_pageSize() self.drawing = Drawing(ps[0], ps[1]) self.draw() def save(self, filename, dpi): ext = os.path.splitext(filename)[1][1:] if (ext == 'pdf'): renderPDF.drawToFile(self.drawing, filename) elif ext in ('png', 'jpg'): self.drawing.scale(dpi / 72.0, dpi / 72.0) renderPM.drawToFile(self.drawing, filename, dpi=dpi, fmt=ext) else: raise NotImplementedError('Unknown file type: ' + ext) def addTextFooter(self, str=None): if str is None: str = self.description() self.drawing.add( String(self.margins[0], self.margins[1] / 2, str, fontName=self.fontName, fontSize=self.fontSize, fillColor=Color(0.6, 0.6, 0.6)))
def get_code128_barcode(self, value, width, barWidth=(0.05 * units.inch) / 2, fontSize=12, humanReadable=True): # El valor por default de fontSize=60 barcode = createBarcodeDrawing('Code128', value=value, barWidth=barWidth, fontSize=fontSize, humanReadable=humanReadable) drawing_width = width barcode_scale = drawing_width / barcode.width drawing_height = barcode.height * barcode_scale drawing = Drawing(drawing_width, drawing_height) drawing.scale(barcode_scale, barcode_scale) drawing.add(barcode, name='barcode') barcode_encode = b64encode(renderPM.drawToString(drawing, fmt='PNG')) barcode_str = '<img style="width:320px;height:80px;" src="data:image/png;base64,{0} : ">'.format( barcode_encode) return barcode_str
from reportlab.lib.pagesizes import A4 guion = [] imagen = Image( 400, 10, 596, 133, "/home/local/DANIELCASTELAO/fsancheztemprano/Imágenes/triforce.jpg") dibujo = Drawing(30, 30) dibujo.add(imagen) dibujo.translate(0, 600) guion.append(dibujo) dibujo = Drawing(30, 30) dibujo.add(imagen) dibujo.rotate(45) dibujo.scale(1.5, 0.5) dibujo.translate(-90, 300) guion.append(dibujo) dibujo = Drawing(30, 30) dibujo.add(imagen) dibujo.rotate(90) dibujo.translate(-20, -100) guion.append(dibujo) dibujo = Drawing(A4[0], A4[1]) for deb in guion: dibujo.add(deb) renderPDF.drawToFile(dibujo, "prueba2.pdf")
def auction_qr_codes(request, pk): user = request.user admin = False try: auction = Auction.objects.get(pk=pk) except Auction.DoesNotExist: raise Http404( "The auction you are trying to view does not exist or may have been deleted" ) if user.is_admin(auction.pk): admin = True buffer = io.BytesIO() # 612.0 x 792.0 (letter size) p = canvas.Canvas(buffer, pagesize=letter) x_res = 612 y_res = 792 include_images = 'include_images' in request.POST num_rows = 4 num_cols = 2 if include_images: num_rows = 2 num_cols = 2 block_width = x_res / num_cols block_height = y_res / num_rows image_width = 170 image_height = 170 qr_width = 130 qr_height = 130 text_size = 20 space_between = (block_height - qr_height - text_size) / 3 if include_images: space_between = (block_height - image_height - qr_height - text_size) / 4 index = 0 # Draw divider lines on first page for r in range(1, num_rows): for c in range(1, num_cols): p.line(c * block_width, 0, c * block_width, y_res) p.line(0, r * block_height, x_res, r * block_height) p.setFont("Times-Roman", text_size) for item in auction.item_set.all(): if index != 0 and index % (num_rows * num_cols) == 0: # Change to new page, draw divider lines, and reset font p.showPage() for r in range(1, num_rows): for c in range(1, num_cols): p.line(c * block_width, 0, c * block_width, y_res) p.line(0, r * block_height, x_res, r * block_height) p.setFont("Times-Roman", text_size) # Generate QR code from item page URL page_url = request.build_absolute_uri( reverse('auction:item', args=(item.id, ))) qrw = QrCodeWidget(page_url) b = qrw.getBounds() w = b[2] - b[0] h = b[3] - b[1] d = Drawing() d.add(qrw) # Bottom left corner of block x_offset = (index % num_cols) * block_width y_offset = (num_rows - 1 - (index // num_cols) % num_rows) * block_height # Draw QR code d.translate(x_offset + block_width / 2 - qr_width / 2, y_offset + space_between) d.scale(qr_width / w, qr_height / h) renderPDF.draw(d, p, 1, 1) # Draw item name p.drawCentredString( x_offset + block_width / 2, y_offset + block_height - text_size - space_between, item.name) if include_images: # Draw item image p.drawImage(settings.BASE_DIR + item.image.url, x_offset + x_res / 4 - image_width / 2, y_offset + qr_height + 2 * space_between, image_width, image_height) index += 1 p.save() buffer.seek(0) # Return rendered PDF file return FileResponse(buffer, as_attachment=True, filename='QR Codes Printout.pdf')
c.setPageSize((width, height)) for element in page['elements']: element_type = element.setdefault('type', 'text') fontSize = element.setdefault('fontSize', 10) fontType = element.setdefault('fontType', 'Courier') element_width = element.setdefault('width', 30) element_height = element.setdefault('height', 30) text = element.setdefault('text', '') if element_type == "barcode": barcode = createBarcodeDrawing( 'Code128', height=element_height, value=text, barWidth=1, humanReadable=True) drawing_width = element_width barcode_scale = drawing_width / barcode.width drawing_height = barcode.height * barcode_scale drawing = Drawing(drawing_width, drawing_height) drawing.scale(barcode_scale, barcode_scale) drawing.add(barcode, name='barcode') c.setFont(fontType, fontSize) renderPDF.draw(drawing, c, element['x'], element['y']) elif element_type == "text": text_width = stringWidth(text, fontType ,fontSize ) font_ratio = element_width/text_width if (font_ratio<=1): fontSize = fontSize*font_ratio pprint(font_ratio) c.setFont(fontType, fontSize) c.drawString(element['x'], element['y'], element['text']) c.showPage() c.save()
class Barcode: """Model of a barcode object. This class handles the generation and parsing of barcodes to image strings.""" unit_mapping = {'inch': units.inch, 'mm': units.mm} DEFAULT_WIDTH_CODE128 = 200 DEFAULT_WIDTH_QR = 100 def __init__(self, type, text_value, width=None, height=None, unit=None): """ Initialize the barcode. This creates the actual barcode and sets it up for further action. """ # Set class variables self.type = type self.text_value = text_value # Create the barcode barcode = createBarcodeDrawing(type, value=text_value, humanReadable=True) width, height, scale = self._determine_dimensions( barcode, width, height, unit) # Create the drawing self.drawing = Drawing(width, height) self.drawing.scale(scale, scale) self.drawing.add(barcode, name='barcode') def _determine_dimensions(self, barcode, width, height, unit): """Determine the dimensions and scale of the barcode.""" # Determine the width if width: width = float(width) * self.unit_mapping.get(unit, units.mm) else: width = self.DEFAULT_WIDTH_CODE128 if self.type == 'Code128' \ else self.DEFAULT_WIDTH_QR width *= self.unit_mapping.get(unit, units.mm) # Determine the scale scale = width / barcode.width # Determine the height if height: height = float(height) * self.unit_mapping.get(unit, units.mm) else: # Determine the barcode height automatically. # Based on code found here: # http://stackoverflow.com/a/13350788 height = barcode.height * scale return (width, height, scale) def save(self, location, filename, formats): """ Save the barcode """ self.drawing.save(formats, location, fnRoot=filename) def as_string(self, format): """Return the barcode as a string in the specified format.""" return self.drawing.asString(format) def as_image(self, format): """Return the barcode as an image in the specified format.""" image_string = self.drawing.asString(format) return base64.b64encode(image_string).decode('ascii') @property def width(self): """Get the width of the barcode drawing.""" return self.drawing.width @property def height(self): """Get the height of the barcode drawing.""" return self.drawing.height
from reportlab.graphics.shapes import Image, Drawing from reportlab.graphics import renderPDF from reportlab.lib.pagesizes import A4 imaxes = [] imaxe = Image(150, 50, 589, 202, "/home/dam2a/Descargas/vaca-lechera.jpg") debuxo = Drawing(500, 102) debuxo.translate(0, 650) #movelo de posición debuxo.add(imaxe) debuxo2 = Drawing(250, 51) debuxo2.add(imaxe) debuxo2.rotate(45) debuxo2.scale(0.5, 1.5) debuxo2.translate(150, -45) imaxes.append(debuxo2) imaxes.append(debuxo) documento = Drawing(A4[0], A4[1]) for elemento in imaxes: documento.add(elemento) renderPDF.drawToFile(documento, "segundoInforme.pdf")
from reportlab.graphics import renderPDF from reportlab.lib.pagesizes import A4 imaxes = [] imaxe = Image(400, 0, 596, 133, "/home/manuel/Imaxes/200.jpg") debuxo = Drawing(30, 30) debuxo.add(imaxe) debuxo.translate(0, 0) imaxes.append(debuxo) debuxo = Drawing(30, 30) debuxo.add(imaxe) debuxo.scale(1.5, 0.5) debuxo.translate(-300, 300) debuxo.rotate(45) imaxes.append(debuxo) debuxo = Drawing(30, 30) debuxo.add(imaxe) debuxo.rotate(theta=60) debuxo.translate(-400, 00) imaxes.append(debuxo) debuxo = Drawing(A4[0], A4[1]) for aux in imaxes: debuxo.add(aux) renderPDF.drawToFile(debuxo, "probaInformes3.pdf")