23#include <QWebEnginePage>
25#include "moc_qgswebenginepage.cpp"
29#include <QTemporaryFile>
36 , mPage { std::make_unique< QWebEnginePage >() }
53 mCachedSize = QSize();
57 bool finished =
false;
59 connect( mPage.get(), &QWebEnginePage::loadFinished, &loop, [&loop, &finished, &result](
bool ok ) {
64 mPage->setContent( data, mimeType, baseUrl );
67 loop.exec( QEventLoop::ExcludeUserInputEvents );
70 handlePostBlockingLoadOperations();
75 mPage->setContent( data, mimeType, baseUrl );
82 mCachedSize = QSize();
86 bool finished =
false;
88 connect( mPage.get(), &QWebEnginePage::loadFinished, &loop, [&loop, &finished, &result](
bool ok ) {
93 mPage->setHtml( html, baseUrl );
96 loop.exec( QEventLoop::ExcludeUserInputEvents );
99 handlePostBlockingLoadOperations();
105 mPage->setHtml( html, baseUrl );
112 mCachedSize = QSize();
116 bool finished =
false;
118 connect( mPage.get(), &QWebEnginePage::loadFinished, &loop, [&loop, &finished, &result](
bool ok ) {
123 mPage->setUrl( url );
126 loop.exec( QEventLoop::ExcludeUserInputEvents );
129 handlePostBlockingLoadOperations();
135 mPage->setUrl( url );
142 if ( mCachedSize.isValid() )
148 bool finished =
false;
149 mPage->runJavaScript(
"[document.documentElement.scrollWidth, document.documentElement.scrollHeight];", [&width, &height, &loop, &finished]( QVariant result ) {
150 width = result.toList().value( 0 ).toInt();
151 height = result.toList().value( 1 ).toInt();
157 loop.exec( QEventLoop::ExcludeUserInputEvents );
161 mCachedSize = QSize( width, height );
165void QgsWebEnginePage::handlePostBlockingLoadOperations()
172 bool finished =
false;
173 mPage->runJavaScript(
"[document.documentElement.scrollWidth, document.documentElement.scrollHeight];", [&width, &height, &loop, &finished]( QVariant result ) {
174 width = result.toList().value( 0 ).toInt();
175 height = result.toList().value( 1 ).toInt();
181 loop.exec( QEventLoop::ExcludeUserInputEvents );
184 mCachedSize = QSize( width, height );
193 static constexpr double dpi = 96.0;
194 const QSizeF pageSize = QSizeF( actualSize.width() / dpi, actualSize.height() / dpi );
197 bool finished =
false;
198 bool printOk =
false;
199 QString renderedPdfPath;
200 connect( mPage.get(), &QWebEnginePage::pdfPrintingFinished, &loop, [&loop, &finished, &printOk, &renderedPdfPath](
const QString &pdfPath,
bool success ) {
202 renderedPdfPath = pdfPath;
214 const QPageLayout layout = QPageLayout( QPageSize( pageSize, QPageSize::Inch ), QPageLayout::Portrait, QMarginsF( 0, 0, 0, 0 ), QPageLayout::Inch, QMarginsF( 0, 0, 0, 0 ) );
215 mPage->printToPdf( f.fileName(), layout );
219 loop.exec( QEventLoop::ExcludeUserInputEvents );
224 QgsPdfRenderer renderer( renderedPdfPath );
225 renderer.render( painter, painterRect, 0 );
232 throw QgsNotSupportedException( QObject::tr(
"Rendering web pages requires a QGIS build with PDF4Qt library support" ) );
Custom exception class which is raised when an operation is not supported.
bool setContent(const QByteArray &data, const QString &mimeType=QString(), const QUrl &baseUrl=QUrl(), bool blocking=false)
Sets the content of the web page to data.
void loadStarted()
This signal is emitted when the page starts loading content.
bool setHtml(const QString &html, const QUrl &baseUrl=QUrl(), bool blocking=false)
Sets the content of this page to html.
bool setUrl(const QUrl &url, bool blocking=false)
Sets the url of the web page to be displayed.
void loadFinished(bool ok)
This signal is emitted when the page finishes loading content.
QgsWebEnginePage(QObject *parent=nullptr)
Constructor for QgsWebEnginePage, with the specified parent widget.
bool render(QPainter *painter, const QRectF &painterRect)
Renders the web page contents to a painter.
void loadProgress(int progress)
This signal is emitted when the global progress status changes.
~QgsWebEnginePage() override
QWebEnginePage * page()
Returns a reference to the QWebEnginePage.
QSize documentSize() const
Returns the size of the page document, in pixels.