Python reportlab.lib.units.inch() Examples

The following are 30 code examples of reportlab.lib.units.inch(). 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: pdfgenerator.py    From anti-XSS with MIT License 7 votes vote down vote up
def __init__(self, text=[], target='your website', pdfName='Report.pdf'):
        self.__target = target
        self.__pdfName = self.__path + pdfName
        self.__text = text
        if not os.path.exists('result/'):
            os.mkdir(r'result/')
        time = datetime.datetime.today()
        date = time.strftime("%h-%d-%Y %H:%M:%S")
        c = canvas.Canvas(self.__pdfName)
        c.setPageSize((16 * inch,22 * inch))
        textobj = c.beginText()
        textobj.setTextOrigin(inch, 20 * inch)
        textobj.textLines('''
            This is the scanning report of %s.
            ''' %self.__target)
        textobj.textLines('''
            Date: %s
            ''' % date)
        for line in self.__text:
            textobj.textLine(line.strip())
        c.drawText(textobj)
        c.showPage()
        c.save() 
Example #3
Source File: para.py    From stdm with GNU General Public License v2.0 6 votes vote down vote up
def test2(canv,testpara):
    #print test_program; return
    from reportlab.lib.units import inch
    from reportlab.lib.styles import ParagraphStyle
    from reportlab.lib import rparsexml
    parsedpara = rparsexml.parsexmlSimple(testpara,entityReplacer=None)
    S = ParagraphStyle("Normal", None)
    P = Para(S, parsedpara)
    (w, h) = P.wrap(5*inch, 10*inch)
    print "wrapped as", (h,w)
    canv.saveState()
    canv.translate(1*inch, 1*inch)
    canv.rect(0,0,5*inch,10*inch, fill=0, stroke=1)
    P.canv = canv
    canv.saveState()
    P.draw()
    canv.restoreState()
    canv.setStrokeColorRGB(1, 0, 0)
    #canv.translate(0, 3*inch)
    canv.rect(0,0,w,h, fill=0, stroke=1)
    canv.restoreState()
    canv.showPage() 
Example #4
Source File: builder.py    From tia with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
def onPage(self, canvas, doc):
        c = canvas
        w, h = c._pagesize
        # The cover page just has some drawing on the canvas.
        c.saveState()
        isletter = (w, h) == letter
        c.setFont(self.font, isletter and 16 or 20)
        imgw, imgh = 2.83 * units.inch, .7 * units.inch

        c.drawString(25, h / 2 - 6, self.title)
        if self.logo_path:
            c.drawImage(self.logo_path, w - imgw - 25,
                        h / 2 - .5 * imgh, width=imgw, height=imgh,
                        preserveAspectRatio=True)
        c.setFillColorRGB(0, 0, 0)
        c.rect(0, h / 2 + .5 * imgh + 5, w, 1, fill=1)
        c.rect(0, h / 2 - .5 * imgh - 5, w, 1, fill=1)
        c.setFontSize(isletter and 12 or 16)
        c.drawString(25, h / 2 - .5 * imgh - 50, self.subtitle)
        if self.subtitle2:
            c.drawString(25, h / 2 - .5 * imgh - 70, self.subtitle2)
        c.restoreState() 
Example #5
Source File: poster_PyConES2019_ESversion.py    From scikit-extremes with MIT License 6 votes vote down vote up
def add_code_example(canvas, filename, plot_filename, x, y, width, height):
    canvas.setFillColor(lightgrey)
    canvas.rect(x, y, width, height, fill=1)
    canvas.drawImage(
        filename,
        x,
        20 * inch,
        width=width,
        height=height*0.2,
        mask=[10, 255, 10, 255, 10, 255]
    )
    canvas.drawImage(
        plot_filename,
        x + width * 0.025,
        y * 1.5,
        width=width * 0.95,
        height=height*0.5
    )

