Html in un file PDF in Python senza wkhtmltopdf

0

Domanda

Ho un Plotly Multipagina(schede) Dash Applicazione. Vorrei convertire questo in un PDF file. So che c'è il dash_snapshot_engine il modulo, che non è gratuito. Quindi sto cercando una alternativa libera. Come il mio Dash applicazione sarà un file eseguibile, che non posso usare software esterni come ad esempio wkhtmltopdfPosso solo usare Python solo le biblioteche.

Qualcuno ha qualche suggerimento su come convertire un html file pdf con librerie Python?

Grazie in anticipo!

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

Migliore risposta

0

Si potrebbe aggiungere wkhtmltopdf per il vostro exe utilizzando 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)

L'edificio exe (wkhtmltopdf e il tuo script nella stessa directory):

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

Fuori:

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

Grazie per questa idea. Si tratta anche di lavorare con cx_freeze? Posso aggiungere .i file exe stesso modo?
abc

@abc: non l'ho mai usato cx_freeze, solo PyInstaller, nuitka o py2exe (per Python 2.x). Potrebbero essere utili: stackoverflow.com/questions/2553886/...
Maurice Meyer

È possibile che wkhtmltopdf utilizza i web-service per convertire html in pdf? - Anche In questo caso non è un'opzione, come il convertitore dovrebbe usare "offline" metodi...
abc

no wkhtmltpdf non tutte le operazioni di elaborazione locale stesso.
Ryan

In altre lingue

Questa pagina è in altre lingue

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