Python reportlab.lib.units.mm() Examples

The following are 30 code examples of reportlab.lib.units.mm(). You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may also want to check out all available functions/classes of the module reportlab.lib.units , or try the search function .
Example #1
Source File: generic_pdf.py    From multiscanner with Mozilla Public License 2.0 7 votes vote down vote up
def __init__(self, pdf_components):
        self.style = getSampleStyleSheet()
        self.style['Normal'].leading = 16
        self.style.add(ParagraphStyle(name='centered', alignment=TA_CENTER))
        self.style.add(ParagraphStyle(name='centered_wide', alignment=TA_CENTER,
                                      leading=18))
        self.style.add(ParagraphStyle(name='section_body',
                                      parent=self.style['Normal'],
                                      spaceAfter=inch * .05,
                                      fontSize=11))
        self.style.add(ParagraphStyle(name='bullet_list',
                                      parent=self.style['Normal'],
                                      fontSize=11))
        if six.PY3:
            self.buffer = six.BytesIO()
        else:
            self.buffer = six.StringIO()
        self.firstPage = True
        self.document = SimpleDocTemplate(self.buffer, pagesize=letter,
                                          rightMargin=12.7 * mm, leftMargin=12.7 * mm,
                                          topMargin=120, bottomMargin=80)

        self.tlp_color = pdf_components.get('tlp_color', '')
        self.pdf_components = pdf_components
        self.pdf_list = [] 
Example #2
Source File: generic_pdf.py    From multiscanner with Mozilla Public License 2.0 6 votes vote down vote up
def vertical_table(self, data, table_style=None, col_widths=None):
        '''A table where the first column is bold. A label followed by values.'''
        self.style['BodyText'].wordWrap = 'LTR'
        self.style['BodyText'].spaceBefore = 2

        if table_style:
            style = table_style
        else:
            style = TableStyle([
                ('LINEABOVE', (0, 0), (-1, 0), 0.75, blue),
                ('BOX', (1, 0), (0, -1), 0.25, black),
                ('ALIGN', (1, 1), (-1, -1), 'RIGHT')
            ])

        if col_widths:
            cols = col_widths
        else:
            cols = (35 * mm, 140 * mm)

        data2 = [[Paragraph(self.bold_text(cell), self.style['BodyText']) if idx == 0
                  else Paragraph(cell, self.style['BodyText'])
                  for idx, cell in enumerate(row)] for row in data]

        table = Table(data2, style=style, colWidths=cols)
        self.pdf_list.append(table) 
Example #3
Source File: paraparser.py    From Fluid-Designer with GNU General Public License v3.0 6 votes vote down vote up
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) 
Example #4
Source File: barcodeMaker.py    From HH---POS-Accounting-and-ERP-Software with MIT License 6 votes vote down vote up
def makeBarcodeFile (brc, width, height):
	brc = str(brc)
	width = float(width) * mm
	height = float(height) * mm
	# generate a canvas (A4 in this case, size doesn"t really matter)
	c=canvas.Canvas(brc+".pdf",pagesize=A4)
	# create a barcode object
	# (is not displayed yet)
	# The encode text is "123456789"
	# barHeight encodes how high the bars will be
	# barWidth encodes how wide the "narrowest" barcode unit is
	barcode=code39.Extended39(brc, barWidth=width*mm, barHeight=height*mm)
	# drawOn puts the barcode on the canvas at the specified coordinates
	
	x, y = (10*mm, 10*mm)
	while y + barcode.height < 290*mm:
		while x + barcode.width < 200*mm:
			barcode.drawOn(c, x, y)
			x = x + (1 + barcode.width)
		x = 10*mm
		y = y + (1 + barcode.height)*mm 

	# now create the actual PDF
	c.showPage()
	c.save() 
Example #5
Source File: paraparser.py    From stdm with GNU General Public License v2.0 6 votes vote down vote up
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) 
Example #6
Source File: pdf.py    From correios with Apache License 2.0 6 votes vote down vote up
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 
Example #7
Source File: pdf.py    From correios with Apache License 2.0 6 votes vote down vote up
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]),
        ) 
Example #8
Source File: generic_pdf.py    From multiscanner with Mozilla Public License 2.0 6 votes vote down vote up
def header_footer(self, canvas, doc):
        canvas.saveState()
        height_adjust = self.add_banner(canvas, doc)

        # Document Header
        if self.pdf_components.get('hdr_image', None) and self.firstPage:
            header = Image(self.pdf_components.get('hdr_image'), height=25 * mm, width=191 * mm)
            header.drawOn(canvas, doc.rightMargin, doc.height + doc.topMargin - 15 * mm)
            self.firstPage = False
        elif self.firstPage:
            header = Paragraph(self.pdf_components.get('hdr_html', ''), self.style['centered'])
            w, h = header.wrap(doc.width, doc.topMargin)
            header.drawOn(canvas, doc.leftMargin, doc.height + doc.topMargin - height_adjust * h)

        # Document Footer
        if self.pdf_components.get('ftr_image', None):
            footer = Image(self.pdf_components.get('ftr_image'), 8.5 * inch, 1.8 * inch)
            footer.drawOn(canvas, 0, 0)
        else:
            footer = Paragraph(self.pdf_components.get('ftr_html', ''), self.style['centered'])
            w, h = footer.wrap(doc.width, doc.bottomMargin)
            footer.drawOn(canvas, doc.leftMargin, height_adjust * h)

        # Release the Canvas
        canvas.restoreState() 