#################################
# Create document and its pieces
#################################
# Canvas (PDF) 
Example #6
Source File: QRCodePrinter.py    From Food-Pantry-Inventory with MIT License 6 votes vote down vote up
def fill_pdf_pages(self):
        """
        Fill one or more pages with labels.

        :return:
        """
        # # draw lines around the boxes that will be filled with labels
        # self.draw_boxes_on_page()
        # # self.pdf.setFillColorRGB(1, 0, 1)
        # # self.pdf.rect(2*inch, 2*inch, 2*inch, 2*inch, fill=1)
        for label_file, label_name in self.get_next_qr_img():
            debug(f'Got {label_file}')
            if self.next_pos >= len(self.label_locations) - 1:
                self. finish_page()
                self.next_pos = 0
            else:
                self.next_pos += 1
            self.draw_bounding_box(self.next_pos)
            self.place_label(label_file, label_name, self.next_pos)
        self.finish_page()
        return 
Example #7
Source File: poster_PyConES2019_ENversion.py    From scikit-extremes with MIT License 6 votes vote down vote up
def add_code_example(canvas, filename, plot_filename, x, y, width, height):
    canvas.setFillColor(lightgrey)
    canvas.rect(x, y, width, height, fill=1)
    canvas.drawImage(
        filename,
        x,
        20 * inch,
        width=width,
        height=height*0.2,
        mask=[10, 255, 10, 255, 10, 255]
    )
    canvas.drawImage(
        plot_filename,
        x + width * 0.025,
        y * 1.5,
        width=width * 0.95,
        height=height*0.5
    )

#################################
# Create document and its pieces
#################################
# Canvas (PDF) 
Example #8
Source File: letters.py    From coursys with GNU General Public License v3.0 6 votes vote down vote up
def _draw_box_left(self, x, y, label, content, width=MAIN_WIDTH-BOX_OFFSET, tick=False):
        """
        Draw one text entry box with the above parameters.

        "width" parameter should include one BOX_OFFSET
        """
        # box/tickmark
        self.c.setLineWidth(2)
        if not tick:
            self.c.rect(x + self.BOX_OFFSET, y - self.BOX_OFFSET - self.ENTRY_SIZE, width - self.BOX_OFFSET, self.ENTRY_SIZE)
        else:
            self.c.line(x + self.BOX_OFFSET, y - self.BOX_OFFSET - self.ENTRY_SIZE, x + self.BOX_OFFSET, y - self.BOX_OFFSET - self.ENTRY_SIZE + 0.2*inch)

        # label
        self.c.setFont("Helvetica", 6)
        self.c.drawString(x + self.BOX_OFFSET + self.LABEL_OFFSET, y - self.BOX_OFFSET - self.LABEL_HEIGHT, label)

        # content
        self.c.setFont("Helvetica-Bold", 12)
        self.c.drawString(x + self.BOX_OFFSET + 2*self.LABEL_OFFSET, y - self.BOX_OFFSET - self.ENTRY_SIZE + self.DATA_BUMP, content) 
Example #9
Source File: doctemplate.py    From stdm with GNU General Public License v2.0 6 votes vote down vote up
def run():
        objects_to_draw = []
        from reportlab.lib.styles import ParagraphStyle
        #from paragraph import Paragraph
        from doctemplate import SimpleDocTemplate

        #need a style
        normal = ParagraphStyle('normal')
        normal.firstLineIndent = 18
        normal.spaceBefore = 6
        from reportlab.lib.randomtext import randomText
        import random
        for i in range(15):
            height = 0.5 + (2*random.random())
            box = XBox(6 * inch, height * inch, 'Box Number %d' % i)
            objects_to_draw.append(box)
            para = Paragraph(randomText(), normal)
            objects_to_draw.append(para)

        SimpleDocTemplate('doctemplate.pdf').build(objects_to_draw,
            onFirstPage=myFirstPage,onLaterPages=myLaterPages) 
Example #10
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 #11
Source File: code93.py    From Fluid-Designer with GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, value='', **args):

        if type(value) is type(1):
            value = asNative(value)
            
        for (k, v) in args.items():
            setattr(self, k, v)

        if self.quiet:
            if self.lquiet is None:
                self.lquiet = max(inch * 0.25, self.barWidth * 10.0)
                self.rquiet = max(inch * 0.25, self.barWidth * 10.0)
        else:
            self.lquiet = self.rquiet = 0.0

        MultiWidthBarcode.__init__(self, value) 
