def build_document(filename, images, name, drag):
    """
    Takes a filename, a list of four PIL Image objects, a name and a value of drag
    and generates a PDF document.
    """

    page = canvas.Canvas(
        filename,
        pagesize=(432, 288),
    )

    rl_images = [convert_to_reportlab(image) for image in images]

    styles = get_styles()

    scw_bg.drawOn(page, 0, 0)

    img_width = 136.417
    img_height = 102.313

    x = [12, 151, 12, 151]
    y = [178.5, 178.5, 60, 60]

    for args in zip(rl_images, x, y):
        page.drawImage(*args, width=img_width, height=img_height)

    scw_logo.drawOn(page, 290, 107)
    erdf_logo.drawOn(page, 290, 7.184)

    name = KeepInFrame(275,
                       41.3, [Paragraph(name, styles["Normal"])],
                       mode='shrink')
    name.wrapOn(page, 275, 46)
    name.drawOn(page, 20, 9)

    drag_caption = KeepInFrame(127.559,
                               20.272, [Paragraph("Drag", styles["Heading2"])],
                               mode='shrink')
    drag_caption.wrapOn(page, 127.559, 20.272)
    drag_caption.drawOn(page, 289, 256.5)

    drag_figure = KeepInFrame(
        127.559,
        50.551,
        [Paragraph("{drag:.1f}".format(drag=drag), styles["Heading2"])],
        mode='shrink')
    drag_figure.wrapOn(page, 127.559, 50.551)
    drag_figure.drawOn(page, 289, 210)

    page.showPage()
    page.save()

    print("pdf file saved to {filename}".format(filename=filename))
Exemple #2
0
def process_fields(data, canvas, fields, pagesize, options):
    # tl = top left; br = bottom right
    default_style = options.get('default', ParagraphStyle(
            name='default-default',
            fontSize=14,
            fontName='Helvetica',
            alignment=TA_CENTER,
        ))
    for fieldname, (bbox_tl, bbox_br) in fields.items():
        style = options.get(fieldname, default_style)
        p = Paragraph(data.get(fieldname) or '',style)
        width = bbox_br[0] - bbox_tl[0]
        height = bbox_br[1] - bbox_tl[1]
        k = KeepInFrame(width*mm, height*mm, [p], mode='shrink')
        w,h = k.wrapOn(canvas, bbox_tl[1]*mm, bbox_tl[0]*mm )
        vpos = pagesize[1] - bbox_tl[1]*mm - h
        # Adjust vpos: we want vertical alignment
        vpos -= (height*mm - h)/2.0
        k.drawOn(canvas, bbox_tl[0]*mm, vpos)
    canvas.showPage()
Exemple #3
0
    def add_description(self, description, industries_type, file_path):
        base = path.basename(file_path)
        filename, file_extension = path.splitext(base)
        pdf_path = path.abspath(path.join(__file__, '../..')) + '/files/pdf/'

        millis = int(round(time.time()))
        water_pdf_name = 'watermark' + str(millis) + '.pdf'
        signature_pdf_name = 'signature' + str(millis) + '.pdf'
        output_pdf_name = 'Email:' + filename + str(millis) + '_signed.pdf'

        # create canvas for content
        c = canvas.Canvas(pdf_path + '/' + water_pdf_name,
                          pagesize=GOV_LEGAL,
                          bottomup=0)
        c_width, c_height = GOV_LEGAL

        # add background
        icolor = self._icolor(industries_type)
        c.drawImage(self.app.config['FILE_FOLDER'] +
                    "/bg_{}.png".format(icolor),
                    0,
                    0,
                    width=c_width,
                    height=c_height,
                    mask='auto',
                    anchor='nw')

        # move the origin up and to the left
        c.translate(inch, inch)

        # change color
        if icolor == 3:
            c.setFillColorRGB(0, 0, 0)
        else:
            c.setFillColorRGB(255, 255, 255)

        # add handshake title
        c.setFont("Helvetica", 23)
        c.drawString(-10, 0.5 * inch, 'The Handshake')

        # define a large font
        stylesheet = getSampleStyleSheet()
        style = stylesheet['Normal']
        style.fontName = 'Courier-Bold'
        style.fontSize = 34
        style.leading = style.fontSize * 1.2
        if icolor == 3:
            style.textColor = colors.black
        else:
            style.textColor = colors.white

        p = Paragraph(description, style)
        f = KeepInFrame(c_width + 20 - 2 * inch, c_height, [p], vAlign='TOP')
        width, height = f.wrapOn(c, c_width + 40 - 2 * inch, c_height)
        f.drawOn(c, -10, -height - 20 + (2.8 * inch))

        c.showPage()
        c.save()

        # Get the watermark file you just created
        watermark = PdfFileReader(open(pdf_path + "/" + water_pdf_name, "rb"))
        signature = None

        # create signature page if any
        if len(description) > 160:
            c_signature = canvas.Canvas(pdf_path + '/' + signature_pdf_name,
                                        pagesize=GOV_LEGAL,
                                        bottomup=0)

            # add background
            c_signature.drawImage(self.app.config['FILE_FOLDER'] +
                                  "/bg_{}.png".format(icolor),
                                  0,
                                  0,
                                  width=c_width,
                                  height=c_height,
                                  mask='auto',
                                  anchor='nw')

            c_signature.showPage()
            c_signature.save()

            signature = PdfFileReader(
                open(pdf_path + "/" + signature_pdf_name, "rb"))

        # Get our files ready
        output_file = PdfFileWriter()
        watermark_page = watermark.getPage(0)
        output_file.addPage(watermark_page)
        if signature is not None:
            output_file.addPage(signature.getPage(0))

        # finally, write "output" to document-output.pdf
        with open(pdf_path + output_pdf_name, "wb") as outputStream:
            output_file.write(outputStream)
            outputStream.close()
        return pdf_path + output_pdf_name