jorisschellekens / borb

borb is a library for reading, creating and manipulating PDF files in python.
https://borbpdf.com/
Other
3.4k stars 147 forks source link

Reduce margin of page #208

Closed lguerard closed 3 months ago

lguerard commented 3 months ago

Describe the bug Trying to change the margins of the PageLayout doesn't do anything.

To Reproduce

for i in [0,5,10,15]:

    # Create document
    pdf = Document()

    # Add page
    page = Page()
    pdf.add_page(page)

    # create PageLayout
    page_layout: PageLayout = SingleColumnLayout(page)
    page_layout.margin_left=Decimal(i) 
    page_layout.margin_right=Decimal(i)
    page_layout.margin_bottom=Decimal(i)
    page_layout.margin_top=Decimal(i)
    # page_layout.vertical_margin = page.get_page_info().get_height() * Decimal(0.001)
    # page_layout.horizontal_margin = page.get_page_info().get_width() * Decimal(0.001)

    page_layout.add(
        Image(
            "https://images.unsplash.com/photo-1625604029887-45f9c2f7cbc9?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8",
            width=Decimal(135),
            height=Decimal(40),
        )
    )

    out_folder = os.getcwd()
    out_name = "Test_" + str(i)

    with open(
        os.path.join(out_folder, out_name),
        "wb",
        ) as out_file_handle:
        PDF.dumps(out_file_handle, pdf)

Expected behaviour I was expecting the image to be placed more on the left with i=0

Desktop (please complete the following information):

lguerard commented 3 months ago

Actually I managed using page_layout._margin_left etc. But then my tables are completely shifted and not covering the whole width of the page...

jorisschellekens commented 3 months ago

Setting the margin on the PageLayout will shift all LayoutElement objects (that are added to that PageLayout). As far as I know, this is the desired behaviour.

I changed your code-snippet:

from decimal import Decimal

from borb.pdf import Document, Page, SingleColumnLayout, PageLayout, Paragraph, Image, PDF

def main():

    # Create document
    pdf = Document()

    # Add page
    page = Page()
    pdf.add_page(page)

    # create PageLayout
    page_layout: PageLayout = SingleColumnLayout(page)

    for i in [Decimal(0), Decimal(16), Decimal(32), Decimal(64)]:

        page_layout._margin_left = i
        page_layout._margin_right = i
        page_layout._margin_bottom = i
        page_layout._margin_top = i

        # Add Paragraph
        page_layout.add(Paragraph(f"padding set at {int(i)}"))
        page_layout.add(
            Image(
                "https://images.unsplash.com/photo-1625604029887-45f9c2f7cbc9?ixid=MnwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHx8",
                width=Decimal(135),
                height=Decimal(40),
            )
        )

    with open("output.pdf", "wb") as out_file_handle:
        PDF.dumps(out_file_handle, pdf)

if __name__ == "__main__":
    main()

Which outputs the following PDF:

image

lguerard commented 3 months ago

Thanks, I wasn't fully clear with what I wanted to achieve but as this is not a bug report, I've posted it to Stackoverflow.