Dynamically adjust size of page #678
Replies: 2 comments 2 replies
-
Hi @NadaAfterwards! Interesting use case. Thanks for opening the discussion. One thing that I do not quite understand: you mentioned that your document will be printed. Overall, it seems that your are trying to build a layout system for structuring your tickets.
If you want to stick with
|
Beta Was this translation helpful? Give feedback.
-
The tricky part here is that the PDF format has the coordinate origin at the bottom of the page, while fpdf2 has it at the top. from fpdf import FPDF
def draw_label(pdf):
for i in range(10):
pdf.cell(txt=f'{i:02}: ABCDEFGHIJKLMNOPQRST', new_x="LEFT", new_y="NEXT")
pdf = FPDF()
pdf.set_margins(5, 5)
pdf.set_font('Courier', '', 11)
# full 80x80 mm page.
pdf.add_page(format=(80,80))
draw_label(pdf)
# page cropped to 80x50 mm from the top.
pdf.add_page(format=(80,80))
draw_label(pdf)
page2 = pdf.pages[2]
page2.set_dimensions(pdf.w_pt, 50*pdf.k)
# content rendered flipped by 180° around page center, then cropped.
pdf.add_page(format=(80,80))
with pdf.rotation(180, x=pdf.w/2, y=pdf.h/2):
draw_label(pdf)
page3 = pdf.pages[3]
page3.set_dimensions(pdf.w_pt, 50*pdf.k)
doc = pdf.output('pagesizetest.pdf') The image shows the three different versions of the output: edit: |
Beta Was this translation helpful? Give feedback.
-
Hi! I'm improving our script at my job to generate tickets for our queue management system, going from PyFPDF to FPDF2 and expanding the script's functionality. One of my objectives is to try to make most things dynamic such as the placing of elements (cell, multi_cell and image) as well as allowing us to easily modify which elements to be rendered and how much space to put between one another.
This means I don't truly know the height of my PDF page until all elements are rendered and as far as I can tell, there isn't a way to go back and update the dimensions of the used FPDF object. A plausible solution, which I somewhat dread, would be to create a new FPDF object after finding the required height, redoing all the elements there and outputing that instead. Seems slow but it could work...
I do want to clarify that I cannot simply leave empty space at the end since we have to print the document with a rotation of 180 degrees, causing the end of document to be the beginning. Our printers can't be configured to skip empty lines from the top.
I'm wondering if there are any other better solutions to this problem, I do understand our use case might be somewhat outside the scope of this library.
Cheers!
Beta Was this translation helpful? Give feedback.
All reactions