Example #12
Source File: common.py    From Fluid-Designer with GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, value='', **args):

        if type(value) == type(1):
            value = str(value)

        for k, v in args.items():
            setattr(self, k, v)

        if self.quiet:
            if self.lquiet is None:
                self.lquiet = min(inch * 0.25, self.barWidth * 10.0)
                self.rquiet = min(inch * 0.25, self.barWidth * 10.0)
        else:
            self.lquiet = self.rquiet = 0.0

        Barcode.__init__(self, value) 
Example #13
Source File: common.py    From Fluid-Designer with GNU General Public License v3.0 6 votes vote down vote up
def __init__(self, value="", **args):

        if type(value) == type(1):
            value = str(value)

        for k, v in args.items():
            setattr(self, k, v)

        if self.quiet:
            if self.lquiet is None:
                self.lquiet = max(inch * 0.25, self.barWidth * 10.0)
                self.rquiet = max(inch * 0.25, self.barWidth * 10.0)
        else:
            self.lquiet = self.rquiet = 0.0

        Barcode.__init__(self, value) 
Example #14
Source File: common.py    From stdm with GNU General Public License v2.0 6 votes vote down vote up
def __init__(self, value="", **args):

        if type(value) == type(1):
            value = str(value)

        for (k, v) in args.items():
            setattr(self, k, v)

        if self.quiet:
            if self.lquiet is None:
                self.lquiet = max(inch * 0.25, self.barWidth * 10.0)
                self.rquiet = max(inch * 0.25, self.barWidth * 10.0)
        else:
            self.lquiet = self.rquiet = 0.0

        Barcode.__init__(self, value) 
Example #15
Source File: common.py    From stdm with GNU General Public License v2.0 6 votes vote down vote up
def __init__(self, value='', **args):

        if type(value) == type(1):
            value = str(value)

        for (k, v) in args.items():
            setattr(self, k, v)

        if self.quiet:
            if self.lquiet is None:
                self.lquiet = min(inch * 0.25, self.barWidth * 10.0)
                self.rquiet = min(inch * 0.25, self.barWidth * 10.0)
        else:
            self.lquiet = self.rquiet = 0.0

        Barcode.__init__(self, value) 
Example #16
Source File: para.py    From Fluid-Designer with GNU General Public License v3.0 6 votes vote down vote up
def test2(canv,testpara):
    #print test_program; return
    from reportlab.lib.units import inch
    from reportlab.lib.styles import ParagraphStyle
    from reportlab.lib import rparsexml
    parsedpara = rparsexml.parsexmlSimple(testpara,entityReplacer=None)
    S = ParagraphStyle("Normal", None)
    P = Para(S, parsedpara)
    (w, h) = P.wrap(5*inch, 10*inch)
    print("wrapped as", (h,w))
    canv.saveState()
    canv.translate(1*inch, 1*inch)
    canv.rect(0,0,5*inch,10*inch, fill=0, stroke=1)
    P.canv = canv
    canv.saveState()
    P.draw()
    canv.restoreState()
    canv.setStrokeColorRGB(1, 0, 0)
    #canv.translate(0, 3*inch)
    canv.rect(0,0,w,h, fill=0, stroke=1)
    canv.restoreState()
    canv.showPage() 
Example #17
Source File: code128.py    From stdm with GNU General Public License v2.0 6 votes vote down vote up
def __init__(self, value='', **args):

        if type(value) is type(1):
            value = str(value)
            
        for (k, v) in args.items():
            setattr(self, k, v)

        if self.quiet:
            if self.lquiet is None:
                self.lquiet = max(inch * 0.25, self.barWidth * 10.0)
            if self.rquiet is None:
                self.rquiet = max(inch * 0.25, self.barWidth * 10.0)
        else:
            self.lquiet = self.rquiet = 0.0

        MultiWidthBarcode.__init__(self, value) 
