Not really. I have no use for QPainter. But nevertheless, I have fixed the problem. For anyone having the same problem, here is a solution. Define a script in your HEAD tags in the HTML content with getters for page height and width:
<script LANGUAGE="JavaScript">
function getHeight ()
{
return window.document.body.scrollHeight;
}
function getWidth ()
{
return window.document.body.scrollWidth;
}
</script>
<script LANGUAGE="JavaScript">
function getHeight ()
{
return window.document.body.scrollHeight;
}
function getWidth ()
{
return window.document.body.scrollWidth;
}
</script>
To copy to clipboard, switch view to plain text mode
Now in the code where you get the page content from the webview after loadFinished signal has been emitted, try this:
QWebView* view = dynamic_cast<QWebView *>(sender());
QVariant heightVariant
= view
->page
()->mainFrame
()->evaluateJavaScript
("getHeight()");
QVariant widthVariant
= view
->page
()->mainFrame
()->evaluateJavaScript
("getWidth()");
printer->setOutputFileName("<path to pdf>");
printer
->setOutputFormat
(QPrinter::PdfFormat);
printer
->setOrientation
(QPrinter::Portrait);
printer
->setPaperSize
(QSize(widthVariant.
toInt(),heightVariant.
toInt()),
QPrinter::Point);
view->page()->mainFrame()->print(printer);
QWebView* view = dynamic_cast<QWebView *>(sender());
QVariant heightVariant = view->page()->mainFrame()->evaluateJavaScript("getHeight()");
QVariant widthVariant = view->page()->mainFrame()->evaluateJavaScript("getWidth()");
QPrinter *printer = new QPrinter();
printer->setOutputFileName("<path to pdf>");
printer->setOutputFormat(QPrinter::PdfFormat);
printer->setOrientation(QPrinter::Portrait);
printer->setPaperSize(QSize(widthVariant.toInt(),heightVariant.toInt()),QPrinter::Point);
view->page()->mainFrame()->print(printer);
To copy to clipboard, switch view to plain text mode
That solves the problem.
Bookmarks