Important: Please read the Qt Code of Conduct -

[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 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:

    1. qmake ---> which generates the Makefile for the project
    2. 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 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????



    Project created by QtCreator 2013-09-10T17:28:35


    QT += core webkit widgets webkitwidgets

    QT -= gui

    TARGET = h2p
    #CONFIG += console
    CONFIG -= app_bundle

    TEMPLATE = app

    SOURCES += main.cpp

    HEADERS +=


    #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&#40;htmlFileName&#41;;
      if (! | QIODevice::Text&#41;&#41;
      return 1;
      QString text;
      while (!file.atEnd()) {
      text += file.readLine();
      QWebView *qWebView = new QWebView();
      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&#40;&#41;;



    #ifndef MYPDFPRINTER_H
    #define MYPDFPRINTER_H
    #include <QApplication>
    #include <QObject>
    #include <QWebView>

    class MyPdfPrinter: public QObject {

    QWebView *qWebView;
    char *pdfFileName;
    MyPdfPrinter(QWebView *webView , char *pdfFileName&#41;;
    public slots:
    void receiveLoad(bool&#41;;


    #endif // MYPDFPRINTER_H


    #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);
    printf("\n Exiting After printing pdf\n");


Log in to reply