Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

QML Export as PDF-For Report



  • 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.


  • Lifetime Qt Champion

    Hi,

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


Log in to reply