Cover added
This commit is contained in:
parent
da36543168
commit
e0ed5e50c5
1 changed files with 57 additions and 3 deletions
|
@ -4,6 +4,7 @@ from mastodon import Mastodon
|
|||
import json
|
||||
import datetime
|
||||
import os.path
|
||||
from reportlab.lib import pagesizes
|
||||
import requests
|
||||
import html2text
|
||||
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
|
||||
|
||||
print("Scarico i post")
|
||||
|
@ -157,13 +204,17 @@ with open('oloturiadump.json') as json_file:
|
|||
|
||||
print("Genero i libretti")
|
||||
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()
|
||||
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')
|
||||
|
||||
try:
|
||||
|
@ -181,3 +232,6 @@ for book_num in range(1, int(len(vgo_dict) / 50) + 2):
|
|||
with open(book_name, 'wb') as pdfOutput:
|
||||
pdfWriter.write(pdfOutput)
|
||||
|
||||
|
||||
|
||||
|
||||
|
|
Loading…
Reference in a new issue