Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. QML and Qt Quick
  4. QML Export as PDF-For Report
Forum Update on Monday, May 27th 2025

QML Export as PDF-For Report

Scheduled Pinned Locked Moved Unsolved QML and Qt Quick
2 Posts 2 Posters 438 Views
  • 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.
  • T Offline
    T Offline
    tacdin
    wrote on last edited by
    #1

    Hi, image text and other items in the interface need to be reported as PDF. I've tried a few times with this but I'm still running into some issues.

    There is a resolution issue here.

    #include <Drawer.h>
    #include <QQuickWindow>
    #include <QPrinter>
    #include <QPainter>
    
    PdfExporter::PdfExporter(QQmlApplicationEngine *engine) : QQmlApplicationEngine(engine), mEngine(engine)
    {
    
    }
    
    void PdfExporter::screenShot()
    {
        foreach(QObject* obj, mEngine->rootObjects()) {
          QQuickWindow* window = qobject_cast<QQuickWindow*>(obj);
          if (window)
          {
            QImage windowImage = window->grabWindow();
            QPrinter pdfPrinter(QPrinter::HighResolution);
            pdfPrinter.setOutputFormat(QPrinter::PdfFormat);
            pdfPrinter.setOutputFileName("test.pdf");
            QPainter painter;
            painter.begin(&pdfPrinter);
            painter.drawImage(QRect(0,0,windowImage.width(),windowImage.height()),windowImage,{0,0,windowImage.width(),windowImage.height()});
            painter.end();
          }
        }
    }
    

    Here I can only create a empty pdf file.

    void PdfExporter::screenShot()
    {
        foreach(QObject* obj, mEngine->rootObjects()) {
          QQuickWindow* window = qobject_cast<QQuickWindow*>(obj);
          if (window)
          {
              QFile pdf_file("QPdfWriter.pdf");
                  pdf_file.open(QIODevice::WriteOnly);
                  QPdfWriter *pdf_writer = new QPdfWriter(&pdf_file);
                  QPainter *pdf_painter = new QPainter(pdf_writer);
                  pdf_writer->setPageSize(QPagedPaintDevice::A4);
                  pdf_painter->drawText(QRect(100, 100, 2000, 200), "pdf writer3");
                  pdf_writer->newPage();
                  pdf_painter->drawText(QRect(100, 100, 2000, 200), "pdf writer2");
                  delete pdf_painter;
                  delete pdf_writer;
                  pdf_file.close();
    
          }
        }
    }
    

    When I try this function, only a black screen appears.

    void PdfExporter::printSlide()
    {
        foreach(QObject* obj, mEngine->rootObjects()) {
          QQuickWindow* window = qobject_cast<QQuickWindow*>(obj);
          if (window)
          {
              QPrinter printer_pixmap(QPrinter::HighResolution);
              printer_pixmap.setPageSize(QPrinter::A4);
              printer_pixmap.setOutputFormat(QPrinter::PdfFormat);
              printer_pixmap.setOutputFileName("E:\\test_pixmap.pdf");
              QPixmap pixmap = QPixmap::grabWindow(window->width(), window->height());
    
              QPainter painter_pixmap;
              painter_pixmap.begin(&printer_pixmap);
              QRect rect = painter_pixmap.viewport();
              int multiple = rect.width()/pixmap.width();
              painter_pixmap.scale(multiple, multiple);
              painter_pixmap.drawPixmap(0, 0, pixmap);
              painter_pixmap.end();
          }
        }
    }
    

    I think the problem is with this line

    QPixmap pixmap = QPixmap::grabWindow(window->width(), window->height());
    

    KDAB LINK
    There is also a link, but no information is given about header files and other files to #include. I didn't understand how to do it.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      QPixmap does not have that method. However your "window" object does.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      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