Printer.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. import time
  8. from platform import system
  9. class Print:
  10. def __init__(self, text: str) -> None:
  11. self.printer = QPrinter()
  12. self.printer.setFullPage(True)
  13. self.printer.setPageSize(QPageSize.A4)
  14. pdf = create_pdf(text)
  15. time.sleep(5)
  16. image = None
  17. if system() in 'Windows':
  18. image = convert_from_path(pdf, dpi=600, poppler_path=r'poppler\bin')[0].toqimage()
  19. else:
  20. image = convert_from_path(pdf, dpi=600)[0].toqimage()
  21. image = QPixmap.fromImage(image)
  22. image = image.scaledToWidth(self.printer.width(), Qt.SmoothTransformation)
  23. dialog = QPrintDialog(self.printer)
  24. if dialog.exec() == QDialog.Accepted:
  25. self.handle_paint_request(image)
  26. def handle_paint_request(self, im):
  27. painter = QPainter(self.printer)
  28. painter.drawPixmap(0, 0, im)
  29. painter.end()