wkhtmltopdf.py 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. import platform
  2. import os
  3. from module import create_filename
  4. import subprocess
  5. import appdirs
  6. from logger import logger
  7. def get_path_wkhtmltopdf() -> str:
  8. match platform.system():
  9. case 'Linux':
  10. result = subprocess.Popen(['whereis wkhtmltopdf'], shell=True, stdout=subprocess.PIPE).stdout.read()
  11. path_programm = str(result).split(' ')[1]
  12. case 'Windows':
  13. path_programm = os.path.join("C:\\", "Program Files", "wkhtmltopdf", "bin", "wkhtmltopdf.exe")
  14. case _:
  15. return None
  16. if os.path.isfile(path_programm) and "wkhtmltopdf" in path_programm:
  17. logger.info(F"Find programm: {path_programm}")
  18. return path_programm
  19. else:
  20. return None
  21. def create_pdf(html_text) -> str:
  22. path_to = create_filename('pdf')
  23. path_temp_html = os.path.join(appdirs.user_cache_dir(), 'simple.html')
  24. with open(path_temp_html, 'w', encoding='utf-8') as file:
  25. file.write(html_text)
  26. logger.info(F"{get_path_wkhtmltopdf()} --enable-local-file-access {path_temp_html} {path_to}")
  27. subprocess.Popen([get_path_wkhtmltopdf(), "--enable-local-file-access", path_temp_html, path_to])
  28. return path_to
  29. def main():
  30. create_pdf('<h1>Hello</h1>')
  31. if __name__ == "__main__":
  32. main()