openPdf.py 508 B

12345678910111213141516171819
  1. import sys
  2. import subprocess
  3. import tempfile
  4. import os
  5. def open_with_system_viewer(pdf_data):
  6. with tempfile.NamedTemporaryFile(suffix='.pdf', delete=False) as f:
  7. f.write(pdf_data)
  8. temp_path = f.name
  9. # 打开系统默认阅读器
  10. if sys.platform.startswith('darwin'): # macOS
  11. subprocess.call(('open', temp_path))
  12. elif os.name == 'nt': # Windows
  13. os.startfile(temp_path)
  14. elif os.name == 'posix': # Linux
  15. subprocess.call(('xdg-open', temp_path))