我们从Python开源项目中,提取了以下22个代码示例,用于说明如何使用reportlab.lib.units.mm()。
def draw(self): self.canv.saveState() XP, YP = self.xp, self.yp self.resize_images(self.text) story = self.text W, H = (self.width - self.lm - self.rm), self.yp - self.y0 - self.bm # possible modes: shrink, truncate, overflow, error frame = KeepInFrame(W, H, story, mode="shrink") w, h = frame.wrapOn(self.canv, W, H) frame.drawOn(self.canv, self.xp, self.yp - h) self.addSpace(0, -h - 5 * mm) self.canv.restoreState() # Layouting
def _num(s, unit=1, allowRelative=True): """Convert a string like '10cm' to an int or float (in points). The default unit is point, but optionally you can use other default units like mm. """ if s.endswith('cm'): unit=cm s = s[:-2] if s.endswith('in'): unit=inch s = s[:-2] if s.endswith('pt'): unit=1 s = s[:-2] if s.endswith('i'): unit=inch s = s[:-1] if s.endswith('mm'): unit=mm s = s[:-2] if s.endswith('pica'): unit=pica s = s[:-4] return _convnum(s,unit,allowRelative)
def __init__(self, shipping_label: ShippingLabel, label_width, label_height, hmargin=5 * mm, vmargin=5 * mm) -> None: super().__init__() self.shipping_label = shipping_label self.label_width = label_width self.hmargin = hmargin self.width = self.label_width - (2 * hmargin) self.label_height = label_height self.vmargin = vmargin self.height = self.label_height - (2 * vmargin) self.x1 = self.hmargin self.y1 = self.vmargin self.x2 = self.hmargin + self.width self.y2 = self.vmargin + self.height
def __init__(self, page_size=pagesizes.A4, shipping_labels_margin=(0, 0), posting_list_margin=(5 * mm, 5 * mm)): self.shipping_labels = [] # type: List[ShippingLabel] self._tracking_codes = set() self.page_size = page_size self.page_width = page_size[0] self.page_height = page_size[1] self.posting_list = None # type: PostingList self.posting_list_margin = posting_list_margin self.shipping_labels_margin = shipping_labels_margin self.shipping_labels_width = self.page_width - (2 * shipping_labels_margin[0]) self.shipping_labels_height = self.page_height - (2 * shipping_labels_margin[1]) self.col_size = self.shipping_labels_width / 2 self.row_size = self.shipping_labels_height / 2 self._label_position = ( (shipping_labels_margin[0], self.page_height / 2), (shipping_labels_margin[0] + self.col_size, self.page_height / 2), (shipping_labels_margin[0], shipping_labels_margin[1]), (shipping_labels_margin[0] + self.col_size, shipping_labels_margin[1]), )
def draw_page_number(self, page_count): self.setFont("Helvetica", 7) self.drawRightString(200*mm, 20*mm, "Page %d of %d" % (self._pageNumber, page_count))
def get_default_document(_buffer, footer: str=None) -> BaseDocTemplate: def on_page(canvas, doc, footer=footer): canvas.saveState() if footer: canvas.setFontSize(8) for i, line in enumerate(footer.split('\n')[::-1]): canvas.drawCentredString(PAGESIZE[0] / 2, 25 + (3.5 * i) * mm, line.strip()) canvas.restoreState() doc = BaseDocTemplate( _buffer, pagesize=PAGESIZE, leftMargin=25 * mm, rightMargin=20 * mm, topMargin=20 * mm, bottomMargin=20 * mm, ) frame = Frame(doc.leftMargin, doc.bottomMargin, doc.width, doc.height, leftPadding=0, rightPadding=0, topPadding=0, bottomPadding=0, id='normal') doc_template = PageTemplate(id='all', pagesize=PAGESIZE, frames=[frame], onPage=on_page) doc.addPageTemplates([doc_template]) return doc
def hello(c, link): c.translate(ORIGIN_X, ORIGIN_Y) # Draw paragraph stylesheet = getSampleStyleSheet() style = stylesheet['BodyText'] style.fontName = 'LeagueGothic' style.fontSize = 42 style.leading = 44 p = Paragraph('<b>print</b><br/>your<br/><b>badge</b><br/>here', style) qr_left = 30*mm p_w, p_h = p.wrap(qr_left, HEIGHT) p.drawOn(c, 0, 0) # Add QR Code qr_code = qr.QrCodeWidget(link) qr_bounds = qr_code.getBounds() qr_width = qr_bounds[2] - qr_bounds[0] qr_height = qr_bounds[3] - qr_bounds[1] d = Drawing(HEIGHT, HEIGHT, transform=[HEIGHT/qr_width,0,0,HEIGHT/qr_height,0,0]) d.add(qr_code) renderPDF.draw(d, c, qr_left, 0) # Draw thin line between text and QR code c.line(qr_left, 0, qr_left, HEIGHT) c.line(qr_left + HEIGHT, 0, qr_left+HEIGHT, HEIGHT) img_left = qr_left + HEIGHT # Draw images c.drawImage('images/ipv6.jpg', img_left, 0, 20*mm, 1/3 * HEIGHT, mask=None, preserveAspectRatio=True, anchor='c') c.drawImage('images/ffrhein_logo_claim_line_rot.png', img_left, 1/3*HEIGHT, 20*mm, 2/3 * HEIGHT, mask=None, preserveAspectRatio=True, anchor='c')
def __init__(self, submission, styles, width): super().__init__() self.submission = submission self.styles = styles self.width = width self.height = min(2.5 * max(submission.get_duration(), 30) * mm, A4[1])
def draw(self): self.canv.rect(0, 0, self.width, self.height) self.canv.rotate(90) self.canv.setFont("Helvetica", 16) self.canv.drawString(10 * mm, - 12 * mm, str(self.submission.submission_type.name)) self.canv.rotate(-90) p = Paragraph(self.submission.title, style=self.styles["Title"]) w, h = p.wrapOn(self.canv, self.width - 30 * mm, 50 * mm) y = h + 10 * mm p.drawOn(self.canv, *self.coord(20 * mm, y)) p = Paragraph(", ".join( [s.get_display_name() for s in self.submission.speakers.all()] ), style=self.styles["Speaker"]) w, h = p.wrapOn(self.canv, self.width - 30 * mm, 50 * mm) y += h + 2 * mm p.drawOn(self.canv, *self.coord(20 * mm, y)) p = Paragraph(_('{} minutes, #{}, {}, {}').format( self.submission.get_duration(), self.submission.code, self.submission.content_locale, self.submission.state ), style=self.styles["Meta"]) w, h = p.wrapOn(self.canv, self.width - 30 * mm, 50 * mm) y += h + 2 * mm p.drawOn(self.canv, *self.coord(20 * mm, y)) if self.submission.abstract: p = Paragraph(ellipsize(self.submission.abstract, 140), style=self.styles["Meta"]) w, h = p.wrapOn(self.canv, self.width - 30 * mm, 50 * mm) y += h + 2 * mm p.drawOn(self.canv, *self.coord(20 * mm, y)) if self.submission.notes: p = Paragraph(ellipsize(self.submission.notes, 140), style=self.styles["Meta"]) w, h = p.wrapOn(self.canv, self.width - 30 * mm, 50 * mm) y += h + 2 * mm p.drawOn(self.canv, *self.coord(20 * mm, y))
def begin(self): canv = self.canv canv.setStrokeColor(colors.black) canv.setFillColor(colors.black) canv.setLineWidth(0.25 * mm) canv.setLineCap(1)
def drawFrame(self): if self.frame: c = self.canv c.saveState() c.setStrokeColor(colors.lightgrey) c.setLineWidth(0.25 * mm) c.rect(self.x0, self.y0, self.width, self.height) c.setStrokeColor(colors.black) c.restoreState()
def fits(self, flowables): story = flowables self.resize_images(story) W, H = ( self.width - self.lm - self.rm), self.height - self.tm - self.bm canv = Canvas(io.StringIO(), (W, H)) total_height = sum([el.wrapOn(canv, W, 0)[1] + bt.spaceBefore for el in story]) if getattr(self, "verbose", False) == True: print("***", total_height / mm, H / mm, \ [txt.text[:5] for txt in story]) return total_height < H
def add_size_info_label(canv, x, y, pageSize, cardSize): "Put info label about used sizes on a page." canv.setFont(bt.fontName, 8) args = (pageSize + cardSize) args = tuple([a / mm for a in args]) desc = "paper: %3.0f x %3.0f mm, cards: %3.0f x %3.0f mm" % args canv.drawString(x, y + cardSize[1] + 6, desc)
def make_cards_platypus(cardSize, cover, items, verbose=False): "Generate q/a sides of quiz cards from a list of quiz items." # cover and items are Platypus flowables! cw, ch = cardSize kwDict = { "lm": 4 * mm, "rm": 4 * mm, "text": "", "width": cw, "height": ch, "verbose": verbose } q_side = QuizCard(**kwDict) a_side = QuizCard(**kwDict) # first cover card questions = cover answers = [] # rest of the normal cards for i, item in enumerate(items): q, a = item["q"], item["a"] q_side.text = questions a_side.text = answers yield q_side, a_side questions = [Paragraph("Question:", fine)] + [q] answers = [Paragraph("Answer %d:<br/><br/>" % (i + 1), fine)] + [a] q_side = QuizCard(**kwDict) a_side = QuizCard(**kwDict) q_side.text = questions a_side.text = answers yield q_side, a_side
def _posting_list_footer(self, pdf, width, x1, y1, x2, y2): canvas = pdf.canvas canvas.rect(x1, y1, width, 38 * mm) canvas.setFont("Helvetica-Bold", size=9) canvas.drawCentredString(x2 - (width / 2), y1 + 38 * mm - 10, self.footer_title_text) canvas.setFont("Helvetica", size=8) canvas.drawString(x1 + 2 * mm, y1 + 28 * mm, self.footer_disclaimer) text_width = stringWidth(self.footer_stamp_text, "Helvetica", 8) canvas.drawString(x2 - 2 * mm - text_width, y1 + 28 * mm, self.footer_stamp_text) text = Paragraph(self.footer_signature_text, style=self.signature_style) text.wrap(stringWidth(self.footer_disclaimer, "Helvetica", 8), 10 * mm) text.drawOn(canvas, x1 + 2 * mm, y1 + 2 * mm) # noinspection PyUnusedLocal
def _posting_list_table(self, canvas, x1, y1, x2, y2, shipping_labels): style = self.table_style[:] table = [self.table_header] for i, shipping_label in enumerate(shipping_labels, start=1): row = ( str(shipping_label.tracking_code), str(shipping_label.receiver.zip_code), str(shipping_label.package.posting_weight), self.yes if ExtraService.get(EXTRA_SERVICE_AR) in shipping_label else self.no, self.yes if ExtraService.get(EXTRA_SERVICE_MP) in shipping_label else self.no, self.yes if ExtraService.get(EXTRA_SERVICE_VD) in shipping_label else self.no, str(shipping_label.value).replace(".", ",") if shipping_label.value is not None else "", str(shipping_label.invoice_number), shipping_label.get_package_sequence(), shipping_label.receiver.name[:self.max_receiver_name_size], ) # noinspection PyTypeChecker table.append(row) if i % 2: style.append(('BACKGROUND', (0, i), (-1, i), colors.lightgrey)) table_flow = Table( table, colWidths=self.col_widths, style=TableStyle(style), ) w, h = table_flow.wrap(0, 0) table_flow.drawOn(canvas, x1, y2 - h - 50 * mm)
def __init__(self,xc, yc, w, h, name, desc, ref): self.xc = xc self.yc = yc self.w = w self.h = h if(self.w == 0): self.w = 0.8 * mm if(self.h == 0): self.h = 0.8 * mm self.name = name self.desc = desc self.ref = ref
def gen_table(self, layer, index, n_comps,canv): parts = self.split_parts(layer, index, n_comps) yt = 260 * mm canv.setFont("Helvetica",10) canv.setStrokeGray(0) canv.setFillGray(0) canv.drawString(20 * mm, yt, "Color"); canv.drawString(40 * mm, yt, "Lib.Reference"); canv.drawString(80 * mm, yt, "Comment"); canv.drawString(120 * mm, yt, "Designators"); n=0 for group in parts: dsgn = "" yt = yt - 6 * mm canv.setFillColor(self.col_map[n]) canv.rect(20 *mm, yt, 10 * mm, 3 * mm, 1, 1) canv.setFillGray(0) n=n+1 for part in group: dsgn = dsgn + " " + part.name canv.drawString(120 * mm, yt, dsgn); canv.drawString(40 * mm, yt, group[0].ref[0:20]); canv.drawString(80 * mm, yt, group[0].desc[0:20]); # table.append(["", dsgn, group[0].desc, group[0].ref])
def draw_page_number(self, page_count): # Change the position of this to wherever you want the page number to be self.drawRightString(115 * mm, 25 * mm + (0.2 * inch), "Page {} of {}".format(self._pageNumber, page_count))
def setTextLine(self,text,line,offset=0): r = self.leftMargin*mm + offset*mm s = 240*mm - line*7*mm self.canvas.drawString(r,s,text)
def badge(): name = request.form['name'][:MAX_CHARS_PER_LINE] if 'name' in request.form else '' name2 = request.form['name2'][:MAX_CHARS_PER_LINE] if 'name2' in request.form else '' nick = request.form['nick'][:MAX_CHARS_PER_LINE] if 'nick' in request.form else '' community = request.form['community'][:MAX_CHARS_PER_LINE] if 'community' in request.form else '' pdf = BytesIO() c = Canvas(pdf, pagesize=(BADGE_W, BADGE_H)) c.translate(ORIGIN_X, ORIGIN_Y) ico_center = 7*mm offset = HEIGHT+2*mm c.setFillGray(0.66) c.setFont('Awesome', 42) c.drawCentredString(ico_center, offset-42*pica/12, '\uf007') c.setFont('Awesome', 38) c.drawCentredString(ico_center, offset-(2*42+40)*pica/12, '\uf1fa') c.drawCentredString(ico_center, offset-(2*42+2*40)*pica/12, '\uf041') txt_start = 15*mm c.setFillGray(0.0) c.setFont('LeagueGothic', 42) c.drawString(txt_start, offset-42*pica/12, name) c.drawString(txt_start, offset-2*42*pica/12, name2) c.setFont('LeagueGothic', 38) c.drawString(txt_start, offset-(2*42+40)*pica/12, nick) c.drawString(txt_start, offset-(2*42+2*40)*pica/12, community) evt_width = 38*pica/12 evt_start = WIDTH - evt_width img_width = 20*mm img_start = evt_start - img_width c.drawImage(path.join(path.dirname(__file__), 'images/ffrhein_logo_claim_line_rot.png'), img_start, 0, img_width, HEIGHT, mask=None, preserveAspectRatio=True, anchor='c') c.rotate(90) c.rect(0, -WIDTH, HEIGHT, evt_width, 0, 1) c.setFillGray(1.0) c.drawCentredString(HEIGHT/2, -WIDTH+MARGIN_R, 'routing days') c.showPage() c.save() _print(pdf.getvalue()) pdf.close() # response = make_response('Meh') # response.headers['Content-Type'] = 'text/plain' # return response return redirect('/badge/printing.html')
def _posting_list_header(self, pdf, width, x1, y1, x2, y2): canvas = pdf.canvas # logo logo = ImageReader(self.posting_list.logo) canvas.drawImage(logo, x1, y2 - 10.3 * mm, height=8 * mm, preserveAspectRatio=True, anchor="sw", mask="auto") # head1 canvas.setFont("Helvetica-Bold", size=14) canvas.drawCentredString(x2 - ((width - 40 * mm) / 2), y2 - 9 * mm, "Empresa Brasileira de Correios e Telégrafos".upper()) # box canvas.setLineWidth(0.5) canvas.rect(x1, y2 - 45 * mm, width, 30 * mm) canvas.drawCentredString(x1 + width / 2, y2 - (15 * mm) - 15, self.heading_title) # header info spacer = 5 * mm col_width = width / 4 col = 0 header = self.header_label_col1.format(self.posting_list.number, self.posting_list.contract, self.posting_list.posting_card.administrative_code, self.posting_list.posting_card) text = Paragraph(header, style=self.label_style) text.wrap(col_width, 30 * mm) text.drawOn(canvas, x1 + spacer + col * col_width, y2 - (43 * mm)) col = 1 header = self.header_label_col2.format(self.posting_list.sender.name[:30], self.posting_list.contract.customer_name[:30], self.posting_list.sender.display_address[0][:30], self.posting_list.sender.display_address[1][:30]) text = Paragraph(header, style=self.label_style) text.wrap(col_width * 2, 30 * mm) text.drawOn(canvas, x1 + spacer + col * col_width, y2 - (43 * mm)) col = 3 header = self.header_label_col3.format(self.posting_list.sender.phone.display()) text = Paragraph(header, style=self.label_style) text.wrap(col_width, 30 * mm) text.drawOn(canvas, x1 + spacer + col * col_width, y2 - (43 * mm)) code = createBarcodeDrawing("Code128", value=str(self.posting_list.number), width=col_width * 0.6, height=10 * mm, quiet=0) code.drawOn(canvas, x1 + spacer + col * col_width, y2 - (35 * mm)) # noinspection PyUnusedLocal