Printer.py 995 B

12345678910111213141516171819202122232425262728293031
  1. from PySide6.QtWidgets import QDialog
  2. from PySide6.QtPrintSupport import QPrintDialog, QPrinter
  3. from PySide6.QtGui import QPainter, QPixmap, QPageSize
  4. from PySide6.QtCore import Qt
  5. from module.wkhtmltopdf import create_pdf
  6. from pdf2image import convert_from_path
  7. class Print:
  8. def __init__(self, text: str) -> None:
  9. self.printer = QPrinter()
  10. self.printer.setFullPage(True)
  11. self.printer.setPageSize(QPageSize.A4)
  12. pdf = create_pdf(text)
  13. image = convert_from_path(pdf.get_path_to_pdf())[0].toqimage()
  14. image = QPixmap.fromImage(image)
  15. image = image.scaledToWidth(self.printer.width(), Qt.SmoothTransformation)
  16. dialog = QPrintDialog(self.printer)
  17. if dialog.exec() == QDialog.Accepted:
  18. self.handle_paint_request(image)
  19. def handle_paint_request(self, im):
  20. painter = QPainter(self.printer)
  21. painter.drawPixmap(0, 0, im)
  22. painter.end()