Each text element is added as a Paragraph, which we've aligned to the right via Alignment.RIGHT, and accept styling arguments such as font.įinally, we've added padding to all the cells to make sure we don't place the text awkwardly near the confounds of the cells. The rows correspond to the street address, city/state, phone, email address and company website. Here, we're making a simple Table with 5 rows and 3 columns. Table_001.set_padding_on_all_cells(Decimal( 2), Decimal( 2), Decimal( 2), Decimal( 2)) Table_001.add(Paragraph( "Due Date", font= "Helvetica-Bold", horizontal_alignment=Alignment.RIGHT)) Table_001.add(Paragraph( "Invoice #", font= "Helvetica-Bold", horizontal_alignment=Alignment.RIGHT)) Table_001.add(Paragraph( "Date", font= "Helvetica-Bold", horizontal_alignment=Alignment.RIGHT)) Table_001 = Table(number_of_rows= 5, number_of_columns= 3) Let's create a separate helper method to build the invoice information in a table, which we can then use to simply add a table to the invoice in our main method: # New imports from .table.fixed_column_width_table import FixedColumnWidthTable as Tableįrom .text.paragraph import Paragraphįrom .layout_element import Alignment A common format for brevity (which incidentally also makes the code cleaner) is to use a table to store invoice data. Through its constructor, we're adding a URL pointing to the image resource and setting its width and height.īeneath the image, we'll want to add our imaginary company info (name, address, website, phone) as well as the invoice information (invoice number, date, due date). Here, we're adding an element to the layout - an Image(). Speaking of which, let's add the company logo to the layout: # New import from .image.image import Image The default value is to trim the top 10% of the page height as the margin, and we're reducing it down to 2%, since we'll want to use this space for the company logo/name. We're also making the vertical margin smaller here. Here, we're using a SingleColumnLayout since all of the content should be in a single column - we won't have a left and right side of the invoice. Page_layout.vertical_margin = page.get_page_info().get_height() * Decimal( 0.02) Since we don't want to deal with calculating coordinates - we can delegate this to a PageLayout which manages all of the content and its positions: # New imports from .page_layout.multi_column_layout import SingleColumnLayout Let's create a Document() and Page() as a blank canvas that we can add the invoice to: Additionally, the PDF class represents an API for loading and saving the Documents we create. Installing borbīorb can be downloaded from source on GitHub, or installed via pip: $ pip install borb Creating a PDF Invoice in Python with borbīorb has two intuitive key classes - Document and Page, which represent a document and the pages within it. We'll take a look at how to create a PDF invoice in Python using borb. It offers both a low-level model (allowing you access to the exact coordinates and layout if you choose to use those) and a high-level model (where you can delegate the precise calculations of margins, positions, etc to a layout manager). In this guide, we'll be using borb - a Python library dedicated to reading, manipulating and generating PDF documents. In fact, PDF is based on a scripting language - PostScript, which was the first device-independent Page Description Language. To achieve this, PDF was constructed to be interacted with via something more like a programming language, and relies on a series of instructions and operations to achieve a result. It was developed to be platform-agnostic, independent of the underlying operating system and rendering engines. The Portable Document Format (PDF) is not a WYSIWYG (What You See is What You Get) format.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |