Html文件的PDF格式在蟒蛇不wkhtmltopdf

0

的问题

我有一个Plotly多页(签)划应用程序。 我想转换这个 PDF 文件。 我知道有的 dash_snapshot_engine 模块,该模块是不是免费的。 因此,我在寻找一个免费的替代方法。 为我冲的应用程序将是可执行,我不能使用外部软件等 wkhtmltopdf我只能使用蟒蛇只有图书馆。

有人提出任何建议如何转换一个 html 文件 pdf 与Python库?

在此先感谢!

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

最好的答案

0

你能加入 wkhtmltopdf 你的 exe 使用 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)

建设 exe (wkhtmltopdf和你的脚本中的同一目录):

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

出:

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

谢谢你为这一想法。 它还与cx_freeze? 我可以添加的。exe文件同样的方式?
abc

@abc:我从来没有使用 cx_freeze,只是 PyInstaller, nuitkapy2exe (Python2.x)。 可能会有所帮助: stackoverflow.com/questions/2553886/...
Maurice Meyer

它是可能的, wkhtmltopdf 使用网络的服务转换html pdf? -在这种情况下,它不是一个选项,作为转换应使用"离线"的方法...
abc

没有 wkhtmltpdf 没有处理当地自身。
Ryan

其他语言

此页面有其他语言版本

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