1234567891011121314151617181920212223242526272829303132333435363738394041 |
- from PySide6.QtWidgets import QDialog
- from PySide6.QtPrintSupport import QPrintDialog, QPrinter
- from PySide6.QtGui import QPainter, QPixmap, QPageSize
- from PySide6.QtCore import Qt
- from module.wkhtmltopdf import create_pdf
- from pdf2image import convert_from_path
- import time
- from platform import system
- class Print:
- def __init__(self, text: str) -> None:
-
- self.printer = QPrinter()
- self.printer.setFullPage(True)
- self.printer.setPageSize(QPageSize.A4)
-
- pdf = create_pdf(text)
- time.sleep(5)
- image = None
- if system() in 'Windows':
- image = convert_from_path(pdf, dpi=600, poppler_path=r'poppler\bin')[0].toqimage()
- else:
- image = convert_from_path(pdf, dpi=600)[0].toqimage()
- image = QPixmap.fromImage(image)
- image = image.scaledToWidth(self.printer.width(), Qt.SmoothTransformation)
-
- dialog = QPrintDialog(self.printer)
- if dialog.exec() == QDialog.Accepted:
- self.handle_paint_request(image)
-
- def handle_paint_request(self, im):
- painter = QPainter(self.printer)
-
- painter.drawPixmap(0, 0, im)
- painter.end()
-
|