20#include <QWebEnginePage>
26#include <QTemporaryFile>
33 , mPage{ std::make_unique< QWebEnginePage >() }
50 mCachedSize = QSize();
54 bool finished =
false;
56 connect( mPage.get(), &QWebEnginePage::loadFinished, &loop, [&loop, &finished, &result](
bool ok )
62 mPage->setContent( data, mimeType, baseUrl );
65 loop.exec( QEventLoop::ExcludeUserInputEvents );
68 handlePostBlockingLoadOperations();
73 mPage->setContent( data, mimeType, baseUrl );
80 mCachedSize = QSize();
84 bool finished =
false;
86 connect( mPage.get(), &QWebEnginePage::loadFinished, &loop, [&loop, &finished, &result](
bool ok )
92 mPage->setHtml( html, baseUrl );
95 loop.exec( QEventLoop::ExcludeUserInputEvents );
98 handlePostBlockingLoadOperations();
104 mPage->setHtml( html, baseUrl );
111 mCachedSize = QSize();
115 bool finished =
false;
117 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 )
151 width = result.toList().value( 0 ).toInt();
152 height = result.toList().value( 1 ).toInt();
158 loop.exec( QEventLoop::ExcludeUserInputEvents );
162 mCachedSize = QSize( width, height );
166void QgsWebEnginePage::handlePostBlockingLoadOperations()
173 bool finished =
false;
174 mPage->runJavaScript(
"[document.documentElement.scrollWidth, document.documentElement.scrollHeight];", [&width, &height, &loop, &finished]( QVariant result )
176 width = result.toList().value( 0 ).toInt();
177 height = result.toList().value( 1 ).toInt();
183 loop.exec( QEventLoop::ExcludeUserInputEvents );
186 mCachedSize = QSize( width, height );
195 static constexpr double dpi = 96.0;
196 const QSizeF pageSize = QSizeF( actualSize.width() / dpi, actualSize.height() / dpi );
199 bool finished =
false;
200 bool printOk =
false;
201 QString renderedPdfPath;
202 connect( mPage.get(), &QWebEnginePage::pdfPrintingFinished, &loop, [&loop, &finished, &printOk, &renderedPdfPath](
const QString & pdfPath,
bool success )
205 renderedPdfPath = pdfPath;
215 const QPageLayout layout = QPageLayout( QPageSize( pageSize, QPageSize::Inch ),
216 QPageLayout::Portrait, QMarginsF( 0, 0, 0, 0 ),
217 QPageLayout::Inch, QMarginsF( 0, 0, 0, 0 ) );
218 mPage->printToPdf( f.fileName(), layout );
222 loop.exec( QEventLoop::ExcludeUserInputEvents );
228 renderer.render( painter, painterRect, 0 );
235 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.