19#include "moc_qgswebenginepage.cpp"
21#include <QWebEnginePage>
27#include <QTemporaryFile>
34 , mPage{ std::make_unique< QWebEnginePage >() }
51 mCachedSize = QSize();
55 bool finished =
false;
57 connect( mPage.get(), &QWebEnginePage::loadFinished, &loop, [&loop, &finished, &result](
bool ok )
63 mPage->setContent( data, mimeType, baseUrl );
66 loop.exec( QEventLoop::ExcludeUserInputEvents );
69 handlePostBlockingLoadOperations();
74 mPage->setContent( data, mimeType, baseUrl );
81 mCachedSize = QSize();
85 bool finished =
false;
87 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 )
124 mPage->setUrl( url );
127 loop.exec( QEventLoop::ExcludeUserInputEvents );
130 handlePostBlockingLoadOperations();
136 mPage->setUrl( url );
143 if ( mCachedSize.isValid() )
149 bool finished =
false;
150 mPage->runJavaScript(
"[document.documentElement.scrollWidth, document.documentElement.scrollHeight];", [&width, &height, &loop, &finished]( QVariant result )
152 width = result.toList().value( 0 ).toInt();
153 height = result.toList().value( 1 ).toInt();
159 loop.exec( QEventLoop::ExcludeUserInputEvents );
163 mCachedSize = QSize( width, height );
167void QgsWebEnginePage::handlePostBlockingLoadOperations()
174 bool finished =
false;
175 mPage->runJavaScript(
"[document.documentElement.scrollWidth, document.documentElement.scrollHeight];", [&width, &height, &loop, &finished]( QVariant result )
177 width = result.toList().value( 0 ).toInt();
178 height = result.toList().value( 1 ).toInt();
184 loop.exec( QEventLoop::ExcludeUserInputEvents );
187 mCachedSize = QSize( width, height );
196 static constexpr double dpi = 96.0;
197 const QSizeF pageSize = QSizeF( actualSize.width() / dpi, actualSize.height() / dpi );
200 bool finished =
false;
201 bool printOk =
false;
202 QString renderedPdfPath;
203 connect( mPage.get(), &QWebEnginePage::pdfPrintingFinished, &loop, [&loop, &finished, &printOk, &renderedPdfPath](
const QString & pdfPath,
bool success )
206 renderedPdfPath = pdfPath;
216 const QPageLayout layout = QPageLayout( QPageSize( pageSize, QPageSize::Inch ),
217 QPageLayout::Portrait, QMarginsF( 0, 0, 0, 0 ),
218 QPageLayout::Inch, QMarginsF( 0, 0, 0, 0 ) );
219 mPage->printToPdf( f.fileName(), layout );
223 loop.exec( QEventLoop::ExcludeUserInputEvents );
229 renderer.render( painter, painterRect, 0 );
236 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.
Utility class for rendering PDF documents.
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.