Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create whole page screen shot using QWebPage

Tags:

qt

qwebpage

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?

like image 660
Omid Kamangar Avatar asked Jan 31 '12 19:01

Omid Kamangar


2 Answers

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.

like image 75
Ariya Hidayat Avatar answered Nov 14 '22 09:11

Ariya Hidayat


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

like image 4
hmuelner Avatar answered Nov 14 '22 09:11

hmuelner