Fummeljob hierum darum
This commit is contained in:
81
lib/PyqcrmPDF.py
Normal file
81
lib/PyqcrmPDF.py
Normal file
@@ -0,0 +1,81 @@
|
||||
from reportlab.lib.pagesizes import A4 #, letter...etc
|
||||
from reportlab.pdfgen import canvas
|
||||
from reportlab.pdfbase import pdfmetrics
|
||||
from reportlab.pdfbase.ttfonts import TTFont
|
||||
|
||||
|
||||
'''
|
||||
Need to check for sender and receiver sections
|
||||
Other alternatives can be pdf-gen, pdf-generator, pdf-play, pdf34, abdul-987-pdf
|
||||
'''
|
||||
|
||||
|
||||
class PyqcrmPDF:
|
||||
__pyq_doc = None
|
||||
__pyq_pdf = None
|
||||
__pyq_usable_width = 0
|
||||
__pyq_usable_height = 0
|
||||
__pyq_lr_margin = 20
|
||||
__pyq_tb_margin = 25
|
||||
__line = 0
|
||||
__line_height = 14
|
||||
__doc_title = "PYQCRM PDF Document"
|
||||
__doc_author = "The PYQCRM Team"
|
||||
__doc_subject = "PYQCRM Document"
|
||||
|
||||
def __init__(self, doc_title, doc_subject, pdf_file):
|
||||
self.__pyq_usable_width = A4[0] - self.__pyq_lr_margin - self.__pyq_lr_margin
|
||||
self.__pyq_usable_height = A4[1] - self.__pyq_tb_margin - self.__pyq_tb_margin
|
||||
self.__line = self.__pyq_usable_height + self.__pyq_tb_margin
|
||||
self.__pyq_pdf = canvas.Canvas(pdf_file, pagesize=A4)
|
||||
self.__pyq_pdf.setAuthor(self.__doc_author)
|
||||
self.__pyq_pdf.setTitle(doc_title if doc_title else self.__doc_title)
|
||||
self.__pyq_pdf.setSubject(doc_subject if doc_subject else self.__doc_subject)
|
||||
self.__pyq_doc = self.__pyq_pdf.beginText(self.__pyq_lr_margin, self.__line)
|
||||
self.__pyq_doc.setFont("Helvetica", 12)
|
||||
|
||||
def addLine(self, line):
|
||||
#self.__pyq_pdf.drawString(self.__pyq_lr_margin, self.__line, line) : Need to check if it does the trick!
|
||||
# print(f"Line No.: {self.__line}")
|
||||
# print(f"Line: {line}")
|
||||
if self.__pyq_doc.getY() < self.__pyq_tb_margin:
|
||||
# print("creating a new page...")
|
||||
self.__pyq_pdf.drawText(self.__pyq_doc)
|
||||
self.__pyq_pdf.showPage()
|
||||
self.__line = self.__pyq_usable_height + self.__pyq_tb_margin
|
||||
# print(f"Line No.: {self.__line}")
|
||||
self.__pyq_doc = self.__pyq_pdf.beginText(self.__pyq_lr_margin, self.__line)
|
||||
self.__pyq_doc.setFont("Helvetica", 12)
|
||||
|
||||
if pdfmetrics.stringWidth(line, "Helvetica", 12) > self.__pyq_usable_width:
|
||||
# print(f"Line width: {pdfmetrics.stringWidth(line, 'Helvetica', 12)}, Available width: {self.__pyq_usable_width}")
|
||||
words = line.split(' ')
|
||||
line = ''
|
||||
for word in words:
|
||||
# print(f"Line: {line}")
|
||||
if self.__pyq_doc.getY() < self.__pyq_tb_margin:
|
||||
# print("creating a new page...")
|
||||
self.__pyq_pdf.drawText(self.__pyq_doc)
|
||||
self.__pyq_pdf.showPage()
|
||||
self.__line = self.__pyq_usable_height + self.__pyq_tb_margin
|
||||
# print(f"Line No.: {self.__line}")
|
||||
self.__pyq_doc = self.__pyq_pdf.beginText(self.__pyq_lr_margin, self.__line)
|
||||
self.__pyq_doc.setFont("Helvetica", 12)
|
||||
|
||||
if pdfmetrics.stringWidth(line + word + ' ', "Helvetica", 12) <= self.__pyq_usable_width:
|
||||
line = line + word + ' '
|
||||
else:
|
||||
self.__pyq_doc.textLine(line)
|
||||
line = word + ' '
|
||||
# print(f"Last line: {line}")
|
||||
|
||||
if line:
|
||||
# print(f"Available line: {line}")
|
||||
self.__pyq_doc.textLine(line)
|
||||
|
||||
self.__line = self.__line + self.__line_height
|
||||
|
||||
def saveDoc(self):
|
||||
self.__pyq_pdf.drawText(self.__pyq_doc)
|
||||
self.__pyq_pdf.showPage()
|
||||
self.__pyq_pdf.save()
|
||||
Reference in New Issue
Block a user