wkhtmltopdf.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import platform
  2. import os
  3. from module import create_filename
  4. from module.MyMessageBox import show_dialog
  5. from PySide6.QtWidgets import QMessageBox
  6. import subprocess
  7. import appdirs
  8. def get_path_wkhtmltopdf() -> str:
  9. match platform.system():
  10. case 'Linux':
  11. result = subprocess.Popen(['whereis wkhtmltopdf'], shell=True, stdout=subprocess.PIPE).stdout.read()
  12. return str(result).split(' ')[1]
  13. case 'Windows':
  14. path_window_programm = "C:\\Program\ Files\\wkhtmltopdf\\bin\\wkhtmltopdf.exe"
  15. if os.path.exists(path_window_programm):
  16. return path_window_programm
  17. else:
  18. show_dialog(QMessageBox.Icon.Critical,
  19. "Не найдено ПО",
  20. "Не установлена программа wkhtmltopdf")
  21. case _: ...
  22. def create_pdf(html_text) -> str:
  23. path_to = create_filename('pdf')
  24. path_temp_html = os.path.join(appdirs.user_cache_dir(), 'simple.html')
  25. with open(path_temp_html, 'w') as file:
  26. file.write(html_text)
  27. print(F"{get_path_wkhtmltopdf()} {path_temp_html} {path_to}")
  28. subprocess.Popen([get_path_wkhtmltopdf(), path_temp_html, path_to])
  29. return path_to
  30. def main():
  31. create_pdf('<h1>Hello</h1>')
  32. if __name__ == "__main__":
  33. main()