Archivo Html a PDF en Python sin wkhtmltopdf

0

Pregunta

Tengo un Plotly de varias páginas(pestañas) Guión de la Aplicación. Me gustaría convertir esto en un PDF archivo. Sé que no es el dash_snapshot_engine el módulo, que no es gratis. Por lo tanto, estoy buscando una alternativa libre. Como mi Dash aplicación va a ser un ejecutable, no puedo usar un software externo como wkhtmltopdfSólo puedo usar Python sólo las bibliotecas.

Tiene alguien alguna sugerencia sobre cómo convertir un html archivo pdf con librerías de Python?

Gracias de antemano!

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

Mejor respuesta

0

Se podría añadir wkhtmltopdf a su exe el uso de 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)

La construcción de la exe (wkhtmltopdf y su secuencia de comandos en el mismo directorio):

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

Fuera:

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

Gracias por esta idea. Es también trabajar con cx_freeze? Puedo añadir .exe archivos de la misma manera?
abc

@abc: nunca he usado cx_freeze, sólo PyInstaller, nuitka o py2exe (para Python 2.x). Podría ser útil: stackoverflow.com/questions/2553886/...
Maurice Meyer

Es posible que wkhtmltopdf utiliza el servicio web para convertir html a pdf? - En este caso no es una opción, ya que el convertidor debe utilizar "sin conexión" métodos de...
abc

no wkhtmltpdf hace todo el procesamiento local en sí.
Ryan

En otros idiomas

Esta página está en otros idiomas

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