139 lines
3.7 KiB
Python
139 lines
3.7 KiB
Python
from fpdf import FPDF
|
|
"""
|
|
A script for generating A5 size pdf zines
|
|
with a text-file input, under development,
|
|
GPL3 Licence, Mara Karagianni 22 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()
|
|
|
|
# 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)
|
|
|
|
class Zine(FPDF):
|
|
def __init__(self):
|
|
super().__init__()
|
|
self.cur_x = current_x
|
|
self.cur_y = current_y
|
|
self.max_x = max_left_margin
|
|
self.max_y = max_height
|
|
self.w_cell = cell_width
|
|
self.text = text
|
|
|
|
def new_signature(self):
|
|
# create new page/signature
|
|
self.add_page()
|
|
self.set_xy(self.cur_x, self.cur_.y)
|
|
self.set_margins(top=15, left=20, right=20)
|
|
print('NEW SIGNATURE')
|
|
|
|
def new_page(self):
|
|
print('*****')
|
|
print("BOTTOM OF PAGE, height is {}".format(pdf.get_y()))
|
|
print('*****')
|
|
self.set_xy(self.x, self.y)
|
|
self.set_margins(
|
|
top=15, left=160, right=20)
|
|
|
|
def create_pages(self):
|
|
index = 0
|
|
for line in self.text:
|
|
# check current page height and if we are in the right side of page
|
|
if self.get_y() >= self.max_y and self.get_x() == self.max_x:
|
|
self.new_signature()
|
|
|
|
if self.get_y() >= self.max_y:
|
|
self.new_page()
|
|
|
|
self.multi_cell(
|
|
self.w_cell, 166, self.text[index], 0, align='L')
|
|
index += 1
|
|
|
|
# def create_pages(self):
|
|
# index = 0
|
|
# for line in self.text:
|
|
# # check current page height and if we are in the right side of page
|
|
# if pdf.get_y() >= self.max_y and pdf.get_x() == self.max_x:
|
|
# self.new_signature()
|
|
#
|
|
# if pdf.get_y() >= self.max_y:
|
|
# self.new_page()
|
|
#
|
|
# pdf.multi_cell(
|
|
# self.w_cell, cell_height, self.text[index], 0, align='L')
|
|
# index += 1
|
|
|
|
zine = Zine()
|
|
zine.create_pages()
|
|
pdf.output('new_trial.pdf')
|
|
|
|
|
|
# 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
|