PDA

View Full Version : QWebView problem with pdf



oscar
21st August 2008, 18:08
Hi,

The following code results in an empty pdf file whereas when I show() the component, the html code displays correctly.
Any idea?

Here is the source:


#include <QtCore>
#include <QtGui>
#include <QtWebKit>


int main(int argc, char *argv[]) {
QApplication app(argc, argv, 1);

QPrinter *qPrinter = new QPrinter(QPrinter::HighResolution);
qPrinter->setOutputFormat(QPrinter::PdfFormat);
qPrinter->setOutputFileName("out.pdf");

QFile file("in.txt");
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
return 1;

QString text;
while (!file.atEnd()) {
text += file.readLine();
}

QWebView *qWebView = new QWebView();
qWebView->setMinimumSize(1024,768);
qWebView->show();
qWebView->setHtml(text);
qWebView->show();
qWebView->print(qPrinter);
return app.exec();
}


The file in.txt contains only:


<html>
<body
helloWorld
</body>
</html>


Thanks for your help.
Best regards,
Oscar

wysota
21st August 2008, 19:52
Try setting the viewport size of the associated web page.

oscar
21st August 2008, 21:19
Thank you wysota for your reply.

I tried:
qWebView->page()->setViewportSize(QSize(2000,2000));
before
qWebView->print(qPrinter);

But the result is the same...Grrrr

Maybe I've got something wrong in my config.
Do you get a valid pdf with this example? (I'm testing it under Vista)

Best regards,
Oscar

wysota
21st August 2008, 22:31
I didn't try. But you don't need a web view widget. All you need is QWebPage with its mainFrame where you can set the content using setHtml().

oscar
23rd August 2008, 13:54
The problem is the same with the use of a QWebPage class...

Here is the code:


#include <QtCore>
#include <QtGui>
#include <QtWebKit>


int main(int argc, char *argv[]) {
QApplication app(argc, argv, 1);

QPrinter *qPrinter = new QPrinter(QPrinter::HighResolution);
qPrinter->setOutputFormat(QPrinter::PdfFormat);
qPrinter->setOutputFileName("out.pdf");

QString text = "<html> <body> coucou <br>coucou br>coucou <br>coucou <br>coucou <br>coucou <br>coucou </body> </html>";

QWebPage *qWebPage = new QWebPage();
qWebPage->mainFrame()->setHtml(text);
qWebPage->setViewportSize(QSize(2000,2000));
qWebPage->mainFrame()->print(qPrinter);
return app.exec();
}



Any idea of what I'm missing?

Best regards,
Oscar

oscar
23rd August 2008, 15:37
Well, I've found a html->pdf converter that's working.

I was missing the following line:


connect(qWebView, SIGNAL(loadFinished(bool)), this, SLOT(loadFinished(bool)));

Now, everything is ok.

Best regards,
Oscar