from fpdf import FPDF """ A script for generating A5 size pdf zines with a text-file input, under development, GPL3 Licence, Mara Karagianni March 2021 """ # Variables header_font = 'helvetica' header_font_size = 14 text_font = 'helvetica' text_font_size = 10 top_margin = 15 left_margin = 20 right_margin = 20 max_left_margin = 160 max_height = 166 current_x = 20 current_y = 30 max_y = 166 max_x = 160 cell_width = 120 cell_header_height = 16 cell_height = 8 pdf = FPDF(orientation="L", unit="mm", format="A4") pdf.set_font(header_font, 'B', header_font_size) pdf.set_margins(top=top_margin, left=left_margin, right=right_margin) pdf.add_page() print("TOP margin is {}".format(pdf.get_y())) # Header title = 'Painful Mailman Migrations' pdf.cell(cell_width, cell_header_height, title, 0, ln=1, align='C') pdf.dashed_line( current_x, current_y, current_x+cell_width, current_y, dash_length=3, space_length=3) # set font for all text pdf.set_font(text_font, size=text_font_size) # file to get the text filename = "./notes_sample" text = open(filename).readlines() # start x and y position pdf.set_xy(current_x, current_y+9) index = 0 for line in text: # check current page height if pdf.get_y() >= max_height and pdf.get_x() == max_left_margin: # create new page/signature pdf.add_page() pdf.set_xy(current_x, current_y) pdf.set_margins(top=top_margin, left=left_margin, right=right_margin) print('NEW SIGNATURE') if pdf.get_y() >= max_height: print('*****') print("BOTTOM OF PAGE, height is {}".format(pdf.get_y())) print('*****') pdf.set_xy(max_left_margin, current_y) pdf.set_margins( top=top_margin, left=max_left_margin, right=right_margin) if line == "\n": # debug if empty lines are detected print('EMPTY LINE {}'.format(line)) print("x {}, y {}".format(pdf.get_x(), pdf.get_y())) print('============') pdf.multi_cell(cell_width, cell_height, text[index], 0, align='L') index += 1 # save pdf file pdf.output('trial.pdf')