Example #9
Source File: pdf.py    From correios with Apache License 2.0 5 votes vote down vote up
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 
Example #10
Source File: pdf.py    From correios with Apache License 2.0 5 votes vote down vote up
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.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 shipping_label.has_declared_value() 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) 
Example #11
Source File: generic_pdf.py    From multiscanner with Mozilla Public License 2.0 5 votes vote down vote up
def draw_page_number(self, page_count):
        self.setFont('Helvetica-Bold', 7)
        self.drawRightString(203 * mm, 12.7 * mm,
                             'Page %d of %d' % (self._pageNumber, page_count)) 
Example #12
Source File: generic_pdf.py    From multiscanner with Mozilla Public License 2.0 5 votes vote down vote up
def add_banner(self, canvas, doc):
        height_adjust = 1
        if self.tlp_color:
            if self.tlp_color == 'WHITE':
                text_color = white
            elif self.tlp_color == 'RED':
                text_color = red
            elif self.tlp_color == 'AMBER':
                text_color = orange
            else:
                text_color = lawngreen
                self.tlp_color = 'GREEN'

            if 'banner_style' not in self.style:
                self.style.add(ParagraphStyle(name='banner_style',
                                              textColor=text_color,
                                              textTransform='uppercase',
                                              alignment=TA_RIGHT))

            banner = Paragraph(
                self.span_text(self.bold_text('TLP:' + self.tlp_color), bgcolor='black'),
                self.style['banner_style'])
            w, h = banner.wrap(doc.width, doc.topMargin)
            banner.drawOn(canvas, doc.leftMargin, doc.height + doc.topMargin + (h + 12 * mm))
            w, h = banner.wrap(doc.width, doc.bottomMargin)
            banner.drawOn(canvas, doc.leftMargin, h + 12 * mm)

            height_adjust = 3

        return height_adjust 
Example #13
Source File: letters.py    From coursys with GNU General Public License v3.0 5 votes vote down vote up
def label_filled(self, x, y, content):
        if content:
            self.c.setFont("Courier", 9)
            self.c.drawString(x*mm, y*mm, content) 
Example #14
Source File: box.py    From box-designer-website with GNU Affero General Public License v3.0 5 votes vote down vote up
def _draw_line(self, from_x, from_y, to_x, to_y):
        self.paths.add_segment(from_x*mm, from_y*mm, to_x*mm, to_y*mm) 
Example #15
Source File: box.py    From box-designer-website with GNU Affero General Public License v3.0 5 votes vote down vote up
def _draw_bounding_box(self):
        # render a box around the whole thing to make resizing easier when imports fail in 3rd party tools
        self._doc.rect(self._margin*mm, self._margin*mm, self._bounding_box_size['w']*mm,
                       self._bounding_box_size['h']*mm)
        self._doc.drawString(15*mm, self._doc_size['h']*mm - 20*mm, "Bounding Box: %.2fmm x %.2fmm" %
                             (self._bounding_box_size['w'], self._bounding_box_size['h'])) 
Example #16
Source File: box.py    From box-designer-website with GNU Affero General Public License v3.0 5 votes vote down vote up
def _initialize_document(self):
        # initialize the pdf file (based on layout of pieces)
        self._doc = self._doc_cls(self._file_path)
        self._doc.setPageSize([self._doc_size['w']*mm, self._doc_size['h']*mm])
        self._doc.setAuthor(boxmaker.APP_NAME+" "+boxmaker.APP_VERSION)
        self._doc.setStrokeColor(black)
        self._doc.setLineWidth(0.1)
        # print out some summary info for good record keeping purposes
        self._doc.drawString(15*mm, 35*mm, "Cut Width: %.4fmm" % self._cut_width)
        self._doc.drawString(15*mm, 30*mm, "Material Thickness: %.4fmm" % self._thickness)
        self._doc.drawString(15*mm, 25*mm, "W x D x H: %.2fmm x %.2fmm x %.2fmm" %
                             (self._size['w'], self._size['d'], self._size['h']))
        self._doc.drawString(15*mm, 20*mm, "Produced by "+boxmaker.APP_NAME+" v"+boxmaker.APP_VERSION +
                             " on "+time.strftime("%m/%d/%Y")+" at "+time.strftime("%H:%M:%S"))
        self._doc.drawString(15*mm, 15*mm, boxmaker.WEBSITE_URL) 
Example #17
Source File: letters.py    From coursys with GNU General Public License v3.0 5 votes vote down vote up
def label_mid_small(self, x, y, content):
        self.c.setFont("Helvetica", 7.25)
        self.c.drawString(x * mm, y * mm, content) 
