Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Qt 5.1.1] QtWebView and QtWebpage not printing text when Qt built manually, but using QtCreator same code Works fine
Qt 6.11 is out! See what's new in the release blog

[Qt 5.1.1] QtWebView and QtWebpage not printing text when Qt built manually, but using QtCreator same code Works fine

Scheduled Pinned Locked Moved General and Desktop
5 Posts 1 Posters 2.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    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:

    1. qmake project.pro ---> 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 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_bundle

    TEMPLATE = app

    SOURCES += main.cpp
    MyPdfPrinter.cpp

    HEADERS +=
    MyPdfPrinter.h

    main.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&#40;htmlFileName&#41;;
      
      if (!file.open(QIODevice::ReadOnly | QIODevice::Text&#41;)
      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&#40;&#41;;
    

    }

    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&#41;;
    
    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);
    }

    @

    1 Reply Last reply
    0
    • ? Offline
      ? Offline
      A Former User
      wrote on last edited by
      #2

      Any guess guys, stuck on this? Thanks.

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        To build QT manually,

        This is what I used:

        1. ./configure -opensource -confirm-license -release -nomake tests -nomake examples

        2. gmake

        3. gmake install

        Following is my configuration summary, when I run 1st command:

        Warning: Disabling Linux Accessibility Bridge: DBus is missing.

        Configure summary

        Build type: linux-g++ (x86_64, CPU features: mmx sse sse2)
        Platform notes:

                - Also available for Linux: linux-kcc linux-icc linux-cxx
        

        Build options:
        @Configuration .......... accessibility audio-backend avx c++11 clock-gettime clock-monotonic compile_examples concurrent debug evdev eventfd fontconfig full-config getaddrinfo getifaddrs iconv icu inotify ipv6ifname large-config largefile linuxfb medium-config minimal-config mremap nis opengl pcre png precompile_header private_tests qpa qpa reduce_exports reduce_relocations rpath shared small-config sse2 sse3 sse4_1 sse4_2 ssse3 system-freetype system-jpeg system-png system-zlib v8 v8snapshot warnings_are_errors xcb xcb-glx xcb-render xcb-xlib xinput2 xkbcommon-qt xlib xrender
        Build parts ............ libs tools
        Mode ................... debug
        Using C++11 ............ yes
        Using PCH .............. yes
        Target compiler supports:
        SSE2/SSE3/SSSE3 ...... yes/yes/yes
        SSE4.1/SSE4.2 ........ yes/yes
        AVX/AVX2 ............. yes/no

        Qt modules and options:
        Qt D-Bus ............... no
        Qt Concurrent .......... yes
        Qt GUI ................. yes
        Qt Widgets ............. yes
        JavaScriptCore JIT ..... yes (To be decided by JavaScriptCore)
        QML debugging .......... yes
        Use system proxies ..... no

        Support enabled for:
        Accessibility .......... yes
        ALSA ................... no
        CUPS ................... no
        FontConfig ............. yes
        Iconv .................. yes
        ICU .................... yes
        Image formats:
        GIF .................. yes (plugin, using system library)
        JPEG ................. yes (plugin, using system library)
        PNG .................. yes (in QtGui, using system library)
        Glib ................... no
        GStreamer .............. no
        GTK theme .............. no
        Large File ............. yes
        libudev ................ no
        Networking:
        getaddrinfo .......... yes
        getifaddrs ........... yes
        IPv6 ifname .......... yes
        OpenSSL .............. no
        NIS .................... yes
        OpenGL ................. desktop
        OpenVG ................. no
        PCRE ................... yes (bundled copy)
        pkg-config ............. yes
        PulseAudio ............. no
        QPA backends:
        DirectFB ............. no
        EGLFS ................ no
        KMS .................. no
        LinuxFB .............. yes
        XCB .................. yes (system library)
        MIT-SHM ............ yes
        Xcursor ............ yes (loaded at runtime)
        Xfixes ............. yes (loaded at runtime)
        Xi ................. no
        Xi2 ................ yes
        Xinerama ........... yes (loaded at runtime)
        Xrandr ............. yes (loaded at runtime)
        Xrender ............ yes
        XKB ................ no
        XShape ............. yes
        XSync .............. yes
        XVideo ............. yes
        Session management ..... yes
        SQL drivers:
        DB2 .................. no
        InterBase ............ no
        MySQL ................ no
        OCI .................. no
        ODBC ................. no
        PostgreSQL ........... no
        SQLite 2 ............. no
        SQLite ............... yes (plugin, using bundled copy)
        TDS .................. no
        udev ................... no
        xkbcommon .............. yes (bundled copy)
        zlib ................... yes (system library)

        NOTE: libxkbcommon 0.2.0 (or higher) not found on the system, will use
        the bundled version from 3rd party directory.

        Qt is now configured for building. Just run 'gmake'.
        Once everything is built, Qt is installed.
        You should not run 'gmake install'.

        Prior to reconfiguration, make sure you remove any leftovers from
        the previous build.

        • cd ..
        • qtbase/bin/qmake /home/bin/qt
          Info: creating super cache file /home/bin/qt/.qmake.super
          @
          Thanks
        1 Reply Last reply
        0
        • ? Offline
          ? Offline
          A Former User
          wrote on last edited by
          #4

          Any guess? Blocked on this. Tried both 5.0.2 and 5.1.1. Same problem

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by
            #5

            It was because my OS did not had fonts installed. Although, when I intall fonts, the Qt5 displays garbled text in pdf, even if the html has plain english. The same thing works fine in qt4 upon installing fonts.

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved