Fișier Html pentru a PDF în Python fără wkhtmltopdf

0

Problema

Am o Plotly mai multe Pagini(tab-uri), Dash Aplicație. Aș dori pentru a converti de la un PDF fișierul. Știu că nu este dash_snapshot_engine modulul, care nu este gratuit. Prin urmare, eu sunt în căutarea pentru o alternativă gratuită. Ca Dash aplicație va fi un executabil, nu pot folosi software-ul extern, cum ar fi wkhtmltopdfPot folosi doar Python numai biblioteci.

Are cineva sugestii cu privire la modul de a converti un html fișier pentru a pdf cu biblioteci Python?

Multumesc anticipat!

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

Cel mai bun răspuns

0

Ai putea adăuga wkhtmltopdf la exe folosind 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)

Construirea exe (wkhtmltopdf și script-ul în același director):

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

Out:

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

Vă mulțumesc pentru această idee. Este, de asemenea, de lucru cu cx_freeze? Pot să adaug .exe, fișiere de același fel?
abc

@abc: nu am folosit niciodată cx_freezedoar PyInstaller, nuitka sau py2exe (pentru Python 2.x). Ar putea fi de ajutor: stackoverflow.com/questions/2553886/...
Maurice Meyer

Este posibil ca wkhtmltopdf folosește un serviciu web pentru a converti html pentru a pdf? - În acest caz, nu este o opțiune, ca converter ar trebui să utilizeze "offline" metode de...
abc

nu wkhtmltpdf nu toate de prelucrare la nivel local în sine.
Ryan

În alte limbi

Această pagină este în alte limbi

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