Example #1
0
File: clg.py Project: phelian/clg
def main(config=None, label=None):
    # Setup PDF Generator
    pdf = PDF("A4")

    # Start initial page, always one
    pdf.add_page()

    # Command run from console with label input
    # Generate label and exit
    if label:
        # Add all fonts here
        pdf.add_font("cambria", "", "fonts/cambria.ttf", True)
        pdf.add_font("cambria", "B", "fonts/cambria_B.ttf", True)
        pdf.generate(**label)
        return

    if config:
        server = Server(config, pdf)
        server.run()
Example #2
0
def print_bib_on_rect(bib,
                      license_code=None,
                      name=None,
                      logo=None,
                      widthInches=5.9,
                      heightInches=3.9,
                      copies=1,
                      onePage=False):
    page_width = widthInches * inches_to_points
    page_height = heightInches * inches_to_points

    pdf = PDF('L', (page_height * (copies if onePage else 1), page_width))
    pdf.set_author(RaceDBVersion)
    pdf.set_title('Race Bib Number: {}'.format(bib))
    pdf.set_subject('Bib number.')
    pdf.set_creator(getpass.getuser())
    pdf.set_keywords(
        'RaceDB CrossMgr Bicycle Racing Software Database Road Time Trial MTB CycloCross RFID'
    )
    pdf.add_font('din1451alt',
                 style='',
                 fname=get_font_file('din1451alt G.ttf'),
                 uni=True)

    margin = min(page_height, page_width) / 17.5
    sep = margin / 2.5

    height = page_height - margin * 2.0
    width = page_width - margin * 2.0

    text_margin = margin
    text_height = margin * 0.4

    for c in xrange(copies):
        if c == 0 or not onePage:
            pdf.add_page()
            page_y = 0
        else:
            page_y = page_height * c
            pdf.dashed_line(0, page_y, page_width, page_y, space_length=12)

        pdf.set_font('din1451alt', '', 16)
        field = Rect(margin, margin + page_y, width, height)
        field.draw_text_to_fit(pdf, bib, Rect.AlignCenter | Rect.AlignMiddle)

        pdf.set_font('Helvetica')
        if logo:
            x = text_margin
            logo_rect = Rect(x, page_height - margin + page_y,
                             (page_width - barcode_width_max) / 2.0 - x,
                             text_height)
            logo_rect.draw_text_to_fit(pdf, logo,
                                       Rect.AlignLeft | Rect.AlignMiddle)

        if license_code:
            barcode_rect = Rect(margin, page_height - margin * 1.2 + page_y,
                                width, margin * 0.8)
            draw_code128(pdf, license_code, barcode_rect.x, barcode_rect.y,
                         barcode_rect.width, barcode_rect.height)

        if name:
            x = (page_width + barcode_width_max) / 2.0
            name_rect = Rect(x, page_height - margin + page_y,
                             page_width - text_margin - x, text_height)
            name_rect.draw_text_to_fit(pdf, name,
                                       Rect.AlignRight | Rect.AlignMiddle)

    pdf_str = pdf.output(dest='s')
    return pdf_str
Example #3
0
def print_bib_tag_label(participant,
                        sponsor_name=None,
                        left_page=True,
                        right_page=True,
                        barcode=True):
    competition = participant.competition
    license_holder = participant.license_holder

    bib = participant.bib
    name = license_holder.first_last
    if len(name) > 32:
        name = license_holder.first_last_short

    if sponsor_name is None:
        if competition.number_set and competition.number_set.sponsor:
            sponsor_name = competition.number_set.sponsor
        else:
            sponsor_name = competition.name
    system_name = 'CrossMgr'

    # Use points at the units.
    page_width = 3.9 * inches_to_points
    page_height = 2.4 * inches_to_points

    pdf = PDF('L', (page_height, page_width))
    pdf.set_author(RaceDBVersion)
    pdf.set_title('Race Bib Number: {}'.format(bib))
    pdf.set_subject(
        'Bib number and rider info to be printed as a label to apply on the chip tag.'
    )
    pdf.set_creator(getpass.getuser())
    pdf.set_keywords(
        'RaceDB CrossMgr Bicycle Racing Software Database Road Time Trial MTB CycloCross RFID'
    )

    pdf.add_font('din1451alt',
                 style='',
                 fname=get_font_file('din1451alt G.ttf'),
                 uni=True)
    pdf.add_font('Arrows',
                 style='',
                 fname=get_font_file('Arrrows-Regular.ttf'),
                 uni=True)

    margin = min(page_height, page_width) / 18.0
    sep = margin / 2.5

    height = page_height - margin * 2.0
    width = page_width - margin * 2.0

    header = Rect(margin, margin, width, height / 18.0)
    footer = Rect(margin, page_height - margin - header.height, header.width,
                  header.height)
    field = Rect(header.x, header.bottom + sep, width,
                 footer.top - header.bottom - sep * 2)

    license_code = license_holder.uci_id or license_holder.license_code

    leftArrow, rightArrow = 'A', 'a'

    font_name = 'Helvetica'
    for lp in ([True] if left_page else []) + ([False] if right_page else []):
        pdf.add_page()

        arrow = copy.deepcopy(header)
        arrow.y -= arrow.height * 0.5
        arrow.height *= 2
        pdf.set_font('Arrows')
        arrowWidth = arrow.draw_text_to_fit(
            pdf,
            leftArrow if lp else rightArrow,
            (Rect.AlignLeft if lp else Rect.AlignRight) | Rect.AlignMiddle,
            consider_descenders=True,
            convert_to_text=False,
        )
        arrowWidth += pdf.get_string_width('  ')

        header_remain = copy.deepcopy(header)
        if lp:
            header_remain.x += arrowWidth
        header_remain.width -= arrowWidth

        pdf.set_font(font_name)
        header_remain.draw_text_to_fit(
            pdf, sponsor_name,
            (Rect.AlignLeft if lp else Rect.AlignRight) | Rect.AlignMiddle,
            True)

        pdf.set_font('din1451alt', '', 16)
        field.draw_text_to_fit(pdf, bib, Rect.AlignCenter | Rect.AlignMiddle)

        pdf.set_font(font_name)
        name_width = footer.draw_text_to_fit(
            pdf, name,
            (Rect.AlignRight if lp else Rect.AlignLeft) | Rect.AlignMiddle)

        logo = copy.deepcopy(footer)
        if not lp:
            logo.x += name_width + sep
        logo.width -= name_width + sep
        if logo.width > 20:
            logo_width = logo.draw_text_to_fit(
                pdf, system_name,
                (Rect.AlignLeft if lp else Rect.AlignRight) | Rect.AlignMiddle)
        else:
            logo_width = 0

        if barcode:
            remaining_width = header.width - name_width - logo_width
            if lp:
                barcode_rect = Rect(footer.x + logo_width, footer.y,
                                    remaining_width, footer.height)
            else:
                barcode_rect = Rect(
                    footer.right - logo_width - remaining_width, footer.y,
                    remaining_width, footer.height)
            if license_code:
                draw_code128(pdf, license_code, barcode_rect.x, barcode_rect.y,
                             barcode_rect.width, barcode_rect.height)

    pdf_str = pdf.output(dest='s')
    return pdf_str