Here is what I've learned:
- Off screen rendering is supported by the underlying chromium embedded framework but this has not been explicitly ported to Qt
- A number of people have requested this functionality be added to QtWebEngine and coincidentally about two weeks ago it looks like they started work on it for Qt5.9 https://bugreports.qt.io/browse/QTBUG-44986
- Various hacky solutions exist, such as scrolling down the page which forces it to be rendered, and resizing the QWebEngine widget so that it shows the whole page. I had no luck in getting scrolling to trigger the rendering, and wasn't able to test pdf printing because it would require an update of my Qt version. So I've ended up resizing the widget until some future release comes along which solves this officially:
webView
->page
()->runJavaScript
("document.documentElement.scrollWidth + '|' + document.documentElement.scrollHeight;",
[=](QVariant result
){ std::string widthAndHeight = result.toString().toStdString();
std::size_t idx = widthAndHeight.find("|");
int newWidth = std::stoi(widthAndHeight.substr(0,idx));
int newHeight = std::stoi(widthAndHeight.substr(idx+1));
webView->resize(newWidth, newHeight);
});
webView->page()->runJavaScript("document.documentElement.scrollWidth + '|' + document.documentElement.scrollHeight;", [=](QVariant result){
std::string widthAndHeight = result.toString().toStdString();
std::size_t idx = widthAndHeight.find("|");
int newWidth = std::stoi(widthAndHeight.substr(0,idx));
int newHeight = std::stoi(widthAndHeight.substr(idx+1));
webView->resize(newWidth, newHeight);
});
To copy to clipboard, switch view to plain text mode
Bookmarks