Skip to content

Simple Letterhead example

Rishikesh Agrawani edited this page Mar 25, 2018 · 5 revisions

#1


Download this image and save it with name dell_logo.png.

dell_logo.png.

#2

Copy and save the below code in lorem_ipsum.py.

LOREM_IPSUM_TEXT1 = """Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. 

	Donec enim diam vulputate ut pharetra sit amet. Sit amet aliquam id diam maecenas. Ornare lectus sit amet est placerat in egestas. Eu volutpat odio facilisis mauris.Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Consequat nisl vel pretium lectus quam. Vitae congue eu consequat ac felis donec et odio. Sed lectus vestibulum mattis ullamcorper velit sed ullamcorper morbi. Senectus et netus et malesuada fames ac turpis egestas."""

LOREM_IPSUM_TEXT2 = """Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Pulvinar pellentesque habitant morbi tristique senectus et netus et malesuada. Neque gravida in fermentum et. Pellentesque id nibh tortor id aliquet lectus proin. In tellus integer feugiat scelerisque. In nibh mauris cursus mattis molestie. Commodo ullamcorper a lacus vestibulum sed arcu non odio euismod. Turpis egestas sed tempus urna et pharetra pharetra. Vitae elementum curabitur vitae nunc sed velit dignissim sodales. Egestas pretium aenean pharetra magna ac placerat vestibulum lectus mauris. Fringilla urna porttitor rhoncus dolor purus non enim praesent elementum. Ut tristique et egestas quis.

Semper auctor neque vitae tempus quam pellentesque. Tempor commodo ullamcorper a lacus vestibulum sed arcu non. Aliquet nec ullamcorper sit amet risus nullam eget. Id venenatis a condimentum vitae sapien pellentesque habitant morbi. Nisl nunc mi ipsum faucibus. Auctor eu augue ut lectus arcu. Dictum varius duis at consectetur lorem donec. Suspendisse interdum consectetur libero id faucibus. Et pharetra pharetra massa massa ultricies mi quis. Phasellus vestibulum lorem sed risus.

Vestibulum lorem sed risus ultricies. Mauris pharetra et ultrices neque ornare aenean. Augue eget arcu dictum varius duis at consectetur lorem. Donec adipiscing tristique risus nec feugiat in fermentum posuere urna. At imperdiet dui accumsan sit amet. Pellentesque pulvinar pellentesque habitant morbi tristique senectus. Rhoncus urna neque viverra justo nec ultrices dui. Ultrices sagittis orci a scelerisque purus semper eget. Dictum at tempor commodo ullamcorper a. Risus nec feugiat in fermentum. Ipsum faucibus vitae aliquet nec ullamcorper sit amet risus. Et ligula ullamcorper malesuada proin libero nunc. Purus gravida quis blandit turpis cursus in hac.

Rhoncus dolor purus non enim praesent. Odio aenean sed adipiscing diam donec. Non sodales neque sodales ut etiam sit. Vel eros donec ac odio tempor orci dapibus. Phasellus faucibus scelerisque eleifend donec. Blandit cursus risus at ultrices mi tempus. Vehicula ipsum a arcu cursus vitae congue mauris rhoncus. Ut tristique et egestas quis ipsum suspendisse ultrices gravida. Leo urna molestie at elementum eu. Mi quis hendrerit dolor magna eget est lorem ipsum dolor. Cursus in hac habitasse platea dictumst quisque. Ut porttitor leo a diam sollicitudin. Dignissim convallis aenean et tortor at risus viverra. Velit ut tortor pretium viverra.

Ornare lectus sit amet est placerat in egestas erat. Dolor morbi non arcu risus quis varius quam. Non blandit massa enim nec dui nunc mattis enim ut. Malesuada fames ac turpis egestas maecenas pharetra convallis posuere. At consectetur lorem donec massa sapien faucibus et molestie ac. Urna porttitor rhoncus dolor purus non. Laoreet non curabitur gravida arcu ac tortor dignissim convallis. Facilisi nullam vehicula ipsum a arcu cursus vitae. Amet nisl suscipit adipiscing bibendum. Nibh tellus molestie nunc non blandit massa enim nec dui. Est velit egestas dui id. Vitae proin sagittis nisl rhoncus mattis rhoncus urna. Nam aliquam sem et tortor consequat id porta nibh. Scelerisque purus semper eget duis at. Sollicitudin nibh sit amet commodo nulla facilisi nullam vehicula ipsum. Sit amet consectetur adipiscing elit duis. Consequat semper viverra nam libero justo laoreet sit.


Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Id neque aliquam vestibulum morbi blandit cursus risus at ultrices. Dignissim enim sit amet venenatis urna cursus. Sed risus ultricies tristique nulla aliquet. Turpis egestas maecenas pharetra convallis posuere morbi leo. Id neque aliquam vestibulum morbi blandit cursus risus at ultrices. Dignissim enim sit amet venenatis urna cursus.

Ornare lectus sit amet est placerat in egestas erat. Dolor morbi non arcu risus quis varius quam. Non blandit massa enim nec dui nunc mattis enim ut. Malesuada fames ac turpis egestas maecenas pharetra convallis posuere. At consectetur lorem donec massa sapien faucibus et molestie ac. Urna porttitor rhoncus dolor purus non. Laoreet non curabitur gravida arcu ac tortor dignissim convallis. Facilisi nullam vehicula ipsum a arcu cursus vitae. Amet nisl suscipit adipiscing bibendum. Nibh tellus molestie nunc non blandit massa enim nec dui. Est velit egestas dui id. Vitae proin sagittis nisl rhoncus mattis rhoncus urna. Nam aliquam sem et tortor consequat id porta nibh. Scelerisque purus semper eget duis at. Sollicitudin nibh sit amet commodo nulla facilisi nullam vehicula ipsum. Sit amet consectetur adipiscing elit duis. Consequat semper viverra nam libero justo laoreet sit.
"""

