コード例 #1
0
def main():
    arguments = get_args()
    supported_ext_types = ['.json', '.xml']

    # Get the file extension
    _, ext = os.path.splitext(arguments.data_file)

    if ext not in supported_ext_types:
        msg = 'PDF Creator only accepts the following file types: ' \
        '{}. Got {}'
        raise RuntimeError(msg.format(str(supported_ext_types, ext)))

    doc = SimpleDocTemplate(arguments.path,
                            rightMargin=72,
                            leftMargin=36,
                            topMargin=125,
                            bottomMargin=18)

    if ext == '.xml':
        data = parse_xml(arguments.data_file)
    elif ext == '.json':
        data = parse_json(arguments.data_file)

    elements = []

    txt = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit,'
    styles = getSampleStyleSheet()
    paragraph = Paragraph(txt, styles["Normal"])
    elements.append(paragraph)

    doc.data = data
    doc.logo_path = arguments.logo

    doc.build(elements, onFirstPage=header)
コード例 #2
0
def buildPDF(pdfdoc, sheet, usr):
    scans = []
    now = datetime.datetime.now()

    pdf_doc = SimpleDocTemplate(pdfdoc,
                                pagesize=PAGE_SIZE,
                                leftMargin=MARGIN_SIZE,
                                rightMargin=MARGIN_SIZE,
                                topMargin=4 * MARGIN_SIZE,
                                bottomMargin=3 * MARGIN_SIZE)

    # data = [['', '', '', 'Grand Total', '' , pFooterGrandTotal]]

    text_data = [
        "S. No", "Vendor", "Vendor Name", "Description", "Date", "Invoice ID",
        "Amount (INR)"
    ]

    d = []
    font_size = 8
    centered = ParagraphStyle(name="centered", alignment=TA_CENTER)
    for text in text_data:
        ptext = "<font size=%s><b>%s</b></font>" % (font_size, text)
        p = Paragraph(ptext, centered)
        d.append(p)

    data = [d]
    line_num = 1
    formatted_line_data = []
    totalAmount = 0
    for i in sheet['invoices']:
        service = i['service']
        dt = i['dated']

        try:
            dateStr = dt.toString('dd-MMM-yyyy')
        except:
            dateStr = dt.strftime("%d-%m-%Y")

        line_data = [
            str(line_num),
            str(service['pk']), service['name'], i['description'], dateStr,
            i['pk'], i['amount']
        ]

        if i['attachment'] is None:
            scans.append(i['file'])
        else:
            scans.append(
                os.path.join(BASE_DIR, 'temp', i['attachment'].split('/')[-1]))
        totalAmount += i['amount']
        for item in line_data:
            ptext = "<font size=%s>%s</font>" % (font_size - 1, item)
            p = Paragraph(ptext, centered)
            formatted_line_data.append(p)
        data.append(formatted_line_data)
        formatted_line_data = []
        line_num += 1

    t = Table(data, colWidths=[35, 40, 100, 220, 50, 50, 50])

    ts = TableStyle([
        ('LINEABOVE', (0, 0), (-1, 0), 0.25, colors.gray),
        ('LINEABOVE', (0, 1), (-1, 1), 0.25, colors.gray),
        ('GRID', (0, 0), (-1, -1), 1, colors.black),
    ])

    t.setStyle(ts)

    #add some flowables

    story = []

    expHead = expanseReportHead(usr)
    story.append(expHead)
    story.append(Spacer(2.5, 0.75 * cm))
    story.append(t)
    story.append(Spacer(2.5, 0.75 * cm))

    summryParaSrc = """
    <font size='12'>Total amount : %s</font>

    """ % (totalAmount)
    story.append(Paragraph(summryParaSrc, styleN))

    for s in scans:
        story.append(PageBreak())
        story.append(FullPageImage(s))

    pdf_doc.data = {
        'barCode':
        '%s%s' % (datetime.datetime.now().strftime("%m%Y"), sheet['pk'])
    }

    pdf_doc.build(story,
                  onFirstPage=addPageNumber,
                  onLaterPages=addPageNumber,
                  canvasmaker=PageNumCanvas)