2

I want to save a page content to an image when it is fully loaded but sometimes I am getting output raster not rendered completely.

import sys
import signal
import os
from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import QWebPage

app = QApplication(sys.argv)
signal.signal(signal.SIGINT, signal.SIG_DFL)
webpage = QWebPage()
def onLoadFinished(result):
    if not result:
        print "Request failed"
        sys.exit(1)
    webpage.setViewportSize(webpage.mainFrame().contentsSize())
    image = QImage(webpage.viewportSize(), QImage.Format_ARGB32)
    painter = QPainter(image)
    webpage.mainFrame().render(painter)
    painter.end()
    if os.path.exists("output.png"):
        os.remove("output.png")
    image.save("output.png")
    sys.exit(0) # quit this application

webpage.mainFrame().load(QUrl("file:///page.html"))
webpage.connect(webpage, SIGNAL("loadFinished(bool)"), onLoadFinished)
sys.exit(app.exec_())

The page is using JavaScript (onload function) to acquire a google map (640x640px) .

Image

Community
  • 1
  • 1
czaaja
  • 119
  • 2
  • 7
  • 1
    Maybe this is because the images are loaded using Ajax after the page has finished loading. Is this useful http://stackoverflow.com/questions/1302874/how-to-know-when-a-web-page-is-loaded-when-using-qtwebkit ? – xioxox Mar 04 '11 at 15:17

1 Answers1

0

Checkout this question. Use the QWebPage.mainFrame().evaluateJavaScript() method to add this javascript code:

google.maps.event.addListenerOnce(map, 'idle', function(){
    // do something only the first time the map is loaded
});

You might also find useful the QWebFrame::addToJavaScriptWindowObject ( const QString & name, QObject * object ) method.

Community
  • 1
  • 1