Cover added

This commit is contained in:
itec78 2021-08-19 10:33:31 +02:00
parent da36543168
commit e0ed5e50c5

View file

@ -4,6 +4,7 @@ from mastodon import Mastodon
import json import json
import datetime import datetime
import os.path import os.path
from reportlab.lib import pagesizes
import requests import requests
import html2text import html2text
import pdfkit import pdfkit
@ -14,6 +15,52 @@ locale.setlocale(locale.LC_TIME, 'it_IT.UTF-8')
def copertina(text):
from PyPDF2 import PdfFileWriter, PdfFileReader
import io
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A4
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfbase.ttfonts import TTFont
from reportlab.pdfbase.pdfmetrics import stringWidth
from reportlab.rl_config import defaultPageSize
FONT = 'Roboto'
SIZE = 48
packet = io.BytesIO()
# create a new PDF with Reportlab
pdfmetrics.registerFont(TTFont("Roboto", "template/roboto-regular-webfont.ttf"))
can = canvas.Canvas(packet, pagesize=A4)
can.setFont(FONT, SIZE)
PAGE_WIDTH = defaultPageSize[0]
#PAGE_HEIGHT = defaultPageSize[1]
text_width = stringWidth(text,FONT, SIZE)
can.drawString((PAGE_WIDTH - text_width) / 2, 150, text)
can.save()
#move to the beginning of the StringIO buffer
packet.seek(0)
new_pdf = PdfFileReader(packet)
# read your existing PDF
existing_pdf = PdfFileReader(open("copertina.pdf", "rb"))
output = PdfFileWriter()
# add the "watermark" (which is the new pdf) on the existing page
page = existing_pdf.getPage(0)
page.mergePage(new_pdf.getPage(0))
output.addPage(page)
return(page)
# Scarica tutti i post da Mastodon # Scarica tutti i post da Mastodon
print("Scarico i post") print("Scarico i post")
@ -157,13 +204,17 @@ with open('oloturiadump.json') as json_file:
print("Genero i libretti") print("Genero i libretti")
os.makedirs('books', exist_ok=True) os.makedirs('books', exist_ok=True)
for book_num in range(1, int(len(vgo_dict) / 50) + 2): for book_num in range(1, int(len(vgo_dict) / 50) + 1):
pdfWriter = PyPDF2.PdfFileWriter() pdfWriter = PyPDF2.PdfFileWriter()
print(book_num) print(book_num)
# aggiungere copertina pagstart = (book_num - 1) * 50 + 1
pagend = book_num * 50
for vgo_num in [str(x).zfill(3) for x in range((book_num - 1) * 50 + 1, book_num * 50 + 1)]: # aggiungere copertina
pdfWriter.addPage(copertina(str(pagstart).zfill(3) + " - " + str(pagend).zfill(3)))
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:
@ -181,3 +232,6 @@ for book_num in range(1, int(len(vgo_dict) / 50) + 2):
with open(book_name, 'wb') as pdfOutput: with open(book_name, 'wb') as pdfOutput:
pdfWriter.write(pdfOutput) pdfWriter.write(pdfOutput)