Example #18
Source File: doctemplate.py    From Fluid-Designer with GNU General Public License v3.0 6 votes vote down vote up
def run():
        objects_to_draw = []
        from reportlab.lib.styles import ParagraphStyle
        #from paragraph import Paragraph
        from reportlab.platypus.doctemplate import SimpleDocTemplate

        #need a style
        normal = ParagraphStyle('normal')
        normal.firstLineIndent = 18
        normal.spaceBefore = 6
        from reportlab.lib.randomtext import randomText
        import random
        for i in range(15):
            height = 0.5 + (2*random.random())
            box = XBox(6 * inch, height * inch, 'Box Number %d' % i)
            objects_to_draw.append(box)
            para = Paragraph(randomText(), normal)
            objects_to_draw.append(para)

        SimpleDocTemplate('doctemplate.pdf').build(objects_to_draw,
            onFirstPage=myFirstPage,onLaterPages=myLaterPages) 
Example #19
Source File: code93.py    From stdm with GNU General Public License v2.0 6 votes vote down vote up
def __init__(self, value='', **args):

        if type(value) is type(1):
            value = str(value)
            
        for (k, v) in args.iteritems():
            setattr(self, k, v)

        if self.quiet:
            if self.lquiet is None:
                self.lquiet = max(inch * 0.25, self.barWidth * 10.0)
                self.rquiet = max(inch * 0.25, self.barWidth * 10.0)
        else:
            self.lquiet = self.rquiet = 0.0

        MultiWidthBarcode.__init__(self, value) 
Example #20
Source File: api.py    From callisto-core with GNU Affero General Public License v3.0 6 votes vote down vote up
def get_cover_page(self, report_id, recipient):
        # title = f"{self.report_title} No.: {report_id}"

        styles = getSampleStyleSheet()
        headline_style = styles["Heading1"]
        headline_style.alignment = TA_CENTER
        headline_style.fontSize = 48
        subtitle_style = styles["Heading2"]
        subtitle_style.fontSize = 24
        subtitle_style.leading = 26
        subtitle_style.alignment = TA_CENTER

        CoverPage = []
        logo = os.path.join(settings.BASE_DIR, self.logo_path)

        image = Image(logo, 3 * inch, 3 * inch)
        CoverPage.append(image)
        CoverPage.append(Spacer(1, 18))
        CoverPage.append(Paragraph("CONFIDENTIAL", headline_style))
        # paragraph = Paragraph(
        #     f"Intended for: {recipient}, Title IX Coordinator", subtitle_style)
        # CoverPage.append(paragraph)
        CoverPage.append(PageBreak())
        return CoverPage

    # entrypoints 
Example #21
Source File: test_basic.py    From svglib with GNU Lesser General Public License v3.0 6 votes vote down vote up
def test_0(self):
        "Test length attribute conversion."

        mapping = (
            ("0", 0),
            ("316", 316),
            ("-316", -316),
            ("-3.16", -3.16),
            ("-1e-2", -0.01),
            ("1e-5", 1e-5),
            ("1e1cm", 10*cm),
            ("1e1in", 10*inch),
            ("-8e-2cm", (-8e-2)*cm),
            ("20px", 20),
            ("20pt", 20 * 1.25),
            ("1.5em", 12 * 1.5),
            ("10.5mm", 10.5*(cm*0.1)),
            ("3, 5 -7", [3, 5, -7]),
            ("2pt  12pt", [2 * 1.25, 12 * 1.25]),
        )
        ac = svglib.Svg2RlgAttributeConverter()
        failed = _testit(ac.convertLength, mapping)
        assert len(failed) == 0
        assert ac.convertLength("1.5em", em_base=16.5) == 24.75 
Example #22
Source File: figures.py    From stdm with GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, background=None):
        Figure.__init__(self, 3*inch, 3*inch)
        self.caption = 'Figure 1 - a blank page'
        self.captionStyle = captionStyle
        self.background = background 
Example #23
Source File: figures.py    From stdm with GNU General Public License v2.0 5 votes vote down vote up
def drawFigure(self):
        drawPage(self.canv, 0.625*inch, 0.25*inch, 1.75*inch, 2.5*inch)
        self.canv.translate(0.625*inch, 0.25*inch)
        self.canv.scale(1.75/8.27, 2.5/11.69)
        self.drawVirtualPage() 
