Source code for bec_lib.pdf_writer
"""
This module contains a class for writing pdfs.
"""
import datetime
from fpdf import FPDF
[docs]
class BECPDF(FPDF):
"""Custom PDF class for BEC."""
[docs]
class PDFWriter:
"""Class for writing pdfs."""
LEFT_MARGIN = 25
TOP_MARGIN = 20
RIGHT_MARGIN = 20
FONT_SIZE = 10
def __init__(self, file: str, title: str = None, font="Courier") -> None:
"""Filer writer for pdfs.
Args:
file (str): full path to the output file (including file extension).
title (str, optional): Title of the document. Defaults to None.
"""
self.file = file
self.title = title
self.font = font
self._pdf = BECPDF(orientation="P", unit="mm", format="A4")
if self.title:
self._pdf.set_title(self.title)
self._pdf.add_page()
self._pdf.set_margins(left=self.LEFT_MARGIN, top=self.TOP_MARGIN, right=self.RIGHT_MARGIN)
self._pdf.set_font(self.font, "", self.FONT_SIZE)
def __enter__(self):
return self
def __exit__(self, *exc):
self.close()
[docs]
def write(self, text: str):
"""add text to the pdf"""
self._pdf.set_x(self.LEFT_MARGIN)
self._pdf.multi_cell(0, self.FONT_SIZE // 2, text)
[docs]
def close(self):
"""close the pdf and write to disk"""
self._pdf.output(self.file, "F")
if __name__ == "__main__": # pragma: no cover
header = (
" \n" * 3
+ " ::: ::: ::: ::: :::: ::: ::::::::::: \n"
+ " :+: :+: :+: :+:+: :+:+: :+:+: :+: :+: \n"
+ " +:+ +:+ +:+ +:+ +:+:+ +:+ :+:+:+ +:+ +:+ \n"
+ " +#+ +#++:++#++: +#+ +:+ +#+ +#+ +:+ +#+ +#+ \n"
+ " +#+ +#+ +#+ +#+ +#+ +#+ +#+#+# +#+ \n"
+ " #+# #+# #+# #+# #+# #+# #+#+# #+# \n"
+ " ########## ### ### ### ### ### #### ########### \n"
)
with PDFWriter("test_output.pdf") as file:
file.write(header)