Export as PDF UI Scene
-
Hi,
I want the images and texts appearing in the interface to be rendered as PDF. I have a button and when the button is triggered, it saves whatever is on the screen that appears in the interface as a PDF(like export as PDF) . For this, I looked at the resources and the topics opened in the forum, but I could not reach a clear result. I would be very happy if you could help with this.
-
Hi,
I want the images and texts appearing in the interface to be rendered as PDF. I have a button and when the button is triggered, it saves whatever is on the screen that appears in the interface as a PDF(like export as PDF) . For this, I looked at the resources and the topics opened in the forum, but I could not reach a clear result. I would be very happy if you could help with this.
@tacdin See https://forum.qt.io/topic/111913/print-widget-to-pdf-with-the-minimal-content
So, create a printer, create a painter with printer as parameter and call widget->render(&painter) on the widget you want to print. -
@tacdin See https://forum.qt.io/topic/111913/print-widget-to-pdf-with-the-minimal-content
So, create a printer, create a painter with printer as parameter and call widget->render(&painter) on the widget you want to print. -
Hi,
KDAB has a very interesting article on the matter.
-
Hi,
KDAB has a very interesting article on the matter.
@SGaist I'm sorry for the late reply. I just saw what you wrote. I tried something like this.
hedader file
#pragma once #include <QQmlApplicationEngine> class PdfExporter : public QQmlApplicationEngine { Q_OBJECT public: PdfExporter(QQmlApplicationEngine * engine); Q_INVOKABLE void screenShot(); // Q_INVOKABLE void writePdf(); private: QQmlApplicationEngine * mEngine; };
.cpp file
#include <Drawer.h> #include <QQuickWindow> #include <QPrinter> #include <QPainter> #include <QPdfWriter> 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.setPaperSize(QPrinter::A4); pdfPrinter.setOutputFileName("test.pdf"); pdfPrinter.setResolution(600); QPainter painter; painter.begin(&pdfPrinter); painter.drawImage(QRect(0,0,windowImage.width(),windowImage.height()),windowImage,{0,0,windowImage.width(),windowImage.height()}); painter.end(); } } }
for .qml file
Button { Layout.preferredWidth: 100 Layout.preferredHeight: 20 Layout.alignment: Qt.AlignHCenter text: "test" onClicked: drawer.screenShot() }
but the resolution is too bad. Do you have a suggestion for this?
-
Hi,
KDAB has a very interesting article on the matter.
@SGaist What is explained here is not clear enough. There is no information about header files, any of the structures to #include, and that's why I'm getting so many errors. I wish you could elaborate a little more on this link. I'm sure it's very easy for you. However, as a beginner, its very difficult.