在从链接中生成PDF时,我遇到了一个问题。我有一个https://example.com/export,这个链接将生成一个包含图表、表格等所有内容的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文件。任何帮助都是非常感谢的。
提前谢谢。
发布于 2017-08-07 14:46:12
你可以使用pdfkit。这比使用鬼要简单得多。用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')
有关更多信息,请访问这。您还需要下载wkhtmltopdf可执行文件。
https://stackoverflow.com/questions/45549571
复制相似问题