def createBarCodes(): """ Create barcode examples and embed in a PDF """ c = canvas.Canvas("barcodes.pdf", pagesize=A4) barcode_value = "1234567890" barcode39 = code39.Extended39(barcode_value) barcode39Std = code39.Standard39(barcode_value, barHeight=20, stop=1) # code93 also has an Extended and MultiWidth version barcode93 = code93.Standard93(barcode_value) barcode128 = code128.Code128(barcode_value) # the multiwidth barcode appears to be broken #barcode128Multi = code128.MultiWidthBarcode(barcode_value) barcode_usps = usps.POSTNET("50158-9999") codes = [barcode39, barcode39Std, barcode93, barcode128, barcode_usps] x = 1 * mm y = 285 * mm x1 = 6.4 * mm for code in codes: code.drawOn(c, x, y) y = y - 15 * mm # draw the eanbc8 code barcode_eanbc8 = eanbc.Ean8BarcodeWidget(barcode_value) bounds = barcode_eanbc8.getBounds() width = bounds[2] - bounds[0] height = bounds[3] - bounds[1] d = Drawing(50, 10) d.add(barcode_eanbc8) renderPDF.draw(d, c, 15, 555) # draw the eanbc13 code barcode_eanbc13 = eanbc.Ean13BarcodeWidget(barcode_value) bounds = barcode_eanbc13.getBounds() width = bounds[2] - bounds[0] height = bounds[3] - bounds[1] d = Drawing(50, 10) d.add(barcode_eanbc13) renderPDF.draw(d, c, 15, 465) # draw a QR code qr_code = qr.QrCodeWidget( 'https://stackoverflow.com/questions/10147455/how-to-send-an-email-with-gmail-as-provider-using-python' ) bounds = qr_code.getBounds() width = bounds[2] - bounds[0] height = bounds[3] - bounds[1] d = Drawing(45, 45, transform=[45. / width, 0, 0, 45. / height, 0, 0]) d.add(qr_code) renderPDF.draw(d, c, 15, 405) c.save()
def drawABox(self, theBox, labelLines, barCodeLine=-1): """ draw the text contained in the string list (labelLines), one per line font size is computed to fit all the lines vertically, and then readjusted to fit inside the box horizontally. The lines are rendered top to bottom theBox = [bottom left x, bottom left y, width, height] in POINTS if barCodeLine is in [0, len(labelLines)-1], then this line is rendered as a barcode (Code 39) """ lineCount = len(labelLines) if (lineCount): # things to do if barCodeLine >= 0 and barCodeLine < lineCount: lineCount += 1 fontSize = theBox[3] / (1 + lineCount) margin = fontSize availableWidth = theBox[2] - margin * 2 self.canvas.setFontSize(fontSize - 1) self.canvas.saveState() p = self.canvas.beginPath() p.rect(theBox[0], theBox[1], theBox[2], theBox[3]) current_y = theBox[1] + theBox[3] - fontSize * 1.2 for lineIDX, aLine in enumerate(labelLines): stringWidth = self.canvas.stringWidth(aLine) if stringWidth > availableWidth: self.canvas.setFontSize( (fontSize - 1) * availableWidth / stringWidth) if lineIDX == barCodeLine: current_y -= fontSize ratio = 2.2 elementWidth = 7 + 2.2 * 3 barcode = code39.Standard39( aLine, barWidth=((availableWidth - 3) / elementWidth / (3 + len(aLine))), ratio=ratio, barHeight=2 * fontSize, humanReadable=False, quiet=True, lquiet=1.5, rquiet=1.5) barcode.drawOn(self.canvas, theBox[0] + margin, current_y) else: self.canvas.drawString(theBox[0] + margin, current_y, aLine) current_y -= fontSize self.canvas.setFontSize(fontSize - 1) self.canvas.restoreState()
def createBarCodes(): """ Create barcode examples and embed in a PDF """ c = canvas.Canvas("barcodes.pdf", pagesize=letter) barcode_value = "12345678999M" barcode39 = code39.Extended39(barcode_value) barcode39Std = code39.Standard39(barcode_value, barHeight=20, stop=1) # code93 also has an Extended and MultiWidth version barcode93 = code93.Standard93(barcode_value) barcode128 = code128.Code128(barcode_value) # the multiwidth barcode appears to be broken #barcode128Multi = code128.MultiWidthBarcode(barcode_value) barcode_usps = usps.POSTNET("50158-9999") codes = [barcode39, barcode39Std, barcode93, barcode128, barcode_usps] x = 1 * mm y = 260 * mm x1 = 6.4 * mm for code in codes: code.drawOn(c, x, y) y = y - 30 * mm barcode_value = "123456789990" # draw the eanbc8 code barcode_eanbc8 = eanbc.Ean8BarcodeWidget(barcode_value) bounds = barcode_eanbc8.getBounds() width = bounds[2] - bounds[0] height = bounds[3] - bounds[1] d = Drawing(50, 10) d.add(barcode_eanbc8) renderPDF.draw(d, c, 150 * mm, 150 * mm) # # draw the eanbc13 code barcode_eanbc13 = eanbc.Ean13BarcodeWidget(barcode_value) bounds = barcode_eanbc13.getBounds() width = bounds[2] - bounds[0] height = bounds[3] - bounds[1] d = Drawing(50, 10) d.add(barcode_eanbc13) renderPDF.draw(d, c, 150 * mm, 100 * mm) # # draw a QR code qr_code = qr.QrCodeWidget('www.ghf.com') bounds = qr_code.getBounds() width = bounds[2] - bounds[0] height = bounds[3] - bounds[1] d = Drawing(45, 45, transform=[45. / width, 0, 0, 45. / height, 0, 0]) d.add(qr_code) renderPDF.draw(d, c, 150 * mm, 50 * mm) c.save()
def _draw_barcode(self, canv: canvas, y: int, data: str, bar_height: int=20 * mm): # http://en.wikipedia.org/wiki/Code_39 barcode = code39.Standard39(data, barWidth=0.6 * mm, barHeight=bar_height, stop=True, checksum=False) barcode.drawOn(canv, self.inner_left + (self.inner_width - barcode.width) / 2, y) box_height = 4 * mm self._draw_textbox(canv, self.inner_left, y - 0.5 * mm - box_height, self.inner_width, box_height, data, style_name='data')
def createBarCodes(): #Unsure of the benefits downsides of using extended vs standard? if len(dfl('catalogNumber')) > 0: barcodeValue = dfl('catalogNumber') code39._Code39Base._humanText = newHumanText #Note, overriding the human text from this library to omit the stopcode ('+') barcode39Std = code39.Standard39(barcodeValue,barHeight=(yPaperSize * .10 ), barWidth=((xPaperSize * 0.28)/(len(barcodeValue)*13+35)), humanReadable=True, quiet = False, checksum=0) #^^^Note width is dynamic, but I don't know the significe of *13+35 beyond making it work. return barcode39Std else: return ''
def process(filename): barcodes = {} with open(filename, 'r') as f: barcode_reader = csv.reader(f, delimiter='\t') for record in barcode_reader: #print(record) if len(record) == 2: barcodes[record[0]] = code39.Standard39(record[1], barWidth=0.3 * mm, barHeight=20 * mm) return barcodes
def _flowable(self, node, extra_style=None): if node.tag == 'para': style = self.styles.para_style_get(node) if extra_style: style.__dict__.update(extra_style) result = [] for i in self._textual(node).split('\n'): result.append( platypus.Paragraph( i, style, **(utils.attr_get(node, [], {'bulletText': 'str'})))) return result elif node.tag == 'barCode': try: from reportlab.graphics.barcode import code128 from reportlab.graphics.barcode import code39 from reportlab.graphics.barcode import code93 from reportlab.graphics.barcode import common from reportlab.graphics.barcode import fourstate from reportlab.graphics.barcode import usps except Exception, e: return None args = utils.attr_get( node, [], { 'ratio': 'float', 'xdim': 'unit', 'height': 'unit', 'checksum': 'int', 'quiet': 'int', 'width': 'unit', 'stop': 'bool', 'bearers': 'int', 'barWidth': 'float', 'barHeight': 'float' }) codes = { 'codabar': lambda x: common.Codabar(x, **args), 'code11': lambda x: common.Code11(x, **args), 'code128': lambda x: code128.Code128(x, **args), 'standard39': lambda x: code39.Standard39(x, **args), 'standard93': lambda x: code93.Standard93(x, **args), 'i2of5': lambda x: common.I2of5(x, **args), 'extended39': lambda x: code39.Extended39(x, **args), 'extended93': lambda x: code93.Extended93(x, **args), 'msi': lambda x: common.MSI(x, **args), 'fim': lambda x: usps.FIM(x, **args), 'postnet': lambda x: usps.POSTNET(x, **args), } code = 'code128' if node.get('code'): code = node.get('code').lower() return codes[code](self._textual(node))
def _draw_barcode(self, canv: canvas, y: int, data: str, bar_height: int=20 * mm): # http://en.wikipedia.org/wiki/Code_39 barcode = code39.Standard39(data, barWidth=0.6 * mm, barHeight=bar_height, quiet=False, stop=True, checksum=False) if barcode.width > self.inner_width: raise LabelGenerationError('Generated barcode is too wide; {!s}'.format( {'available_mm': self.inner_width * mm, 'barcode_mm': barcode.width * mm})) barcode.drawOn(canv, self.inner_left + (self.inner_width - barcode.width) / 2, y) box_height = 4 * mm self._draw_textbox(canv, self.inner_left, y - 0.5 * mm - box_height, self.inner_width, box_height, data, style_name='data')
def append_requisition_items_story(self, story): story.append(Spacer(0.1 * cm, .5 * cm)) index = 0 for requisition in self.requisitions: table_data = [] if index == 0: table_data.append([ Paragraph('BARCODE', self.styles["line_label_center"]), Paragraph('ITEM', self.styles["line_label_center"]), Paragraph('SPECIMEN', self.styles["line_label_center"]), Paragraph('SUBJECT', self.styles["line_label_center"]), Paragraph('PANEL', self.styles["line_label_center"]), Paragraph('TYPE', self.styles["line_label_center"]), Paragraph('DRAWN', self.styles["line_label_center"]), Paragraph('RECV\'D', self.styles["line_label_center"]) ]) barcode = code39.Standard39(requisition.requisition_identifier, barHeight=10 * mm, stop=1) item_count = requisition.item_count or 1 for count in range(1, item_count + 1): table_data.append([ barcode, Paragraph(str(index + 1), self.styles['row_data']), Paragraph( f'{requisition.human_readable_identifier} ' f'({count}/{item_count})', self.styles['row_data']), Paragraph(requisition.subject_identifier or '?', self.styles['row_data']), Paragraph( f'{requisition.panel_object.verbose_name} <br />' f'({requisition.panel_object.abbreviation})', self.styles['row_data']), Paragraph(f'{requisition.panel_object.aliquot_type}', self.styles['row_data']), Paragraph(requisition.drawn_datetime.strftime('%Y-%m-%d'), self.styles['row_data']), Paragraph('', self.styles['row_data']) ]) index += 1 t1 = Table(table_data) t1.setStyle( TableStyle([('INNERGRID', (0, 0), (-1, -1), 0.25, colors.black), ('VALIGN', (0, 0), (-1, -1), 'MIDDLE'), ('BOX', (0, 0), (-1, -1), 0.25, colors.black)])) story.append(t1) return story
def _draw_barcode(self, canv, y, data, bar_height=20 * mm): # http://en.wikipedia.org/wiki/Code_39 #barcode = code39.Standard39(data, barWidth=0.5*mm, barHeight=bar_height, stop=True, checksum=True) barcode = code39.Standard39(data, barWidth=0.55 * mm, barHeight=bar_height, stop=True, checksum=False) barcode.drawOn( canv, self.inner_left + (self.inner_width - barcode.width) / 2, y) split_data = self._split_barcode_data(data, 3) canv.setFont('Helvetica', 10) canv.drawCentredString(self.inner_left + self.inner_width / 2, y - 4 * mm, split_data)
def code39_demo(barcode_value): doc = SimpleDocTemplate('code39_demo.pdf') styles = getSampleStyleSheet() flowables = [] flowables.append(Paragraph('Code 39 Standard:', style=styles['Normal'])) barcode39Std = code39.Standard39(barcode_value, barHeight=20, stop=1) flowables.append(barcode39Std) flowables.append(Spacer(0, 25)) flowables.append(Paragraph('Code 39 Extended:', style=styles['Normal'])) barcode39 = code39.Extended39(barcode_value) flowables.append(barcode39) doc.build(flowables)
def createBarCodes(c): barcode_value = '0112358' barcode39 = code39.Extended39(barcode_value) barcode39Std = code39.Standard39(barcode_value, barHeight=20, stop=1) barcode93 = code93.Standard93(barcode_value) barcode128 = code128.Code128(barcode_value) # barcode128Multi = code128.MultiWidthBarcode(barcode_value) barcode_usps = usps.POSTNET('50158-9999') codes = [barcode39, barcode39Std, barcode93, barcode128, barcode_usps] x = 1 * mm y = 285 * mm for code in codes: code.drawOn(c, x, y) y = y - 15 * mm barcode_eanbc8 = eanbc.Ean8BarcodeWidget(barcode_value) d = Drawing(50, 10) d.add(barcode_eanbc8) renderPDF.draw(d, c, 15, 555) barcode_eanbc13 = eanbc.Ean13BarcodeWidget(barcode_value) d = Drawing(50, 10) d.add(barcode_eanbc13) renderPDF.draw(d, c, 15, 465) qr_code = qr.QrCodeWidget('http://www.baidu.com') bounds = qr_code.getBounds() width = bounds[2] - bounds[0] height = bounds[3] - bounds[1] d = Drawing(45, 45, transform=[45./width, 0,0,45./height, 0, 0]) d.add(qr_code) renderPDF.draw(d, c, 15, 405)
def create(self): random_no = random.randint(1000000, 9999999) filename = str(random_no) + ".pdf" filepath = TEMP_FILE_PATH filenamepath = filepath + filename if AUTO_PRINT: from reportlab.pdfbase import pdfdoc pdfdoc.PDFCatalog.OpenAction = '<</S/JavaScript/JS(this.print\({bUI:true,bSilent:false,bShrinkToFit:true}\);)>>' pdfdoc.PDFInfo.title = 'Label ' + filename c = canvas.Canvas(filenamepath, pagesize=A4) # GODSFLAGGA x_margin = 2 * mm # lines c.setLineWidth(1) lines = [ (3 * mm, 47 * mm + 158 * mm, 206 * mm, 47 * mm + 158 * mm), (143 * mm, 59 * mm + 158 * mm, 206 * mm, 59 * mm + 158 * mm), (3 * mm, 71 * mm + 158 * mm, 206 * mm, 71 * mm + 158 * mm), (3 * mm, 98 * mm + 158 * mm, 206 * mm, 98 * mm + 158 * mm), (143 * mm, 47 * mm + 158 * mm, 143 * mm, 71 * mm + 158 * mm), (114 * mm, 71 * mm + 158 * mm, 114 * mm, 98 * mm + 158 * mm), (3 * mm, 47 * mm, 206 * mm, 47 * mm), (143 * mm, 59 * mm, 206 * mm, 59 * mm), (3 * mm, 71 * mm, 206 * mm, 71 * mm), (3 * mm, 98 * mm, 206 * mm, 98 * mm), (143 * mm, 47 * mm, 143 * mm, 71 * mm), (114 * mm, 71 * mm, 114 * mm, 98 * mm), ] c.lines(lines) # PARTNO # label label = "PART NO.(P)" c.setFont("Helvetica", 10) x = 3 * mm + x_margin y = 122 * mm c.drawString(x, y, label) y = 122 * mm + 158 * mm c.drawString(x, y, label) # data data = self.part_no c.setFont("Helvetica-Bold", 50) x = 30 * mm y = 113 * mm c.drawString(x, y, data) y = 113 * mm + 158 * mm c.drawString(x, y, data) # barcode barcode = code39.Standard39('P' + data, barWidth=0.50 * mm, barHeight=12 * mm, checksum=0) x = 0 * mm y = 100 * mm barcode.drawOn(c, x, y) y = 100 * mm + 158 * mm barcode.drawOn(c, x, y) # QUANTITY # label label = "QUANTITY (Q)" c.setFont("Helvetica", 10) x = 3 * mm + x_margin y = 94 * mm c.drawString(x, y, label) y = 94 * mm + 158 * mm c.drawString(x, y, label) # data data = self.quantity c.setFont("Helvetica-Bold", 48) x = 33 * mm y = 85 * mm c.drawString(x, y, data) y = 85 * mm + 158 * mm c.drawString(x, y, data) # barcode barcode = code39.Standard39('Q' + data, barWidth=0.50 * mm, barHeight=12 * mm, checksum=0) x = 0 * mm y = 72 * mm barcode.drawOn(c, x, y) y = 72 * mm + 158 * mm barcode.drawOn(c, x, y) # VENDOR # label label = "VENDOR LOT (1T)" c.setFont("Helvetica", 10) x = 3 * mm + x_margin y = 67 * mm c.drawString(x, y, label) y = 67 * mm + 158 * mm c.drawString(x, y, label) # data data = self.vendor_lot c.setFont("Helvetica-Bold", 36) x = 41 * mm y = 61 * mm c.drawString(x, y, data) y = 61 * mm + 158 * mm c.drawString(x, y, data) # barcode barcode = code39.Standard39('1T' + data, barWidth=0.50 * mm, barHeight=12 * mm, checksum=0) x = 0 * mm y = 48 * mm barcode.drawOn(c, x, y) y = 48 * mm + 158 * mm barcode.drawOn(c, x, y) # SERIAL # label label = "SERIAL (1S)" c.setFont("Helvetica", 10) x = 3 * mm + x_margin y = 43 * mm c.drawString(x, y, label) y = 43 * mm + 158 * mm c.drawString(x, y, label) # data data = self.vendor_number c.setFont("Helvetica-Bold", 32) x = 27 * mm y = 36 * mm c.drawString(x, y, data) y = 36 * mm + 158 * mm c.drawString(x, y, data) # data data = '00000' + self.serial c.setFont("Helvetica-Bold", 32) x = 87 * mm y = 36 * mm c.drawString(x, y, data) y = 36 * mm + 158 * mm c.drawString(x, y, data) # DESCRIPTION # label label = "DESCRIPTION" c.setFont("Helvetica", 10) x = 117 * mm y = 94 * mm c.drawString(x, y, label) y = 94 * mm + 158 * mm c.drawString(x, y, label) # data data = self.description c.setFont("Helvetica-Bold", 24) x = 118 * mm y = 76 * mm c.drawString(x, y, data) y = 76 * mm + 158 * mm c.drawString(x, y, data) # DATE # label label = "MFG DATE" c.setFont("Helvetica", 10) x = 145 * mm y = 67 * mm c.drawString(x, y, label) y = 67 * mm + 158 * mm c.drawString(x, y, label) # data data = self.date c.setFont("Helvetica-Bold", 24) x = 145 * mm y = 60 * mm c.drawString(x, y, data) y = 60 * mm + 158 * mm c.drawString(x, y, data) # PO NUMBER # label label = "PO NUMBER" c.setFont("Helvetica", 10) x = 145 * mm y = 55 * mm c.drawString(x, y, label) y = 55 * mm + 158 * mm c.drawString(x, y, label) # data data = self.po_number c.setFont("Helvetica-Bold", 24) x = 145 * mm y = 48 * mm c.drawString(x, y, data) y = 48 * mm + 158 * mm c.drawString(x, y, data) # COLOPHONE label = COLOPHONE c.setFont("Helvetica", 12) x = 3 * mm + x_margin y = 19 * mm c.drawString(x, y, label) y = 19 * mm + 158 * mm c.drawString(x, y, label) c.showPage() c.save() return { 'FILENAME': filename, 'FILEPATH': filepath, 'FILENAMEPATH': filenamepath }
def _flowable(self, node, extra_style=None): if node.tag=='pto': return self._pto(node) if node.tag=='para': style = self.styles.para_style_get(node) if extra_style: style.__dict__.update(extra_style) result = [] for i in self._textual(node).split('\n'): result.append(platypus.Paragraph(i, style, **(utils.attr_get(node, [], {'bulletText':'str'})))) return result elif node.tag=='barCode': try: from reportlab.graphics.barcode import code128 from reportlab.graphics.barcode import code39 from reportlab.graphics.barcode import code93 from reportlab.graphics.barcode import common from reportlab.graphics.barcode import fourstate from reportlab.graphics.barcode import usps from reportlab.graphics.barcode import createBarcodeDrawing except ImportError: _logger.warning("Cannot use barcode renderers:", exc_info=True) return None args = utils.attr_get(node, [], {'ratio':'float','xdim':'unit','height':'unit','checksum':'int','quiet':'int','width':'unit','stop':'bool','bearers':'int','barWidth':'float','barHeight':'float'}) codes = { 'codabar': lambda x: common.Codabar(x, **args), 'code11': lambda x: common.Code11(x, **args), 'code128': lambda x: code128.Code128(str(x), **args), 'standard39': lambda x: code39.Standard39(str(x), **args), 'standard93': lambda x: code93.Standard93(str(x), **args), 'i2of5': lambda x: common.I2of5(x, **args), 'extended39': lambda x: code39.Extended39(str(x), **args), 'extended93': lambda x: code93.Extended93(str(x), **args), 'msi': lambda x: common.MSI(x, **args), 'fim': lambda x: usps.FIM(x, **args), 'postnet': lambda x: usps.POSTNET(x, **args), 'ean13': lambda x: createBarcodeDrawing('EAN13', value=str(x), **args), 'qrcode': lambda x: createBarcodeDrawing('QR', value=x, **args), } code = 'code128' if node.get('code'): code = node.get('code').lower() return codes[code](self._textual(node)) elif node.tag=='name': self.styles.names[ node.get('id')] = node.get('value') return None elif node.tag=='xpre': style = self.styles.para_style_get(node) return platypus.XPreformatted(self._textual(node), style, **(utils.attr_get(node, [], {'bulletText':'str','dedent':'int','frags':'int'}))) elif node.tag=='pre': style = self.styles.para_style_get(node) return platypus.Preformatted(self._textual(node), style, **(utils.attr_get(node, [], {'bulletText':'str','dedent':'int'}))) elif node.tag=='illustration': return self._illustration(node) elif node.tag=='blockTable': return self._table(node) elif node.tag=='title': styles = reportlab.lib.styles.getSampleStyleSheet() style = styles['Title'] return platypus.Paragraph(self._textual(node), style, **(utils.attr_get(node, [], {'bulletText':'str'}))) elif re.match('^h([1-9]+[0-9]*)$', (node.tag or '')): styles = reportlab.lib.styles.getSampleStyleSheet() style = styles['Heading'+str(node.tag[1:])] return platypus.Paragraph(self._textual(node), style, **(utils.attr_get(node, [], {'bulletText':'str'}))) elif node.tag=='image': image_data = False if not node.get('file'): if node.get('name'): if node.get('name') in self.doc.images: _logger.debug("Image %s read ", node.get('name')) image_data = self.doc.images[node.get('name')].read() else: _logger.warning("Image %s not defined", node.get('name')) return False else: import base64 newtext = node.text if self.localcontext: newtext = utils._process_text(self, node.text or '') image_data = base64.decodestring(newtext) if not image_data: _logger.debug("No inline image data") return False image = StringIO(image_data) else: _logger.debug("Image get from file %s", node.get('file')) image = _open_image(node.get('file'), path=self.doc.path) return platypus.Image(image, mask=(250,255,250,255,250,255), **(utils.attr_get(node, ['width','height']))) elif node.tag=='spacer': if node.get('width'): width = utils.unit_get(node.get('width')) else: width = utils.unit_get('1cm') length = utils.unit_get(node.get('length')) return platypus.Spacer(width=width, height=length) elif node.tag=='section': return self.render(node) elif node.tag == 'pageNumberReset': return PageReset() elif node.tag in ('pageBreak', 'nextPage'): return platypus.PageBreak() elif node.tag=='condPageBreak': return platypus.CondPageBreak(**(utils.attr_get(node, ['height']))) elif node.tag=='setNextTemplate': return platypus.NextPageTemplate(str(node.get('name'))) elif node.tag=='nextFrame': return platypus.CondPageBreak(1000) # TODO: change the 1000 ! elif node.tag == 'setNextFrame': from reportlab.platypus.doctemplate import NextFrameFlowable return NextFrameFlowable(str(node.get('name'))) elif node.tag == 'currentFrame': from reportlab.platypus.doctemplate import CurrentFrameFlowable return CurrentFrameFlowable(str(node.get('name'))) elif node.tag == 'frameEnd': return EndFrameFlowable() elif node.tag == 'hr': width_hr=node.get('width') or '100%' color_hr=node.get('color') or 'black' thickness_hr=node.get('thickness') or 1 lineCap_hr=node.get('lineCap') or 'round' return platypus.flowables.HRFlowable(width=width_hr,color=color.get(color_hr),thickness=float(thickness_hr),lineCap=str(lineCap_hr)) else: sys.stderr.write('Warning: flowable not yet implemented: %s !\n' % (node.tag,)) return None
def run(): c = reportlab.pdfgen.canvas.Canvas('barcodetest.pdf', pagesize=pagesizes.A4) framePage(c, 'Hebi-No-Shisho Barcode Test Page' ) y = 700 yd = 50 xb = 40 xs = 240 y = y-yd mybarcode = code39.Standard39(barWidth=inch * 0.010, value="CD3910", checksum=0) mybarcode.drawOn(c, xb, y) c.drawString(xs, y, '%s - Standard39 10 mil' % mybarcode.encoded) y = y-yd mybarcode = code39.Standard39(barWidth=inch * 0.015, value="CD3915", checksum=0) mybarcode.drawOn(c, xb, y) c.drawString(xs, y, '%s - Standard39 15 mil' % mybarcode.encoded) y = y-yd mybarcode = code39.Standard39(barWidth=inch * 0.020, value="CD3930", checksum=0) mybarcode.drawOn(c, xb, y) c.drawString(xs, y, '%s - Standard39 20 mil' % mybarcode.encoded) y = y-yd mybarcode = code93.Standard93(barWidth=inch * 0.010, value="CD9310") mybarcode.drawOn(c, xb, y) c.drawString(xs, y, '%s - Standard93 10 mil' % mybarcode.encoded) y = y-yd mybarcode = code93.Standard93(barWidth=inch * 0.015, value="CD9315") mybarcode.drawOn(c, xb, y) c.drawString(xs, y, '%s - Standard93 15 mil' % mybarcode.encoded) y = y-yd mybarcode = code93.Standard93(barWidth=inch * 0.020, value="CD9320") mybarcode.drawOn(c, xb, y) c.drawString(xs, y, '%s - Standard93 20 mil' % mybarcode.encoded) y = y-yd mybarcode = code128.Code128(barWidth=inch * 0.010, value="C12810") mybarcode.drawOn(c, xb, y) c.drawString(xs, y, '%s - Standard128 10 mil' % mybarcode.value) y = y-yd mybarcode = code128.Code128(barWidth=inch * 0.015, value="C12815") mybarcode.drawOn(c, xb, y) c.drawString(xs, y, '%s - Standard128 15 mil' % mybarcode.value) y = y-yd mybarcode = code128.Code128(barWidth=inch * 0.020, value="C12820") mybarcode.drawOn(c, xb, y) c.drawString(xs, y, '%s - Standard128 20 mil' % mybarcode.value) y = y-yd-10 mydrawing = Drawing() mybarcode = eanbc.Ean13BarcodeWidget(barHeight=inch * 0.5, barWidth=inch * 0.015, value="123456789012") mydrawing.add(mybarcode) mydrawing.drawOn(c, xb, y) c.drawString(xs, y, 'EAN13') c.save()
def createBarCodes(): """ Create barcode examples and embed in a PDF """ c = canvas.Canvas("barcodes.pdf", pagesize=letter) barcode_value = [ '0004-0000000001', '0004-0000000002', '0004-0000000003', '0004-0000000004', '0004-0000000005', '0004-0000000013', '0004-0000000028', '0004-0000000029', '0004-0000000030', '0004-0000000031', ] #barcode_value = "1234567890" barcode_value = "0004-0000000001" barcode39 = code39.Extended39(barcode_value, humanReadable=True) barcode39Std = code39.Standard39(barcode_value, barHeight=20, stop=1, humanReadable=True) # code93 also has an Extended and MultiWidth version barcode93 = code93.Standard93(barcode_value, humanReadable=True) barcode128 = code128.Code128(barcode_value, humanReadable=True) # the multiwidth barcode appears to be broken #barcode128Multi = code128.MultiWidthBarcode(barcode_value) barcode_usps = usps.POSTNET("50158-9999") codes = [barcode39, barcode39Std, barcode93, barcode128, barcode_usps] x = 1 * mm y = 285 * mm x1 = 6.4 * mm for code in codes: code.drawOn(c, x, y) y = y - 15 * mm # draw the eanbc8 code """ barcode_eanbc8 = eanbc.Ean8BarcodeWidget(barcode_value) bounds = barcode_eanbc8.getBounds() width = bounds[2] - bounds[0] height = bounds[3] - bounds[1] d = Drawing(50, 10) d.add(barcode_eanbc8) renderPDF.draw(d, c, 15, 555) """ # draw the eanbc13 code """ barcode_eanbc13 = eanbc.Ean13BarcodeWidget(barcode_value) bounds = barcode_eanbc13.getBounds() width = bounds[2] - bounds[0] height = bounds[3] - bounds[1] d = Drawing(50, 10) d.add(barcode_eanbc13) renderPDF.draw(d, c, 15, 465) """ c.save()
def generateFiche(pdf_filename, vars, size=(115 * mm, 85 * mm)): sizeX = size[0] sizeY = size[1] midX = size[0] / 2 midY = size[1] / 2 marginX = 10 marginY = 10 cnv = canvas.Canvas(pdf_filename, pagesize=size, verbosity=1) # 115 x 85 mm # Bordure pour le massicot cnv.rect(0, 0, sizeX, sizeY, stroke=1, fill=0) # Styles pour les paragraphes styleSheet = getSampleStyleSheet() style = styleSheet['BodyText'] style_obs = ParagraphStyle(name='Obs', fontSize=9, leading=9, fontName='Helvetica-Oblique') style_usuel = ParagraphStyle(name='SynoUsuel', fontSize=12, alignment=1, leading=12) # Genre - Espece style_nomenc = ParagraphStyle(name='Nomenclature', fontSize=18, alignment=1, fontName='Helvetica-Bold', leading=22) # Variete - Forme style_nomenc_bis = ParagraphStyle(name='NomenclatureBis', fontSize=14, alignment=1, leading=11, fontName='Helvetica-Bold') # Noms style_noms = ParagraphStyle(name='Noms', fontSize=12, leading=12, fontName='Helvetica-Bold') # Theme if 'theme' in vars.keys(): cnv.drawString(marginX, sizeY - (marginY + 10), vars['theme']) # Numero de fiche cnv.drawRightString(sizeX - marginX, sizeY - (marginY + 10), vars['fiche']) # Rectangle supérieur cnv.setLineWidth(2) frame_top = Frame(0 + marginX, midY + (4.5 * marginY) + 5, sizeX - (2 * marginX), sizeY / 5, showBoundary=1, topPadding=2, leftPadding=2, rightPadding=2, bottomPadding=2) nomenc = [] # Genre et Espece nomenc.append(Paragraph(vars['genre'] + ' ' + vars['espece'], style_nomenc)) # Variete et Forme if vars['variete'] != "": nomenc.append(Paragraph('var. ' + vars['variete'], style_nomenc_bis)) elif vars['forme'] != "": nomenc.append(Paragraph('f. ' + vars['forme'], style_nomenc_bis)) nomenc_inframe = KeepInFrame(sizeX - (2 * marginX), sizeY / 5, nomenc) frame_top.addFromList([nomenc_inframe], cnv) cnv.setLineWidth(1) # Noms if 'noms' in vars.keys() and vars['noms'] != "": frameW = sizeX * 0.65 frameH = marginY * 7 frame_noms = Frame(marginX, midY - 4.5 * marginY, frameW, frameH, showBoundary=0) noms = [] for nom in vars['noms'].splitlines(): if nom != "": noms.append(Paragraph(nom, style_noms)) noms_inframe = KeepInFrame(frameW, frameH, noms) frame_noms.addFromList([noms_inframe], cnv) # Synonyme usuel if 'usuel_genre' in vars.keys(): frameW = 0.66 * sizeX frameH = marginY * 3.5 frame_syno = Frame(0.165 * sizeX, midY + 2 * marginY, frameW, frameH, showBoundary=0) if vars['usuel_variete'] != "": varfor_str = ' var. ' + vars['usuel_variete'] elif vars['usuel_forme'] != "": varfor_str = ' f. ' + vars['usuel_forme'] else: varfor_str = '' ustr = '( = ' + vars['usuel_genre'] + ' ' + vars[ 'usuel_espece'] + varfor_str + ' )' usuel = [Paragraph(ustr, style_usuel)] usuel_inframe = KeepInFrame(frameW, frameH, usuel) frame_syno.addFromList([usuel_inframe], cnv) # Texte comestibilite if 'comestibilite' in vars.keys() and vars['comestibilite'] != "": assets_path = settings.BASE_DIR + '/app/pdf_assets/' if (vars['comestibilite'] == 'C'): com_txt = 'Comestible' com_img = assets_path + 'Comest.bmp' elif (vars['comestibilite'] == 'NC'): com_txt = 'Non Comestible' com_img = assets_path + 'NonCom.bmp' elif (vars['comestibilite'] == 'T'): com_txt = 'Toxique' com_img = assets_path + 'Toxique.bmp' else: com_txt = 'Mortel' com_img = assets_path + 'Mortel.bmp' img_width = 138.75 / 2.5 img_height = 131.25 / 2.5 cnv.drawImage(com_img, sizeX - marginX - img_width, midY - (1.5 * marginY), width=img_width, height=img_width) cnv.drawRightString(sizeX - marginX, midY - (2 * marginY) - 5, com_txt) # Rectangle du bas cnv.setLineWidth(1) frameW = sizeX - (2 * marginX) frameH = sizeY / 5 frame_obs = Frame(0 + marginX, 5 + marginY, frameW, frameH, showBoundary=1, leftPadding=2, topPadding=2, rightPadding=2, bottomPadding=2) # Observations if 'obs' in vars.keys(): observations = [ Paragraph(vars['obs'][0], style_obs), Paragraph(vars['obs'][1], style_obs) ] obs_inframe = KeepInFrame(frameW, frameH, observations) frame_obs.addFromList([obs_inframe], cnv) # Copyright cnv.setFont('Helvetica', 8) year = str(datetime.datetime.now().year) cnv.drawRightString(sizeX - marginX, marginY - 5, '©SMS ' + year) # Code Barre (standard code39) barcode = code39.Standard39(vars['taxon'], barWidth=0.5 * mm, barHeight=3.5 * mm, checksum=0) barcode.drawOn(cnv, 10, 3) cnv.showPage() cnv.save()
def generateFicheTheme(pdf_filename, vars, size=(325.984, 240.945)): sizeX = size[0] sizeY = size[1] midX = size[0] / 2 midY = size[1] / 2 marginX = 10 marginY = 10 cnv = canvas.Canvas(pdf_filename, pagesize=size, verbosity=1) # 115 x 85 mm style_eco = ParagraphStyle(name='ObsEco', fontSize=10, leading=10, fontName='Helvetica-Oblique') style_notes = ParagraphStyle(name='ObsNotes', fontSize=9, leading=9, fontName='Helvetica-Bold') style_titre = ParagraphStyle(name='ThemeTitre', fontName='Helvetica-Bold', fontSize=14, alignment=1, textColor=colors.Color( 239 / 255, 239 / 255, 0, 1)) style_nomenc = ParagraphStyle(name='Nomenc', fontName='Helvetica-BoldOblique', fontSize=12) style_nomenc_bis = ParagraphStyle(name='Nomenc', fontName='Helvetica-Oblique', fontSize=10) style_noms = ParagraphStyle(name='Noms', fontSize=16, alignment=1, fontName='Helvetica-Bold', leading=16) # Rectangle du haut cnv.setStrokeColorRGB(80 / 255, 80 / 255, 80 / 255) cnv.setLineWidth(2) cnv.setFillColorRGB(80 / 255, 80 / 255, 80 / 255) cnv.rect(0, sizeY - (10 * mm), sizeX, 10 * mm, fill=True) # Bordure pour le massicot cnv.setStrokeColorRGB(0, 0, 0) cnv.setLineWidth(1) cnv.rect(0, 0, sizeX, sizeY, stroke=1, fill=0) # Numero de fiche cnv.setStrokeColorRGB(0, 0, 0) cnv.setFillColorRGB(1, 1, 1) cnv.drawRightString(sizeX - marginX, sizeY - (marginY + 10), vars['fiche']) # Theme cnv.drawString(marginX, sizeY - (marginY + 10), vars['theme_code']) cnv.setFillColorRGB(0, 0, 0) frameW = sizeX - (9 * marginX) frameH = marginY * 2.5 frame_titre = Frame(4.5 * marginX, sizeY - marginY * 2.7, frameW, frameH, showBoundary=0, leftPadding=2, topPadding=2, rightPadding=2, bottomPadding=2) titre = [Paragraph(vars['theme_titre'], style_titre)] titre_inframe = KeepInFrame(frameW, frameH, titre) frame_titre.addFromList([titre_inframe], cnv) # Genre et Espece cnv.setFillColorRGB(0, 0, 0) frameW = 0.9 * sizeX frameH = 3 * marginY frame_nomenc = Frame(marginX * 0.6, sizeY - (10 * mm) - frameH, frameW, frameH, showBoundary=0, leftPadding=2, topPadding=2, rightPadding=2, bottomPadding=2) nomenc = [] # Genre et Espece genre_esp = vars['genre'] + ' ' + vars['espece'] if vars['variete'] != "": genre_esp += ' var. ' + vars['variete'] elif vars['forme'] != "": genre_esp += ' f. ' + vars['forme'] nomenc.append(Paragraph(genre_esp, style_nomenc)) if 'usuel_genre' in vars.keys(): nomenc.append( Paragraph( '( = ' + vars['usuel_genre'] + ' ' + vars['usuel_espece'] + ' ) ', style_nomenc_bis)) if vars['usuel_variete'] != "": nomenc.append( Paragraph('var. ' + vars['usuel_variete'], style_nomenc_bis)) elif vars['usuel_forme'] != "": nomenc.append( Paragraph('f. ' + vars['usuel_forme'], style_nomenc_bis)) nomenc_inframe = KeepInFrame(frameW, frameH, nomenc) frame_nomenc.addFromList([nomenc_inframe], cnv) cnv.setFont('Helvetica', 11) # Noms usuels if 'noms' in vars.keys() and vars['noms'] != "": frameW = sizeX * 0.75 frameH = marginY * 6 frame_noms = Frame(marginX, midY - 3 * marginY, frameW, frameH, showBoundary=0, leftPadding=2, topPadding=2, rightPadding=2, bottomPadding=2) noms = [] for nom in vars['noms'].splitlines(): if nom != "": noms.append(Paragraph(nom, style_noms)) noms_inframe = KeepInFrame(frameW, frameH, noms) frame_noms.addFromList([noms_inframe], cnv) # Texte comestibilite if 'comestibilite' in vars.keys() and vars['comestibilite'] != "": assets_path = settings.BASE_DIR + '/app/pdf_assets/' if (vars['comestibilite'] == 'C'): com_txt = 'Comestible' com_img = assets_path + 'Comest.bmp' elif (vars['comestibilite'] == 'NC'): com_txt = 'Non Comestible' com_img = assets_path + 'NonCom.bmp' elif (vars['comestibilite'] == 'T'): com_txt = 'Toxique' com_img = assets_path + 'Toxique.bmp' else: com_txt = 'Mortel' com_img = assets_path + 'Mortel.bmp' img_width = 138.75 / 2.5 img_height = 131.25 / 2.5 cnv.drawImage(com_img, sizeX - marginX - img_width, midY + (0.5 * marginY), width=img_width, height=img_width) cnv.drawRightString(sizeX - marginX, midY - (0 * marginY) - 5, com_txt) # Rectangle du bas cnv.setLineWidth(1) frame_obs = Frame(0 + marginX, 5 + marginY, sizeX - (2 * marginX), sizeY / 6, showBoundary=1, leftPadding=2, topPadding=2, rightPadding=2, bottomPadding=2) # Observations if 'obs' in vars.keys(): notes = [Paragraph(vars['obs'][0], style_notes)] eco = [Paragraph(vars['obs'][1], style_eco)] frame_eco = Frame(0 + marginX, 5 + (1 * marginY) + sizeY / 6, sizeX - (2 * marginX), 2.6 * marginY, showBoundary=0, leftPadding=2, topPadding=2, rightPadding=2, bottomPadding=2) notes_inframe = KeepInFrame(sizeX - (2 * marginX), sizeY / 6, notes) eco_inframe = KeepInFrame(sizeX - (2 * marginX), 2.6 * marginY, eco) frame_obs.addFromList([notes_inframe], cnv) frame_eco.addFromList([eco_inframe], cnv) # Copyright cnv.setFont('Helvetica', 8) year = str(datetime.datetime.now().year) cnv.drawRightString(sizeX - marginX, marginY - 5, '©SMS ' + year) # Code Barre (standard code39) barcode = code39.Standard39(vars['taxon'], barWidth=0.5 * mm, barHeight=3.5 * mm, checksum=0) barcode.drawOn(cnv, 10, 3) cnv.showPage() cnv.save()
def append_requisition_items_story(self, story): story.append(Spacer(0.1 * cm, 0.5 * cm)) index = 0 for requisition in self.requisitions: table_data = [] if index == 0: table_data.append( [ Paragraph("BARCODE", self.styles["line_label_center"]), Paragraph("ITEM", self.styles["line_label_center"]), Paragraph("SPECIMEN", self.styles["line_label_center"]), Paragraph("SUBJECT", self.styles["line_label_center"]), Paragraph("PANEL", self.styles["line_label_center"]), Paragraph("TYPE", self.styles["line_label_center"]), Paragraph("DRAWN", self.styles["line_label_center"]), Paragraph("RECV'D", self.styles["line_label_center"]), ] ) barcode = code39.Standard39( requisition.requisition_identifier, barHeight=10 * mm, stop=1 ) item_count = requisition.item_count or 1 for count in range(1, item_count + 1): table_data.append( [ barcode, Paragraph(str(index + 1), self.styles["row_data"]), Paragraph( f"{requisition.human_readable_identifier} " f"({count}/{item_count})", self.styles["row_data"], ), Paragraph( requisition.subject_identifier or "?", self.styles["row_data"], ), Paragraph( f"{requisition.panel_object.verbose_name} <br />" f"({requisition.panel_object.abbreviation})", self.styles["row_data"], ), Paragraph( f"{requisition.panel_object.aliquot_type}", self.styles["row_data"], ), Paragraph( requisition.drawn_datetime.strftime("%Y-%m-%d"), self.styles["row_data"], ), Paragraph("", self.styles["row_data"]), ] ) index += 1 t1 = Table(table_data) t1.setStyle( TableStyle( [ ("INNERGRID", (0, 0), (-1, -1), 0.25, colors.black), ("VALIGN", (0, 0), (-1, -1), "MIDDLE"), ("BOX", (0, 0), (-1, -1), 0.25, colors.black), ] ) ) story.append(t1) return story
def append_manifest_items_story(self, story): box_header = [ Paragraph("BOX:", self.styles["line_label"]), Paragraph("CATEGORY:", self.styles["line_label"]), Paragraph("TYPES:", self.styles["line_label"]), Paragraph("ITEMS:", self.styles["line_label"]), Paragraph("BOX DATE:", self.styles["line_label"]), Paragraph("BOX BARCODE:", self.styles["line_label"]), ] for index, manifest_item in enumerate( self.manifest.manifestitem_set.all().order_by("-created")): if index > 0: story.append(Spacer(0.1 * cm, 0.5 * cm)) data1 = [] data1.append(box_header) try: box = self.box_model.objects.get( box_identifier=manifest_item.identifier) except ObjectDoesNotExist as e: raise ManifestReportError( f"{e} Got Manifest item '{manifest_item.identifier}'.", code="unboxed_item", ) from e barcode = code39.Standard39(box.box_identifier, barHeight=5 * mm, stop=1) data1.append([ Paragraph(box.box_identifier, self.styles["line_data_large"]), Paragraph( box.get_category_display().upper(), self.styles["line_data_large"], ), Paragraph(box.specimen_types, self.styles["line_data_large"]), Paragraph( f"{str(box.count)}/{str(box.box_type.total)}", self.styles["line_data_large"], ), Paragraph( box.box_datetime.strftime("%Y-%m-%d"), self.styles["line_data_large"], ), barcode, ]) t1 = Table(data1, colWidths=(None, None, None, None, None, None)) t1.setStyle( TableStyle([ ("INNERGRID", (0, 0), (-1, 0), 0.25, colors.black), ("INNERGRID", (0, 1), (-1, -1), 0.25, colors.black), ("BOX", (0, 0), (-1, -1), 0.25, colors.black), ])) story.append(t1) story.append(Spacer(0.1 * cm, 0.5 * cm)) table_data = [] table_data = [[ Paragraph("BARCODE", self.styles["line_label_center"]), Paragraph("POS", self.styles["line_label_center"]), Paragraph("ALIQUOT IDENTIFIER", self.styles["line_label_center"]), Paragraph("SUBJECT", self.styles["line_label_center"]), Paragraph("TYPE", self.styles["line_label_center"]), Paragraph("DATE", self.styles["line_label_center"]), ]] for box_item in box.boxitem_set.all().order_by("position"): aliquot = self.get_aliquot(box_item.identifier) panel_object = self.get_panel_object(aliquot) barcode = code39.Standard39(aliquot.aliquot_identifier, barHeight=5 * mm, stop=1) table_data.append([ barcode, Paragraph(str(box_item.position), self.styles["row_data"]), Paragraph(aliquot.human_readable_identifier, self.styles["row_data"]), Paragraph(aliquot.subject_identifier, self.styles["row_data"]), Paragraph( "{} ({}) {}".format( aliquot.aliquot_type, aliquot.numeric_code, panel_object.abbreviation, ), self.styles["row_data"], ), Paragraph( aliquot.aliquot_datetime.strftime("%Y-%m-%d"), self.styles["row_data"], ), ]) t1 = Table(table_data) t1.setStyle( TableStyle([ ("INNERGRID", (0, 0), (-1, -1), 0.25, colors.black), ("VALIGN", (0, 0), (-1, -1), "MIDDLE"), ("BOX", (0, 0), (-1, -1), 0.25, colors.black), ])) story.append(t1) return story
def get_report_story(self, **kwargs): story = [] data = [ [ Paragraph( " ".join(self.manifest.site.name.split("_")).upper(), self.styles["line_data_large"], ) ], [Paragraph("SITE NAME", self.styles["line_label"])], [ Paragraph( self.formatted_address(**self.manifest.shipper.__dict__), self.styles["line_data_large"], ) ], [Paragraph("SITE DETAILS", self.styles["line_label"])], ] t = Table(data, colWidths=(9 * cm)) t.setStyle( TableStyle([ ("INNERGRID", (0, 0), (0, 1), 0.25, colors.black), ("INNERGRID", (0, 2), (0, 3), 0.25, colors.black), ])) t.hAlign = "RIGHT" story.append(t) story.append(Spacer(0.1 * cm, 0.5 * cm)) story.append( Paragraph( "SPECIMEN MANIFEST{}".format( " (reprint)" if self.manifest.printed else ""), self.styles["line_label_center"], )) if self.manifest.shipped: barcode = code39.Standard39(self.manifest.manifest_identifier, barHeight=10 * mm, stop=1) else: barcode = "PREVIEW" data = [[ Paragraph("MANIFEST NO.", self.styles["line_label"]), Paragraph( (self.manifest.human_readable_identifier if self.manifest.shipped else "PREVIEW"), self.styles["line_data_largest"], ), barcode, ]] t = Table(data, colWidths=(3 * cm, None, 5.5 * cm)) t.setStyle( TableStyle([ ("INNERGRID", (1, 0), (-1, -1), 0.25, colors.black), ("BOX", (0, 0), (-1, -1), 0.25, colors.black), ("VALIGN", (0, 0), (-1, -1), "MIDDLE"), ])) story.append(t) data = [ [ Paragraph("MANIFEST DATE", self.styles["line_label"]), Paragraph("EXPORT REFERENCES", self.styles["line_label"]), ], [ Paragraph( self.manifest.manifest_datetime.strftime("%Y-%m-%d"), self.styles["line_data_largest"], ), Paragraph( self.manifest.export_references or "", self.styles["line_data_largest"], ), ], ] t = Table(data) t.setStyle( TableStyle([ ("INNERGRID", (0, 0), (1, 0), 0.25, colors.black), ("INNERGRID", (0, 1), (1, 1), 0.25, colors.black), ("BOX", (0, 0), (-1, -1), 0.25, colors.black), ("VALIGN", (0, 0), (-1, -1), "MIDDLE"), ])) story.append(t) data = [ [ Paragraph( "SHIPPER/EXPORTER (complete name and address)", self.styles["line_label"], ), Paragraph("CONSIGNEE (complete name and address)", self.styles["line_label"]), ], [ Paragraph( self.formatted_address(**self.shipper_data), self.styles["line_data_large"], ), Paragraph( self.formatted_address(**self.consignee_data), self.styles["line_data_large"], ), ], ] t = Table(data) t.setStyle( TableStyle([ ("INNERGRID", (0, 0), (1, 0), 0.25, colors.black), ("INNERGRID", (0, 1), (1, 1), 0.25, colors.black), ("BOX", (0, 0), (-1, -1), 0.25, colors.black), ("VALIGN", (0, 0), (-1, -1), "MIDDLE"), ])) story.append(t) data1 = [ [ Paragraph("COUNTRY OF EXPORT", self.styles["line_label"]), Paragraph( "DESCRIPTION OF GOODS (description and special instructions)<br />", self.styles["line_label"], ), ], [ Paragraph(self.manifest.shipper.country, self.styles["line_data_largest"]), Paragraph(self.description, self.styles["line_data_large"]), ], [Paragraph("COUNTRY OF ORIGIN", self.styles["line_label"]), ""], [ Paragraph(self.manifest.shipper.country, self.styles["line_data_largest"]), "", ], [ Paragraph("COUNTRY OF ULTIMATE DESTINATION", self.styles["line_label"]), "", ], [ Paragraph(self.manifest.consignee.country, self.styles["line_data_largest"]), "", ], ] t1 = Table(data1) t1.setStyle( TableStyle([ ("INNERGRID", (0, 1), (0, 2), 0.25, colors.black), ("INNERGRID", (0, 3), (0, 4), 0.25, colors.black), ("INNERGRID", (0, 0), (1, 0), 0.25, colors.black), ("INNERGRID", (0, 1), (1, 1), 0.25, colors.black), ("INNERGRID", (0, 2), (1, 2), 0.25, colors.black), ("INNERGRID", (0, 3), (1, 3), 0.25, colors.black), ("INNERGRID", (0, 4), (1, 4), 0.25, colors.black), ("INNERGRID", (0, 5), (1, 5), 0.25, colors.black), ("SPAN", (1, 1), (1, 5)), ("BOX", (0, 0), (-1, -1), 0.25, colors.black), ("VALIGN", (0, 0), (-1, -1), "MIDDLE"), ])) story.append(t1) story.append(Spacer(0.1 * cm, 0.5 * cm)) story.append( Table([[ Paragraph( "I DECLARE THE INFORMATION CONTAINED IN THIS " "MANIFEST TO BE TRUE AND CORRECT", self.styles["line_label"], ) ]])) story.append(Spacer(0.1 * cm, 0.5 * cm)) data1 = [ [ Paragraph(self.contact_name, self.styles["line_data_large"]), "", Paragraph( self.manifest.export_datetime.strftime("%Y-%m-%d %H:%M") if self.manifest.shipped else "PREVIEW", self.styles["line_data_large"], ), ], [ Paragraph( "SIGNATURE OF SHIPPER/EXPORTER (Type name and title and sign.)", self.styles["line_label"], ), "", Paragraph("DATE", self.styles["line_label"]), ], ] t1 = Table(data1, colWidths=(None, 2 * cm, None)) t1.setStyle( TableStyle([ ("INNERGRID", (0, 0), (0, 1), 0.25, colors.black), ("INNERGRID", (2, 0), (2, 1), 0.25, colors.black), ])) story.append(t1) story.append(Spacer(0.1 * cm, 0.5 * cm)) # boxes story.append( Table([[ Paragraph("MANIFEST CONTENTS", self.styles["line_label_center"]) ]])) story = self.append_manifest_items_story(story) if self.manifest.shipped and not self.manifest.printed: self.manifest.printed = True self.manifest.save() return story
def createBarCodes(): """ Create barcode examples and embed in a PDF """ #Taking the filename from the user name = input('enter a filename(.pdf) :') c = canvas.Canvas(name, pagesize=letter) #Setting the 10 digit barcode value barcode_value = str(random_with_N_digits(12)) barcode39 = code39.Extended39(barcode_value) barcode39Std = code39.Standard39(barcode_value, barHeight=20, stop=1) # code93 also has an Extended and MultiWidth version barcode93 = code93.Standard93(barcode_value) barcode128 = code128.Code128(barcode_value) # the multiwidth barcode appears to be broken #barcode128Multi = code128.MultiWidthBarcode(barcode_value) codes = [barcode39, barcode39Std, barcode93, barcode128] x = 1 * mm y = 285 * mm x1 = 6.4 * mm for code in codes: code.drawOn(c, x, y) y = y - 15 * mm # draw the eanbc8 code barcode_eanbc8 = eanbc.Ean8BarcodeWidget(barcode_value) bounds = barcode_eanbc8.getBounds() width = bounds[2] - bounds[0] height = bounds[3] - bounds[1] d = Drawing(50, 10) d.add(barcode_eanbc8) renderPDF.draw(d, c, 15, 555) # draw the eanbc13 code barcode_eanbc13 = eanbc.Ean13BarcodeWidget(barcode_value) bounds = barcode_eanbc13.getBounds() width = bounds[2] - bounds[0] height = bounds[3] - bounds[1] d = Drawing(50, 10) d.add(barcode_eanbc13) renderPDF.draw(d, c, 15, 465) # draw a QR code qr_code = qr.QrCodeWidget(barcode_value) bounds = qr_code.getBounds() width = bounds[2] - bounds[0] height = bounds[3] - bounds[1] d = Drawing(45, 45, transform=[45. / width, 0, 0, 45. / height, 0, 0]) d.add(qr_code) renderPDF.draw(d, c, 15, 405) c.save()
#tutorial 39 #create multiple shapes #reportlab graphics sub package to create multiple shapes from reportlab.graphics.barcode import code39 from reportlab.platypus import SimpleDocTemplate,Paragraph from reportlab.lib.styles import getSampleStyleSheet pdf=SimpleDocTemplate("tutorial39.pdf") flow_obj=[] styles=getSampleStyleSheet() codetext="-paste-colgatemax white-100gm-5usd-" code=code39.Standard39(codetext,barHeight=50) flow_obj.append(code) pdf.build(flow_obj)
from reportlab.lib.units import mm from reportlab.graphics.barcode import code39 import xlrd from barcode import generate plik = xlrd.open_workbook("numery.xls") strona = plik.sheet_by_index(0) total_rows = strona.nrows c = canvas.Canvas("barcode.pdf", pagesize=A4) licznik = total_rows // 16 i = 0 for d in range(licznik - 1): c.setFont("Helvetica", 25) code = strona.cell(i, 0).value barcode = code39.Standard39(code, checksum=0, barWidth=0.25 * mm, barHeight=30 * mm) #1#1 barcode.drawOn(c, 10 * mm, 245 * mm) c.drawString(14 * mm, 235 * mm, code) i += 1 code = strona.cell(i, 0).value barcode = code39.Standard39(code, checksum=0, barWidth=0.25 * mm, barHeight=30 * mm) #1#2 c.drawString(60 * mm, 250 * mm, code) barcode.drawOn(c, 55 * mm, 215 * mm) i += 1