当前位置: 首页>>代码示例>>Python>>正文


Python Canvas.drawAlignedString方法代码示例

本文整理汇总了Python中reportlab.pdfgen.canvas.Canvas.drawAlignedString方法的典型用法代码示例。如果您正苦于以下问题:Python Canvas.drawAlignedString方法的具体用法?Python Canvas.drawAlignedString怎么用?Python Canvas.drawAlignedString使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在reportlab.pdfgen.canvas.Canvas的用法示例。


在下文中一共展示了Canvas.drawAlignedString方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: invoice_pdf

# 需要导入模块: from reportlab.pdfgen.canvas import Canvas [as 别名]
# 或者: from reportlab.pdfgen.canvas.Canvas import drawAlignedString [as 别名]
def invoice_pdf(gnc_file, invoice_number, pdf_file):
    uri = "xml://{0}".format(os.path.abspath(gnc_file))
    ses = Session(uri, is_new=False)
    try:
        book = ses.get_book()
        commod_table = book.get_table()
        USD = commod_table.lookup("CURRENCY", "USD")

        invoice = book.InvoiceLookupByID(invoice_number)
        client = invoice.GetOwner()
        client_addr = client.GetAddr()

        pdf = Canvas(pdf_file, bottomup=False, pagesize=letter)

        pdf.setFont("Helvetica-Bold", 24)
        pdf.setFillColor(colors.lightgrey)
        pdf.drawCentredString(letter[0] / 2, inch * 0.75, "INVOICE")

        font_height = 10
        pdf.setFont("Helvetica", font_height)
        pdf.setFillColor(colors.black)
        from_header = pdf.beginText(inch * 0.75, inch * 0.75)
        to_header = pdf.beginText(inch * 0.75, inch * 2.25)

        header_file = "{0}/.gnc-invoice-header".format(os.environ["HOME"])
        with open(header_file, "r") as f:
            for line in f:
                from_header.textLine(line.strip())

        to_fields = [
            client.GetName(),
            client_addr.GetName(),
            client_addr.GetAddr1(),
            client_addr.GetAddr2(),
            client_addr.GetAddr3(),
            client_addr.GetAddr4(),
        ]
        for field in to_fields:
            if field:
                to_header.textLine(field)

        pdf.drawText(from_header)
        pdf.drawText(to_header)

        #
        # This is the summary table / box in the mid-upper right.
        #
        table_data = (
            ("Invoice #", invoice.GetID()),
            ("Date", invoice.GetDatePosted().strftime("%Y-%m-%d")),
            ("Amount Due (USD)", "${0:0.2f}".format(invoice.GetTotal().to_double())),
        )
        x = inch * 4.5
        y = (inch * 2.25) - font_height
        width = inch * 3
        height = inch * 0.75
        num_rows = 3
        num_cols = 2
        col_width = width / num_cols
        row_height = height / num_rows
        for row in range(num_rows):
            for col in range(num_cols):
                rect_x = x + (col_width * col)
                rect_y = y + (row_height * row)

                pdf.setFillColor(colors.darkgrey)
                pdf.rect(rect_x, rect_y, col_width, row_height, stroke=True, fill=(col == 0))

                pdf.setFillColor(colors.black)
                if col:
                    pdf.drawAlignedString(rect_x + col_width, rect_y + font_height + 2, table_data[row][col], "%")
                else:
                    pdf.drawString(rect_x + 5, rect_y + font_height + 2, table_data[row][col])

        #
        # This is the detail table in the lower half.
        #
        table_data = [("Date", "Description", "Hours", "Rate ($)", "Line Total")]
        for entry in [Entry(instance=e) for e in invoice.GetEntries()]:
            qty = GncNumeric(instance=entry.GetQuantity()).to_double()
            rate = GncNumeric(instance=entry.GetInvPrice()).to_double()
            line_total = GncNumeric(instance=entry.ReturnValue(True)).to_double()
            row = [
                entry.GetDate().strftime("%Y-%m-%d"),
                entry.GetDescription(),
                "{0:0.2f}".format(qty),
                "{0:0.2f}".format(rate),
                "{0:0.2f}".format(line_total),
            ]
            table_data.append(row)

        x = inch * 0.75
        y = inch * 4.0

        # Let column 1 consume the rest of the space.
        width = inch * 6.75
        widths = [80, 0, 50, 50, 80]
        widths[1] = width - sum(widths)

        height = font_height + 2 + 2  # 2pt spacing above and below.
#.........这里部分代码省略.........
开发者ID:bstpierre,项目名称:gnucash-util,代码行数:103,代码来源:invoice_pdf.py


注:本文中的reportlab.pdfgen.canvas.Canvas.drawAlignedString方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。