def render_html(path_to_html, width=590, height=750, as_xhtml=True): from PyQt4.QtWebKit import QWebPage from PyQt4.Qt import QEventLoop, QPalette, Qt, QUrl, QSize from calibre.gui2 import is_ok_to_use_qt if not is_ok_to_use_qt(): return None path_to_html = os.path.abspath(path_to_html) with CurrentDir(os.path.dirname(path_to_html)): page = QWebPage() pal = page.palette() pal.setBrush(QPalette.Background, Qt.white) page.setPalette(pal) page.setViewportSize(QSize(width, height)) page.mainFrame().setScrollBarPolicy(Qt.Vertical, Qt.ScrollBarAlwaysOff) page.mainFrame().setScrollBarPolicy(Qt.Horizontal, Qt.ScrollBarAlwaysOff) loop = QEventLoop() renderer = HTMLRenderer(page, loop) page.loadFinished.connect(renderer, type=Qt.QueuedConnection) if as_xhtml: page.mainFrame().setContent(open(path_to_html, 'rb').read(), 'application/xhtml+xml', QUrl.fromLocalFile(path_to_html)) else: page.mainFrame().load(QUrl.fromLocalFile(path_to_html)) loop.exec_() renderer.loop = renderer.page = None page.loadFinished.disconnect() del page del loop if isinstance(renderer.exception, ParserError) and as_xhtml: return render_html(path_to_html, width=width, height=height, as_xhtml=False) return renderer
class _WebkitRendererHelper(QObject): """This helper class is doing the real work. It is required to allow WebkitRenderer.render() to be called "asynchronously" (but always from Qt's GUI thread). """ def __init__(self, parent): """Copies the properties from the parent (WebkitRenderer) object, creates the required instances of QWebPage, QWebView and QMainWindow and registers some Slots. """ QObject.__init__(self) # Copy properties from parent for key, value in parent.__dict__.items(): setattr(self, key, value) # Create and connect required PyQt4 objects self._page = QWebPage() self._view = QWebView() self._view.setPage(self._page) self._window = QMainWindow() self._window.setCentralWidget(self._view) # Import QWebSettings for key, value in self.qWebSettings.iteritems(): self._page.settings().setAttribute(key, value) # Connect required event listeners self.connect( self._page, SIGNAL("loadFinished(bool)"), self._on_load_finished ) self.connect( self._page, SIGNAL("loadStarted()"), self._on_load_started ) self.connect( self._page.networkAccessManager(), SIGNAL("sslErrors(QNetworkReply *,const QList<QSslError>&)"), self._on_ssl_errors ) self.connect( self._page.networkAccessManager(), SIGNAL("finished(QNetworkReply *)"), self._on_each_reply ) # The way we will use this, it seems to be unesseccary to have # Scrollbars enabled. self._page.mainFrame().setScrollBarPolicy( Qt.Horizontal, Qt.ScrollBarAlwaysOff ) self._page.mainFrame().setScrollBarPolicy( Qt.Vertical, Qt.ScrollBarAlwaysOff ) self._page.settings().setUserStyleSheetUrl( QUrl("data:text/css,html,body{overflow-y:hidden !important;}") ) # Show this widget self._window.show() def __del__(self): """Clean up Qt4 objects. """ self._window.close() del self._window del self._view del self._page def render(self, url): """The real worker. Loads the page (_load_page) and awaits the end of the given 'delay'. While it is waiting outstanding QApplication events are processed. After the given delay, the Window or Widget (depends on the value of 'grabWholeWindow' is drawn into a QPixmap and postprocessed (_post_process_image). """ self._load_page(url, self.width, self.height, self.timeout) # Wait for end of timer. In this time, process # other outstanding Qt events. if self.wait > 0: if self.logger: self.logger.debug("Waiting %d seconds " % self.wait) waitToTime = time.time() + self.wait while time.time() < waitToTime and QApplication.hasPendingEvents(): QApplication.processEvents() if self.renderTransparentBackground: # Another possible drawing solution image = QImage(self._page.viewportSize(), QImage.Format_ARGB32) image.fill(QColor(255, 0, 0, 0).rgba()) # http://ariya.blogspot.com/2009/04/transparent-qwebview-and-qwebpage.html palette = self._view.palette() palette.setBrush(QPalette.Base, Qt.transparent) self._page.setPalette(palette) self._view.setAttribute(Qt.WA_OpaquePaintEvent, False) painter = QPainter(image) painter.setBackgroundMode(Qt.TransparentMode) self._page.mainFrame().render(painter) painter.end() else: if self.grabWholeWindow: # Note that this does not fully ensure that the # window still has the focus when the screen is # grabbed. This might result in a race condition. self._view.activateWindow() image = QPixmap.grabWindow(self._window.winId()) else: image = QPixmap.grabWidget(self._window) return self._post_process_image(image) def _load_page(self, url, width, height, timeout): """ This method implements the logic for retrieving and displaying the requested page. """ # This is an event-based application. So we have to wait until # "loadFinished(bool)" raised. cancelAt = time.time() + timeout self.__loading = True self.__loadingResult = False # Default # TODO: fromEncoded() needs to be used in some situations. Some # sort of flag should be passed in to WebkitRenderer maybe? #self._page.mainFrame().load(QUrl.fromEncoded(url)) self._page.mainFrame().load(QUrl(url)) while self.__loading: if timeout > 0 and time.time() >= cancelAt: raise RuntimeError("Request timed out on %s" % url) while QApplication.hasPendingEvents() and self.__loading: QCoreApplication.processEvents() if self.logger: self.logger.debug("Processing result") if not self.__loading_result: if self.logger: self.logger.warning("Failed to load %s" % url) raise BadURLException("Failed to load %s" % url) # Set initial viewport (the size of the "window") size = self._page.mainFrame().contentsSize() if self.logger: self.logger.debug("contentsSize: %s", size) if width > 0: size.setWidth(width) if height > 0: size.setHeight(height) self._window.resize(size) def _post_process_image(self, qImage): """If 'scaleToWidth' or 'scaleToHeight' are set to a value greater than zero this method will scale the image using the method defined in 'scaleRatio'. """ if self.scaleToWidth > 0 or self.scaleToHeight > 0: # Scale this image if self.scaleRatio == 'keep': ratio = Qt.KeepAspectRatio elif self.scaleRatio in ['expand', 'crop']: ratio = Qt.KeepAspectRatioByExpanding else: # 'ignore' ratio = Qt.IgnoreAspectRatio qImage = qImage.scaled( self.scaleToWidth, self.scaleToHeight, ratio ) if self.scaleRatio == 'crop': qImage = qImage.copy( 0, 0, self.scaleToWidth, self.scaleToHeight ) return qImage def _on_each_reply(self, reply): """Logs each requested uri""" self.logger.debug("Received %s" % (reply.url().toString())) # Eventhandler for "loadStarted()" signal def _on_load_started(self): """Slot that sets the '__loading' property to true.""" if self.logger: self.logger.debug("loading started") self.__loading = True # Eventhandler for "loadFinished(bool)" signal def _on_load_finished(self, result): """Slot that sets the '__loading' property to false and stores the result code in '__loading_result'. """ if self.logger: self.logger.debug("loading finished with result %s", result) self.__loading = False self.__loading_result = result # Eventhandler for "sslErrors(QNetworkReply *,const QList<QSslError>&)" # signal. def _on_ssl_errors(self, reply, errors): """Slot that writes SSL warnings into the log but ignores them.""" for e in errors: if self.logger: self.logger.warn("SSL: " + e.errorString()) reply.ignoreSslErrors()