[Qt 5.1.1] QtWebView and QtWebpage not printing text when Qt built manually, but using QtCreator same code Works fine
-
Hi,
I am newbie to Qt.
Trying to generate basic pdf from html. I created project in QtCreator on ubuntu. When I run the resulting binary created by Qtcreator for my project on ubuntu, it works fine. I get the desired pdf with QWebView and QWebPage both printing text as well as images.
But ultimately we want to put this basic project on the Fedora based linux VM. In fedora, I took the Qt 5.1.1 source from http://download.qt-project.org/official_releases/qt/5.1/5.1.1/single/qt-everywhere-opensource-src-5.1.1.tar.gz and built Qt on that VM. The build runs fine and installs the Qt in usr/local/Qt directory
But taking my same project to that VM. I run:
- qmake project.pro ---> which generates the Makefile for the project
- Running Makefile creates binary for the project on the VM.
But when I run this binary on VM, if the html contains any text, the QPrinter does not print it in the final pdf. *But, images are printed fine. * Tried both the QWebView and the QWebpage class, happening for both. Happens only on VM, not locally.
Does anyone know, what I am missing? Code is below:
I installed QtCreator from here http://download.qt-project.org/official_releases/qt/5.1/5.1.1/qt-linux-opensource-5.1.1-x86_64-offline.run. Maybe if there a way I can install this on Fedora??? Since Fedora VM is all commandline, I dont need QtCreator there, but Qt5.1.1 libraries will do, as long as those libraries allow me to do qmake and build my project's binary, i am good?? Let me know if I can go this way without building Qt on Fedora VM?
1 more thing:
While running the resulting binary of my project on VM, I ran into one more issue . Basically executing it caused- QXcbConnection: Could not connect to display*. And the program will exit But I resolved this by installing xvfb-run
Running the executable in this fasion : xvfb-run --server-args="-screen 0, 1024x768x24" ./h2p resolves the QXcbConnection problem. I wonder if the text not getting printed problem is not because of running the binary like this????
@
h2p.pro#-------------------------------------------------
Project created by QtCreator 2013-09-10T17:28:35
#-------------------------------------------------
QT += core webkit widgets webkitwidgets
QT -= gui
TARGET = h2p
#CONFIG += console
CONFIG -= app_bundleTEMPLATE = app
SOURCES += main.cpp
MyPdfPrinter.cppHEADERS +=
MyPdfPrinter.hmain.cpp
#include <QApplication>
#include <QPrinter>
#include <QStyleOption>
#include <QtPlugin>
#include <QWebView>
#include <QFile>
#include <QPrinter>
#include <cstdlib>
#include <iostream>
#include "MyPdfPrinter.h"int main(int argc, char *argv[])
{
QApplication a(argc, argv);char *pdfFileName = "/home/rohit.chawla/Desktop/testPDF2StaticResourceImages_PAGES.pdf"; char *htmlFileName = "/home/rohit.chawla/Desktop/testPDF2StaticResourceImages_PAGES.html"; printf("Converting %s to %s\n",htmlFileName, pdfFileName); QFile file(htmlFileName); 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, QUrl("file:///home/rohit.chawla/Desktop/resources/")); MyPdfPrinter *myPdfPrinter = new MyPdfPrinter(qWebView , pdfFileName); QObject::connect(qWebView, SIGNAL(loadFinished(bool)), myPdfPrinter, SLOT(receiveLoad(bool))); return a.exec();
}
MyPdfPrinter.h
#ifndef MYPDFPRINTER_H
#define MYPDFPRINTER_H
#include <QApplication>
#include <QObject>
#include <QWebView>class MyPdfPrinter: public QObject {
Q_OBJECT private: QWebView *qWebView; char *pdfFileName; public: MyPdfPrinter(QWebView *webView , char *pdfFileName); public slots: void receiveLoad(bool);
};
#endif // MYPDFPRINTER_H
MyPdfPrinter.cpp
#include "MyPdfPrinter.h"
#include <stdio.h>
#include <QPrinter>MyPdfPrinter::MyPdfPrinter(QWebView *webView , char *pdfFileName){
this->qWebView = webView; this->pdfFileName = pdfFileName; printf("\n Made Pdf Printer object\n");
}
void MyPdfPrinter::receiveLoad(bool isPrint){
printf("\nHere After loading\n");
QPrinter *qPrinter = new QPrinter(QPrinter::HighResolution);
qPrinter->setOutputFormat(QPrinter::PdfFormat);
qPrinter->setOutputFileName(pdfFileName);
this->qWebView->print(qPrinter);
printf("\n Exiting After printing pdf\n");
exit(0);
}@