12345678910111213141516171819202122232425262728293031 |
- 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
- class Print:
- def __init__(self, text: str) -> None:
-
- self.printer = QPrinter()
- self.printer.setFullPage(True)
- self.printer.setPageSize(QPageSize.A4)
-
- pdf = create_pdf(text)
- image = convert_from_path(pdf.get_path_to_pdf())[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()
-
|