LOREM_IPSUM_TEXT3 = """Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Tincidunt lobortis feugiat vivamus at augue eget. Quisque egestas diam in arcu cursus euismod quis viverra. Dolor sit amet consectetur adipiscing elit pellentesque. Orci ac auctor augue mauris augue neque gravida."""

#3


Copy and paste the below code in any empty python file and run it. You will get beautiful PDF document(as a letterhead). You may change and customize the code based on your need.

The example is the modified version of code available at https://jeltef.github.io/PyLaTeX/current/examples/complex_report.html. So you will find most of lines commented.

"""
    {
        "createdOn": "13 Mar 2018, Tue",
        "aim": "To create a simple letterhead document",
        "modifiedBy": "Rishikesh Agrawani"
    }
"""

import os

from pylatex import Document, PageStyle, Head, Foot, MiniPage, \
    StandAloneGraphic, MultiColumn, Tabu, LongTabu, LargeText, MediumText, \
    TextColor, LineBreak, NewPage, Tabularx, TextColor, simple_page_number
from pylatex.utils import bold, NoEscape
import lorem_ipsum
from pylatex.section import Paragraph

logo_name = "dell_logo.png"

def generate_unique():
    geometry_options = {
        "head": "40pt",
        "margin": "0.5in",
        "bottom": "0.6in",
        "includeheadfoot": True
    }
    doc = Document(geometry_options=geometry_options, document_options="a4paper")

    # Generating first page style
    first_page = PageStyle("firstpage")

    # Header image
    with first_page.create(Head("L")) as header_left:
        with header_left.create(MiniPage(width=NoEscape(r"0.49\textwidth"),
                                         pos='c')) as logo_wrapper:
            logo_file = os.path.join(os.path.dirname(__file__),
                                     logo_name)
            logo_wrapper.append(StandAloneGraphic(image_options="width=50px",
                                filename=logo_file))

    # Add document title
    with first_page.create(Head("R")) as right_header:
        with right_header.create(MiniPage(width=NoEscape(r"0.49\textwidth"),
                                 pos='c', align='r')) as title_wrapper:
            title_wrapper.append(LargeText(bold("Allexix India Pvt Ltd")))
            title_wrapper.append(LineBreak())
            title_wrapper.append(MediumText(bold("C-250, Pachpedi Naka")))
            title_wrapper.append(LineBreak())
            # title_wrapper.append(TextColor("gray", MediumText(bold("Raipur, CG"))))
            title_wrapper.append(MediumText(bold("Raipur, CG")))

    # Add footer
    # with first_page.create(Foot("C")) as footer:
    #     message = "Important message please read"
    #     with footer.create(Tabularx(
    #             "X X X X",
    #             width_argument=NoEscape(r"\textwidth"))) as footer_table:

    #         footer_table.add_row(
    #             [MultiColumn(4, align='l', data=TextColor("blue", message))])
    #         footer_table.add_hline(color="blue")
    #         footer_table.add_empty_row()

    #         branch_address = MiniPage(
    #             width=NoEscape(r"0.25\textwidth"),
    #             pos='t')
    #         branch_address.append("960 - 22nd street east")
    #         branch_address.append("\n")
    #         branch_address.append("Saskatoon, SK")

    #         document_details = MiniPage(width=NoEscape(r"0.25\textwidth"),
    #                                     pos='t', align='r')
    #         document_details.append("1000")
    #         document_details.append(LineBreak())
    #         document_details.append(simple_page_number())

    #         footer_table.add_row([branch_address, branch_address,
    #                               branch_address, document_details])

    # Newly added
    with first_page.create(Foot("L")) as right_footer:
        with right_footer.create(MiniPage(width=NoEscape(r"0.49\textwidth"),
                                 pos='l', align='l')) as contact_wrapper:
            contact_wrapper.append(MediumText(bold("Rishikesh Agrawani")))
            contact_wrapper.append(LineBreak())
            contact_wrapper.append(MediumText("[email protected]"))
            contact_wrapper.append(LineBreak())
            contact_wrapper.append(MediumText("+91 7877681234"))

    doc.preamble.append(first_page)
    # End first page style

    # Add customer information
    with doc.create(Tabu("X[r]")) as first_page_table:
        # customer = MiniPage(width=NoEscape(r"0.9\textwidth"), pos='h')
        # customer.append("Verna Volcano")
        # customer.append("\n")
        # customer.append("For some Person")
        # customer.append("\n")
        # customer.append("Address1")
        # customer.append("\n")
        # customer.append("Address2")
        # customer.append("\n")
        # customer.append("Address3")

        # Add branch information
        branch = MiniPage(width=NoEscape(r"0.49\textwidth"), pos='t!',
                          align='r')
        branch.append("http://allexix.com")
        branch.append(LineBreak())
        branch.append("[email protected]")
        # branch.append(LineBreak())
        # branch.append("+91 7877681234")

        first_page_table.add_row([branch]) # [customer, branch]
        first_page_table.add_empty_row()

    doc.change_document_style("firstpage")
    doc.add_color(name="lightgray", model="gray", description="0.80")

    # Add statement table
    # with doc.create(LongTabu("X[l] X[2l] X[r] X[r] X[r]",
    #                          row_height=1.5)) as data_table:
    #     data_table.add_row(["date",
    #                         "description",
    #                         "debits($)",
    #                         "credits($)",
    #                         "balance($)"],
    #                        mapper=bold,
    #                        color="lightgray")
    #     data_table.add_empty_row()
    #     data_table.add_hline()
    #     row = ["2016-JUN-01", "Test", "$100", "$1000", "-$900"]
    #     for i in range(25):
    #         if (i % 2) == 0:
    #             data_table.add_row(row, color="lightgray")
    #         else:
    #             data_table.add_row(row)

    with doc.create(Paragraph("Dear Rishikesh,")) as para1:
        para1.append(lorem_ipsum.LOREM_IPSUM_TEXT2)


    # doc.append(NewPage())

    # Add cheque images
    # with doc.create(LongTabu("X[c] X[c]")) as cheque_table:
    #     cheque_file = os.path.join(os.path.dirname(__file__),
    #                                'facebook_logo.png')
    #     cheque = StandAloneGraphic(cheque_file, image_options="width=200px")
    #     for i in range(0, 20):
    #         cheque_table.add_row([cheque, cheque])

    doc.generate_pdf("complex_report", clean_tex=False)

generate_unique()
Clone this wiki locally