=== added file 'examples/websitescreenshot.py' --- examples/websitescreenshot.py 1970-01-01 00:00:00 +0000 +++ examples/websitescreenshot.py 2007-05-06 15:05:28 +0000 @@ -0,0 +1,32 @@ + +import sys +sys.path.append("..") + +import pykhtml +pykhtml.debugWithGUI = True + +def takeScreenshot(browser): + # Take the screenshot. Simple. File type can be specified + # manually or determined from extension + browser.screenshot("screenshot.png", screenshotFinished) + +def screenshotFinished(): + print "screenshot saved to `screenshot.png`" + # Stop here, we're done + pykhtml.stopEventLoop() + +def main(): + browser = pykhtml.Browser() + # if a URL has been specified, use that instead + if len(sys.argv) > 1: + url = sys.argv[1] + else: + url = "http://paul.giannaros.org/pykhtml" + # load the page. takeScreenshot will be called when it has + # been loaded + browser.load(url, takeScreenshot) + pykhtml.startEventLoop() + + +if __name__ == "__main__": + main() === modified file 'doc/pykhtml.htm' --- doc/pykhtml.htm 2007-04-27 17:10:41 +0000 +++ doc/pykhtml.htm 2007-05-06 15:05:28 +0000 @@ -22,6 +22,7 @@

location (property)

Browse to a new location. You probably don't want to set this directly as you'll receive no notification when the page has loaded. Have a look at Browser.load instead.

onNextLoad (property)

If you're going to do something that will inadvertently cause PyKHTML to browse to a new page and you want a function to be called when the page is loaded, set onNextLoad to the function.

passReferenceToCallbacks (property)

Set whether callbacks passed to functions such as Browser.load or dom.Document.visit will have a reference to this browser object passed as a parameter. Default is True.
+

screenshot(self, fileName, callback, width=800, format=None, quality=None)

Take a screenshot of the current webpage and save it to the given file name. Once the screenshot has been taken and saved, the given callback parameter will be called. You can specify the width (the default is 800) to resize the page to. File type will be determined by extension or by the optional format parameter (one of "PNG", "BMP", "XBM", "XPM", or "JPG"). You can also specify the optional quality parameter, a value from 1-100 (leave as None for default values).

setHtml(self, source, url=None)

Set the HTML of the browser. Parses the HTML and generates the DOM tree so you can navigate it as usual. As well as the `source` parameter, a `url` parameter allows you to specify a URL with which this source code is linked so that e.g any scripts/images referenced in the HTML will be found.

source (read-only property)

class partial

=== modified file 'pykhtml/__init__.py' --- pykhtml/__init__.py 2007-04-27 17:10:41 +0000 +++ pykhtml/__init__.py 2007-05-06 15:05:28 +0000 @@ -302,6 +302,35 @@ return str(self.part.url().url()) location = property(_getLocation, _setLocation, None, "Browse to a new location. You probably don't want to set this directly as you'll receive no notification when the page has loaded. Have a look at [[Browser.load]] instead") + def screenshot(self, fileName, callback, width=800, format=None, quality=None): + """ Take a screenshot of the current webpage and save it to the given file name. Once the screenshot has been taken and saved, the given callback parameter will be called. You can specify the width (the default is 800) to resize the page to. File type will be determined by extension or by the optional format parameter (one of "PNG", "BMP", "XBM", "XPM", or "JPG"). You can also specify the optional quality parameter, a value from 1-100 (leave as None for default values) """ + if format is None: + format = fileName.split(".")[-1].upper() + if format not in ("PNG", "BMP", "XBM", "XPM", "JPG"): + raise TypeError("format must be one of 'PNG', 'BMP', 'XBM', 'XPM', or 'JPG'") + if format == "JPG": + format = "JPEG" + if quality is None: + quality = -1 + dialog.resize(width, 10) + contentsHeight = self.part.view().contentsHeight() + 10 + dialog.resize(width, contentsHeight) + #print view.contentsX(), view.contentsY() + #qt.qApp.processEvents() + timer(0.1, partial(self._screenshotPart2, callback, fileName, width, contentsHeight, format, quality)) + + def _screenshotPart2(self, callback, fileName, width, contentsHeight, format, quality): + # now wait for a re-paint and onto phase two + #pixmap = qt.QPixmap.grabWidget(self.part.widget()) + pixmap = qt.QPixmap(width, contentsHeight) + pixmap.fill(qt.Qt.white) + painter = qt.QPainter() + painter.begin(pixmap) + self.part.paint(painter, qt.QRect(0, 0, width, contentsHeight)) + painter.end() + pixmap.save(fileName, format, quality) + callback() + def load(self, uri, callback): """ Load a webpage in the browser. It takes as parameters the URI of the page to load, and a callable object to call when the page has loaded. This callback will be given the browser object as a reference unless you set [[Browser.referencelessCallbacks]] to True """ self.onNextLoad = callback