Reputation: 120
I am stuck with one problem in generating PDF from a link. I have a https://example.com/export_html?parameter1=abc¶meter2=def, this link will generate a HTML file with all the contents includes charts, tables & etc.
I am trying extract these contents from the above URL and save it to a PDF file. But the problem is above link takes atleast 5-7 seconds to load all its content, after loading all its content only i have to save it as PDF.
I tried time.sleep() function to load all the contents of PDF, but that did not work with PyQt4 tried few stuffs with PyQt4 but didn't work for me.
Even tried with Ghost.py, below is the code i tried:
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()
But the above code is not working because Attribute error. Can someone suggest me with the better solution / approach ?
I am completely stuck with this generating PDF thing, i have to wait for that link to load for 5-7 seconds and then save it as a PDF file. Any help is much appreciated.
Thanks in advance.
Upvotes: 1
Views: 370
Reputation: 6748
You can use pdfkit. It is much simpler than using ghost. Install it from pypi with pip install pdfkit
. The usage is below:
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')
For more information, check out this. You will need to download the wkhtmltopdf executable too.
Upvotes: 2