Wkhtmltopdf olmadan Python'da PDF'ye HTML dosyası

0

Soru

Çok Sayfalı(sekmeler) bir Çizgi Uygulamam var. Bunu bir dosyaya dönüştürmek istiyorum PDF dosya. Orada olduğunu biliyorum dash_snapshot_engine ücretsiz olmayan modül. Bu nedenle ücretsiz bir alternatif arıyorum. Dash uygulamam çalıştırılabilir olacağından, aşağıdaki gibi harici yazılımları kullanamıyorum wkhtmltopdf Sadece Python kütüphanelerini kullanabilirim.

A'yı nasıl dönüştüreceğinize dair herhangi bir öneriniz var mı html dosyaya pdf Python kütüphaneleriyle mi?

Şimdiden teşekkürler!

html pdf plotly-dash python
2021-11-22 09:53:01
1

En iyi cevabı

0

Ekleyebilirsin wkhtmltopdf senin için exe kullanım PyInstaller:

import subprocess
import sys

htmlPath = 'C:/temp/test.html'
pdfPath = 'C:/temp/test_through_exe.pdf'

if getattr(sys, 'frozen', False):
    # Change wkhtmltopdf path for exe!
    wkPath = os.path.join(sys._MEIPASS, "wkhtmltopdf.exe")
else:
    wkPath = 'C:/.../Downloads/wkhtmltopdf.exe'

with open(htmlPath, 'w') as f:
    f.write('<html><body><h1>%s</h1></body></html>' % sys.version)

cmd = '%s %s %s' % (wkPath, htmlPath, pdfPath)
print(cmd)

proc = subprocess.Popen(cmd, stderr=subprocess.PIPE, stdout=subprocess.PIPE, shell=True)
stdout, stderr = proc.communicate()

print(proc.returncode, stdout)
print(proc.returncode, stderr)

Bina exe (wkhtmltopdf ve komut dosyanız aynı dizinde):

PyInstaller --onefile --add-data ./wkhtmltopdf.exe;. test.py

Dışarı:

C:\Users\xxx\AppData\Local\Temp\_MEI33842\wkhtmltopdf.exe C:/temp/test.html C:/temp/test_through_exe.pdf
0 b''
0 b'Loading pages (1/6)\r\n[>                                                           ] 0%\r[======>
     ] 10%\r[==============================>                             ] 50%\r[============================================================] 100%\rCounting pages (2/6)                                               \r\n[============================================================] Object 1 of 1\rResolving links (4/6)                                                       \r\n[============================================================] Object 1 of 1\rLoading headers and footers (5/6)                                           \r\nPrinting pages (6/6)\r\n[>
                     ] Preparing\r[============================================================] Page 1 of 1\rDone
                                  \r\n'

enter image description here

2021-11-22 11:18:13

Teşekkür ederim bu fikri. Ayrıca cx_freeze ile çalışıyor mu? Ekleyebilirim .exe dosyaları da aynı şekilde mi?
abc

@ abc: Hiç kullanmadım cx_freeze, sade PyInstaller, nuitka veya py2exe (Python 2 için.x). Yardımcı olabilir: stackoverflow.com/questions/2553886/...
Maurice Meyer

Bu mümkün mü wkhtmltopdf html'yi pdf'ye dönüştürmek için web servisini kullanıyor mu? - Bu durumda, dönüştürücü "çevrimdışı" yöntemleri kullanması gerektiğinden, bu bir seçenek değildir...
abc

hayır wkhtmltpdf tüm işlemleri yerel olarak kendisi yapar.
Ryan

Diğer dillerde

Bu sayfa diğer dillerde

Русский
..................................................................................................................
Italiano
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................