开发者

Loading page with frames for a screenshot

I have an app that renders a web page in a iframe, and then loads X number of images in a div that overlays the frame.

So I have this python script that loads the url, and takes a screenshot. It works on regular web pages, but I think the frame is throwing it off. Below is my code, and a link to the screenshot it's taking.

#!/usr/bin/env python
import sys
import signal

from PyQt4.QtCore import *
from PyQt4.QtGui import *
from PyQt4.QtWebKit import QWebPage

def onLoadFinished(result):
    if not result:
        print "Request failed"
        sys.exit(1)

    #screen = QtGui.QDesktopWidget().screenGeometry()
    size = webpage.mainFrame().contentsSize()
    # Set the size of the (virtual) browser window
    webpage.setViewportSize(webpage.mainFrame().contentsSize())

    # Paint this frame into an image
    image = QImage(webpage.viewportSize(), QImage.Format_ARGB32)
    painter = QPainter(image)
    webpage.mainFrame().render(painter)
    painter.end()
    image.save("/var/www/html/output.png")
    sys.exit(0)


qtargs = [sys.argv[0]]
qtargs.append("-display")
qtargs.append(":0")

app = QApplication(qtargs,True)
#app = QApplication(sys.argv)
signal.signal(signal.SIGINT, signal.SIG_DFL)

webpage = QWebPage()
webpage.mainFrame().setScrollBarPolicy(Qt.Horizontal, Qt.ScrollBarAlwaysOff)
webpage.mainFrame().setScro开发者_Python百科llBarPolicy(Qt.Vertical, Qt.ScrollBarAlwaysOff)
webpage.connect(webpage, SIGNAL("loadFinished(bool)"), onLoadFinished)
webpage.mainFrame().load(QUrl("http://localhost/heatmap"))

sys.exit(app.exec_())

Screenshot taken: http://img28.imageshack.us/img28/7506/outputc.png

As you can see in the above image, the page is shrunk into a small image with scroll bars, not showing the entire page with the iframe and div on top of it. Do I somehow need to load the stylesheets or force it to render like it is in the browser? Qt.ScrollbarAlwaysOff doesn't really seem to be working in my instance. The "placeholder text" is part of the webpage, and the image is what is making it kind of grey, with a blue smug and color key in the top left.

When I print the contents of webpage.mainFrame().contentsSize() for my app, I get PyQt4.QtCore.QSize(400, 158)

But when I render something like www.google.com, I get PyQt4.QtCore.QSize(617, 573)

So something isn't being loaded properly to mess up the contentsSize(). Ideas on where I have gone wrong?


One option is to set a sane preferred size with QWebPage.setPreferredContentsSize() before loading the page. This way your frameset size will be always that preferred size. (This will also make some pages render in a more sensible way, instead of using a very minimal width)

Otherwise you'll need to iterate through all child frames and calculate the main frame size yourself, which can become quite complex, and may or may not work correctly.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