46 lines
1.5 KiB
Python
46 lines
1.5 KiB
Python
import os
|
||
|
||
from reportlab.lib.pagesizes import mm
|
||
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
|
||
from reportlab.pdfbase import pdfmetrics
|
||
from reportlab.pdfbase.ttfonts import TTFont
|
||
from reportlab.platypus import SimpleDocTemplate
|
||
|
||
from constants import APP_PATH
|
||
|
||
|
||
class PDFGenerator:
|
||
def __init__(self, page_width: int, page_height: int):
|
||
ASSETS_FOLDER = os.path.join(APP_PATH, "assets")
|
||
FONTS_FOLDER = os.path.join(ASSETS_FOLDER, "fonts")
|
||
FONT_FILE_PATH = os.path.join(FONTS_FOLDER, "DejaVuSans.ttf")
|
||
self.page_width = page_width * mm
|
||
self.page_height = page_height * mm
|
||
self.number_of_spacing_pages = 1
|
||
|
||
pdfmetrics.registerFont(TTFont("DejaVuSans", FONT_FILE_PATH))
|
||
|
||
# Get standard styles and create a new style with a smaller font size
|
||
styles = getSampleStyleSheet()
|
||
self.small_style = ParagraphStyle(
|
||
"Small",
|
||
parent=styles["Normal"],
|
||
fontName="DejaVuSans", # Specify the new font
|
||
fontSize=6,
|
||
leading=7,
|
||
spaceAfter=2,
|
||
leftIndent=2,
|
||
rightIndent=2,
|
||
)
|
||
|
||
# Создание документа с указанным размером страницы
|
||
def _create_doc(self, buffer):
|
||
return SimpleDocTemplate(
|
||
buffer,
|
||
pagesize=(self.page_width, self.page_height),
|
||
rightMargin=1 * mm,
|
||
leftMargin=1 * mm,
|
||
topMargin=1 * mm,
|
||
bottomMargin=1 * mm,
|
||
)
|