Create whole page screen shot using QWebPage

2.9k views Asked by At

I am using Qt (the fancy browser example with the Qt creator) to create a screen shot of web pages using this code (taken and converted from here):

QImage *image = new QImage(view->page()->mainFrame()->contentsSize(), QImage::Format_ARGB32);
QPainter *painter = new QPainter(image);

view->page()->mainFrame()->render(painter);

painter->end();
image->save(view->title() + "png");

But it only creates a screen shot of the visible portion of the page (view port).

If I add this line at the beginning, the screen shot is created from the whole page, but the problem is, it adds a new scroll bar each time a page loads.

view->page()->setViewportSize(view->page()->mainFrame()->contentsSize());

Any idea how to fix this?

2

There are 2 answers

1
Ariya Hidayat On BEST ANSWER

You just need to disable the scrollbars:

page()->mainFrame()->setScrollBarPolicy(Qt::Vertical, Qt::ScrollBarAlwaysOff);
page()->mainFrame()->setScrollBarPolicy(Qt::Horizontal, Qt::ScrollBarAlwaysOff);

For a complete example, use what I have described 3.5 years ago in Capturing web pages blog post. The code repository for that has been since moved to http://qt.gitorious.org/qt-labs/graphics-dojo.

If you pay attention to the above example, there is no need to create QWebView. You can totally work only from QWebPage instance.

0
hmuelner On

It looks like "wkhtmltopdf" ("wkhtmltopdf") has the correct implementation for this - search for "painter" in src/lib/imageconverter.cc.