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 )
65 mPage->setContent( data, mimeType, baseUrl );
68 loop.exec( QEventLoop::ExcludeUserInputEvents );
71 handlePostBlockingLoadOperations();
76 mPage->setContent( data, mimeType, baseUrl );
83 mCachedSize = QSize();
87 bool finished =
false;
89 connect( mPage.get(), &QWebEnginePage::loadFinished, &loop, [&loop, &finished, &result](
bool ok )
95 mPage->setHtml( html, baseUrl );
98 loop.exec( QEventLoop::ExcludeUserInputEvents );
101 handlePostBlockingLoadOperations();
107 mPage->setHtml( html, baseUrl );
114 mCachedSize = QSize();
118 bool finished =
false;
120 connect( mPage.get(), &QWebEnginePage::loadFinished, &loop, [&loop, &finished, &result](
bool ok )
126 mPage->setUrl( url );
129 loop.exec( QEventLoop::ExcludeUserInputEvents );
132 handlePostBlockingLoadOperations();
138 mPage->setUrl( url );
145 if ( mCachedSize.isValid() )
151 bool finished =
false;
152 mPage->runJavaScript(
"[document.documentElement.scrollWidth, document.documentElement.scrollHeight];", [&width, &height, &loop, &finished]( QVariant result )
154 width = result.toList().value( 0 ).toInt();
155 height = result.toList().value( 1 ).toInt();
161 loop.exec( QEventLoop::ExcludeUserInputEvents );
165 mCachedSize = QSize( width, height );
169void QgsWebEnginePage::handlePostBlockingLoadOperations()
176 bool finished =
false;
177 mPage->runJavaScript(
"[document.documentElement.scrollWidth, document.documentElement.scrollHeight];", [&width, &height, &loop, &finished]( QVariant result )
179 width = result.toList().value( 0 ).toInt();
180 height = result.toList().value( 1 ).toInt();
186 loop.exec( QEventLoop::ExcludeUserInputEvents );
189 mCachedSize = QSize( width, height );
198 static constexpr double dpi = 96.0;
199 const QSizeF pageSize = QSizeF( actualSize.width() / dpi, actualSize.height() / dpi );
202 bool finished =
false;
203 bool printOk =
false;
204 QString renderedPdfPath;
205 connect( mPage.get(), &QWebEnginePage::pdfPrintingFinished, &loop, [&loop, &finished, &printOk, &renderedPdfPath](
const QString & pdfPath,
bool success )
208 renderedPdfPath = pdfPath;
218 const QPageLayout layout = QPageLayout( QPageSize( pageSize, QPageSize::Inch ),
219 QPageLayout::Portrait, QMarginsF( 0, 0, 0, 0 ),
220 QPageLayout::Inch, QMarginsF( 0, 0, 0, 0 ) );
221 mPage->printToPdf( f.fileName(), layout );
225 loop.exec( QEventLoop::ExcludeUserInputEvents );
230 QgsPdfRenderer renderer( renderedPdfPath );
231 renderer.render( painter, painterRect, 0 );
238 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.