Example #24
Source File: doctemplate.py    From stdm with GNU General Public License v2.0 5 votes vote down vote up
def myFirstPage(canvas, doc):
        from reportlab.lib.colors import red
        PAGE_HEIGHT = canvas._pagesize[1]
        canvas.saveState()
        canvas.setStrokeColor(red)
        canvas.setLineWidth(5)
        canvas.line(66,72,66,PAGE_HEIGHT-72)
        canvas.setFont(_baseFontNameB,24)
        canvas.drawString(108, PAGE_HEIGHT-108, "TABLE OF CONTENTS DEMO")
        canvas.setFont(_baseFontName,12)
        canvas.drawString(4 * inch, 0.75 * inch, "First Page")
        canvas.restoreState() 
Example #25
Source File: code39.py    From stdm with GNU General Public License v2.0 5 votes vote down vote up
def __init__(self, value = "", **args):
        for k, v in args.iteritems():
            setattr(self, k, v)

        if self.quiet:
            if self.lquiet is None:
                self.lquiet = max(inch * 0.25, self.barWidth * 10.0)
                self.rquiet = max(inch * 0.25, self.barWidth * 10.0)
        else:
            self.lquiet = self.rquiet = 0.0

        Barcode.__init__(self, value) 
Example #26
Source File: report_delivery.py    From callisto-core with GNU Affero General Public License v3.0 5 votes vote down vote up
def cover_page(self):
        return [
            Image(
                os.path.join(settings.BASE_DIR, api.NotificationApi.logo_path),
                3 * inch,
                3 * inch,
            ),
            Spacer(1, 18),
            Paragraph("CONFIDENTIAL", self.headline_style),
            Spacer(1, 30),
            Spacer(1, 40),
            Paragraph(self.title, self.subtitle_style),
            Spacer(1, 40),
            PageBreak(),
        ] 
Example #27
Source File: test_basic.py    From svglib with GNU Lesser General Public License v3.0 5 votes vote down vote up
def test_0(self):
        "Test length list attribute conversion."

        mapping = (
            (" 5cm 5in", [5*cm, 5*inch]),
            (" 5, 5", [5, 5]),
        )
        ac = svglib.Svg2RlgAttributeConverter()
        failed = _testit(ac.convertLengthList, mapping)
        assert len(failed) == 0 
Example #28
Source File: corp.py    From Fluid-Designer with GNU General Public License v3.0 5 votes vote down vote up
def __init__(self, atx=0, aty=0, width=2.5*inch, height=1.5*inch, powered_by=0):
        self.origin = (atx, aty)
        self.dimensions = (width, height)
        self.powered_by = powered_by 
Example #29
Source File: templates.py    From HH---POS-Accounting-and-ERP-Software with MIT License 5 votes vote down vote up
def finish(self):
        self._story = []

        self._build_invoice_info()
        self._build_service_provider_and_client_info()
        self._build_items()
        self._build_transactions()
        self._build_bottom_tip()

        kwargs = {}
        if self.is_paid:
            kwargs['onFirstPage'] = PaidStamp(7 * inch, 5.8 * inch)

        self.build(self._story, **kwargs) 
Example #30
Source File: QRCodePrinter.py    From Food-Pantry-Inventory with MIT License 5 votes vote down vote up
def __init__(self, workdir: Path):
        self.working_dir: Path = None
        self.url_prefix: str = ''
        self.box_start: int = 0
        self.label_count: int = 0
        self.output_file: str = ''
        self.full_path: Path = None
        self.pdf: Canvas = None

        # width and height are in points (1/72 inch)
        self.width: int = None
        self.height: int = None

        # database connection information
        self.con = None
        self.meta: MetaData = None
        self.box: Table = None

        # label locations on the page
        self.label_locations: List[LabelPosition] = list()
        self.compute_box_dimensions()

        # set this to the last position in the list to force a new page
        self.next_pos: int = len(self.label_locations)

        # use the page number to control first page handling
        self.page_number: int = 0

        if not workdir is None and workdir.is_dir():
            self.working_dir = workdir
        return