Example #18
Source File: letters.py    From coursys with GNU General Public License v3.0 5 votes vote down vote up
def label_mid(self, x, y, content):
        self.c.setFont("Helvetica", 7.5)
        self.c.drawString(x * mm, y * mm, content) 
Example #19
Source File: letters.py    From coursys with GNU General Public License v3.0 5 votes vote down vote up
def checkbox(self, x, y, filled=0):
        self.c.rect(x * mm, y * mm, 3.1 * mm, 3.1 * mm, fill=filled) 
Example #20
Source File: letters.py    From coursys with GNU General Public License v3.0 5 votes vote down vote up
def checkbox(self, x, y, filled=0):
        self.c.circle(x*mm, y*mm + 1*mm, 1*mm, fill=filled) 
Example #21
Source File: letters.py    From coursys with GNU General Public License v3.0 5 votes vote down vote up
def label_filled_centred(self, x, y, content):
        if content:
            self.c.setFont("Courier", 9)
            self.c.drawCentredString(x*mm, y*mm, content) 
Example #22
Source File: letters.py    From coursys with GNU General Public License v3.0 5 votes vote down vote up
def label_filled_large(self, x, y, content):
        if content:
            self.c.setFont("Courier", 11)
            self.c.drawString(x*mm, y*mm, content) 
Example #23
Source File: note.py    From Servo with BSD 2-Clause "Simplified" License 5 votes vote down vote up
def __init__(self, text_value, *args, **kwargs):
        barcode = createBarcodeDrawing("Code128",
                                       value=text_value.encode("utf-8"),
                                       barHeight=10 * mm,
                                       width=80 * mm)
        Drawing.__init__(self, barcode.width, barcode.height, *args, **kwargs)
        self.add(barcode, name="barcode") 
Example #24
Source File: letters.py    From coursys with GNU General Public License v3.0 5 votes vote down vote up
def header_label_italics(self, x, y, content):
        if content:
            self.c.setFont("Helvetica-BoldOblique", 8)
            self.c.drawString(x*mm, y*mm, content) 
Example #25
Source File: letters.py    From coursys with GNU General Public License v3.0 5 votes vote down vote up
def _checkbox(self, x, y, text="", filled=0, leading=0):
        self.c.circle(x+1.5*mm, y+1.5*mm, 1.5*mm, stroke=1, fill=filled)
        self.c.setFont("Helvetica", 7)
        self.c.drawString(x+5*mm, y+0.5*mm+leading, text) 
Example #26
Source File: letters.py    From coursys with GNU General Public License v3.0 5 votes vote down vote up
def _box_entry(self, x, y, width, height, content=None):
        self.c.setLineWidth(1)
        self.c.rect(x, y, width, height)
        if content:
            self.c.setFont(self.ENTRY_FONT, 12)
            self.c.drawString(x+3*mm, y+height-4.5*mm, content) 
Example #27
Source File: letters.py    From coursys with GNU General Public License v3.0 5 votes vote down vote up
def check_label(self, x, y, label, fill=0):
        self.label_font()
        self.c.rect(x, y, 3*mm, 3*mm, fill=0)
        if fill:
            self.c.line(x, y, x+3*mm, y+3*mm)
            self.c.line(x+3*mm, y, x, y+3*mm)
        self.c.drawString(x+5*mm, y, label) 
Example #28
Source File: letters.py    From coursys with GNU General Public License v3.0 5 votes vote down vote up
def _header_line(self, y, text):
        self.c.setFillColor(black)
        self.c.rect(0, y, self.main_width, 3.5*mm, fill=1)
        self.c.setFillColor(white)
        self.c.setFont("Helvetica-Bold", 9)
        self.c.drawCentredString(self.main_width/2, y + 0.5*mm, text)
        self.c.setFillColor(black) 
Example #29
Source File: letters.py    From coursys with GNU General Public License v3.0 5 votes vote down vote up
def _line_entry(self, x, y, text, line_offset, line_length, entry_text=''):
        self.c.setFont(*self.LABEL_FONT)
        self.c.setLineWidth(1)
        self.c.drawString(x, y+1*mm, text)
        self.c.line(x+line_offset, y, x+line_offset+line_length, y)
        self.c.setFont(*self.ENTRY_FONT)
        self.c.drawString(x+line_offset+2*mm, y+1*mm, entry_text) 
Example #30
Source File: letters.py    From coursys with GNU General Public License v3.0 5 votes vote down vote up
def check_label(self, x, y, label, fill=False):
        self.label_font()
        self.c.setLineWidth(0.4)
        self.c.rect(x, y, 3*mm, 3*mm, fill=0)
        self.c.setLineWidth(self.LINE_WIDTH)
        if fill:
            self.c.line(x, y, x+3*mm, y+3*mm)
            self.c.line(x+3*mm, y, x, y+3*mm)
        self.c.drawString(x+8*mm, y, label)