gpt4 book ai didi

python - 使用 Python、Phantomjs/PyQt/Ghost 将 URL 转换为 PDF

转载 作者:太空宇宙 更新时间:2023-11-03 14:56:16 25 4
gpt4 key购买 nike

我在从链接生成 PDF 时遇到了一个问题。我有一个https://example.com/export_html?parameter1=abc&parameter2=def ,此链接将生成一个 HTML 文件,其中包含所有内容,包括图表、表格等。

我正在尝试从上述 URL 中提取这些内容并将其保存到 PDF 文件中。但问题是上面的链接至少需要 5-7 秒才能加载其所有内容,加载所有内容后我只需将其另存为 PDF。

我尝试了time.sleep()函数来加载PDF的所有内容,但这不适用于PyQt4尝试了一些PyQt4的东西,但没有用对我来说。

即使尝试使用 Ghost.py,下面是我尝试过的代码:

from ghost import Ghost
from PySide.QtGui import QApplication, QImage, QPainter, QPrinter
#from Pyside import *
#import PySide
#from PyQt4.QtWebKitWidgets import *

class MyGhost(Ghost):

def capture_pdf(self):
printer = QPrinter(QPrinter.HighResolution)
printer.setResolution(300)
printer.setOutputFileName("QtPrinter.pdf")
printer.setPaperSize(QPrinter.A4)
printer.setOrientation(QPrinter.Landscape)
printer.setOutputFormat(QPrinter.PdfFormat)

painter = QPainter(printer)
self.main_frame.render(painter)
painter.end()

ghost = Ghost(viewport_size=(1280,960))

page, resources = ghost.open('https://www.google.co.in/search?q=ghost+py+save+as+pdf&oq=ghost&aqs=chrome.1.69i57j69i59j69i60l4.5364j0j1&sourceid=chrome&ie=UTF-8')
ghost.capture_pdf()

但是上面的代码由于属性错误而无法工作。有人可以建议我更好的解决方案/方法吗?

我完全被这个生成 PDF 的事情困扰,我必须等待该链接加载 5-7 秒,然后将其另存为 PDF 文件。非常感谢任何帮助。

提前致谢。

最佳答案

您可以使用 pdfkit。它比使用 Ghost 简单得多。使用 pip install pdfkit 从 pypi 安装它。用法如下:

import pdfkit
pdfkit.from_url('https://www.google.co.in/search?q=ghost+py+save+as+pdf&oq=ghost&aqs=chrome.1.69i57j69i59j69i60l4.5364j0j1&sourceid=chrome&ie=UTF-8', 'out.pdf')

有关更多信息,请查看this 。您还需要下载 wkhtmltopdf 可执行文件。

关于python - 使用 Python、Phantomjs/PyQt/Ghost 将 URL 转换为 PDF,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45549571/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com