Alex Sidorov 1 жил өмнө
parent
commit
4c9f235071
2 өөрчлөгдсөн 47 нэмэгдсэн , 20 устгасан
  1. 3 20
      module/Printer.py
  2. 44 0
      module/wkhtmltopdf.py

+ 3 - 20
module/Printer.py

@@ -2,8 +2,7 @@ from PySide6.QtWidgets import QDialog
 from PySide6.QtPrintSupport import QPrintDialog, QPrinter
 from PySide6.QtGui import QPainter, QPixmap, QPageSize
 from PySide6.QtCore import Qt
-import pdfkit
-from module import create_filename
+from module.wkhtmltopdf import create_pdf
 from pdf2image import convert_from_path
 
 
@@ -14,10 +13,9 @@ class Print:
         self.printer.setFullPage(True)
         self.printer.setPageSize(QPageSize.A4)
         
-        pdf  = CreatorPDF(text)
+        pdf = create_pdf(text)
         image = convert_from_path(pdf.get_path_to_pdf())[0].toqimage()
         image = QPixmap.fromImage(image)
-        # print(self.printer.pageRect())
         image = image.scaledToWidth(self.printer.width(), Qt.SmoothTransformation)
         
         dialog = QPrintDialog(self.printer)
@@ -30,19 +28,4 @@ class Print:
         
         painter.drawPixmap(0, 0, im)
         painter.end()
-       
-
-
-class CreatorPDF():
-    def __init__(self, text_html):
-        self.__file_name = create_filename('pdf')
-        # self.__file_name = 'pdf.pdf'
-        configuration = pdfkit.configuration(
-            wkhtmltopdf="/usr/bin/wkhtmltopdf")
-        pdfkit.from_string(text_html, self.__file_name, configuration=configuration, css=None, options={
-            "enable-local-file-access": ""
-        })
-        
-    def get_path_to_pdf(self) -> str:
-        return self.__file_name
- 
+       

+ 44 - 0
module/wkhtmltopdf.py

@@ -0,0 +1,44 @@
+import platform
+import os
+from module import create_filename
+from module.MyMessageBox import show_dialog
+from PySide6.QtWidgets import QMessageBox
+import subprocess
+import appdirs
+
+def get_path_wkhtmltopdf() -> str:
+    match platform.system():
+        case 'Linux':
+            result = subprocess.Popen(['whereis wkhtmltopdf'], shell=True, stdout=subprocess.PIPE).stdout.read()
+            return str(result).split(' ')[1]
+        case 'Windows':
+            path_window_programm = "C:\\Program\ Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe"
+            if os.path.exists(path_window_programm):
+                return path_window_programm
+            else:
+                show_dialog(QMessageBox.Icon.Critical, 
+                            "Не найдено ПО",
+                            "Не установлена программа wkhtmltopdf")
+        case _: ...
+        
+def create_pdf(html_text) -> str:
+    path_to = create_filename('pdf')
+    
+    path_temp_html = os.path.join(appdirs.user_cache_dir(), 'simple.html')
+    with open(path_temp_html, 'w') as file:
+        file.write(html_text)
+    
+    print(F"{get_path_wkhtmltopdf()} {path_temp_html} {path_to}")
+    subprocess.Popen([get_path_wkhtmltopdf(), path_temp_html, path_to])
+    return path_to
+    
+    
+def main():
+    create_pdf('<h1>Hello</h1>')
+
+
+if __name__ == "__main__":
+    main()
+    
+
+