This commit is contained in:
itec78 2021-08-19 13:40:39 +02:00
parent cf62814378
commit de61cfa258

View file

@ -57,6 +57,52 @@ def copertina(text):
return(page) return(page)
def indice(text):
# PDF GENERATION LIBRARIES
# import the report lab PDF generation tools
from reportlab.lib.pagesizes import letter
from reportlab.lib.styles import ParagraphStyle
from reportlab.lib.units import inch
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, PageBreak
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.lib.pagesizes import A5
import io
from PyPDF2 import PdfFileWriter, PdfFileReader
parts = []
pdfmetrics.registerFont(TTFont("Roboto", "template/roboto-regular-webfont.ttf"))
style = ParagraphStyle(
name='Normal',
fontName='Roboto',
fontSize=12,
leading = 14,
leftIndent=32,
firstLineIndent=-32,
spaceBefore=5
)
for ro in text.splitlines():
ro = ro.replace(' ',' ')
ro = ro.replace('\t','    ')
parts.append(Paragraph(ro, style = style))
packet = io.BytesIO()
doc = SimpleDocTemplate(packet,
pagesize=A5,
rightMargin=20,
leftMargin=20,
topMargin=40,
bottomMargin=30)
doc.build(parts)
pdfReader = PdfFileReader(packet)
return([pdfReader.getPage(0),pdfReader.getPage(1)])
def main(): def main():
# Scarica tutti i post da Mastodon # Scarica tutti i post da Mastodon
@ -110,6 +156,8 @@ def main():
for vgo in all_vgos: for vgo in all_vgos:
vgo_num = html2text.html2text(vgo['content']).split(' ')[0] vgo_num = html2text.html2text(vgo['content']).split(' ')[0]
vgo_name = os.linesep.join([s for s in html2text.html2text(vgo['content']).splitlines() if s]).splitlines()[-1] vgo_name = os.linesep.join([s for s in html2text.html2text(vgo['content']).splitlines() if s]).splitlines()[-1]
if len(vgo_name) < 10:
vgo_name = [s for s in html2text.html2text(vgo['content']).split("\n\n") if s][-1].replace("\n"," ")
#print(vgo_num +' - '+ vgo_name) #print(vgo_num +' - '+ vgo_name)
#print(str(vgo['id']) +' '+ vgo['uri']) #print(str(vgo['id']) +' '+ vgo['uri'])
vgo_dict[vgo_num] = vgo_name vgo_dict[vgo_num] = vgo_name
@ -211,12 +259,14 @@ def main():
# aggiungere copertina # aggiungere copertina
pdfWriter.addPage(copertina(str(pagstart).zfill(3) + " - " + str(pagend).zfill(3))) pdfWriter.addPage(copertina(str(pagstart).zfill(3) + " - " + str(pagend).zfill(3)))
indtext = ""
for vgo_num in [str(x).zfill(3) for x in range(pagstart, pagend + 1)]: for vgo_num in [str(x).zfill(3) for x in range(pagstart, pagend + 1)]:
pdf_name = os.path.join('pdf', vgo_num + '.pdf') pdf_name = os.path.join('pdf', vgo_num + '.pdf')
try: try:
#print(vgo_num + " - " + vgo_dict[vgo_num]) #print(vgo_num + " - " + vgo_dict[vgo_num])
indtext = indtext + vgo_num + "\t" + vgo_dict[vgo_num] + "\n"
pdfFileObj = open(pdf_name, 'rb') pdfFileObj = open(pdf_name, 'rb')
pdfReader = PyPDF2.PdfFileReader(pdfFileObj) pdfReader = PyPDF2.PdfFileReader(pdfFileObj)
pageObj = pdfReader.getPage(0) pageObj = pdfReader.getPage(0)
@ -225,6 +275,9 @@ def main():
pass pass
# aggiungere indice ed eventualmente pagina finale # aggiungere indice ed eventualmente pagina finale
for indpag in indice(indtext):
pdfWriter.addPage(indpag)
book_name = os.path.join('books', 'book' + str(book_num).zfill(2) + '.pdf') book_name = os.path.join('books', 'book' + str(book_num).zfill(2) + '.pdf')
with open(book_name, 'wb') as pdfOutput: with open(book_name, 'wb') as pdfOutput: