Combine Multiple Images Into a Single PDF
-
I have a multiple widgets each covering one page of an application I convert each widget to an image with
ui->myWidget->grab().save("image.png");
I would like to save them into a pdf later if the users decides too it is possible to convert on image into a pdf and save it with qt but how do I make all 5 images I have into one pdf with page numbers (if possible)
thank you
-
-
Hi! No need for external libraries. QPdfWriter can create documents with multiple pages.
-
I thought that if somebody asks this question, the answer from the documentation it is not suitable for him. I made a mistake.
-
@Wieland I took a look at the functions for that class and all I see is way to edit the pages but how do you add the images?
thank you
-
@koronabora thank you but I prefer not to use 3rd party lib
-
@geor
Hi
You draw the images to it.
http://www.codeprogress.com/cpp/libraries/qt/showQtExample.php?index=570&key=QPdfWriterWriteImageKeep in mind about DPI.
-
@geor Like this:
void writePdf() { const QString fileName("/home/patrick/mydoc.pdf"); const QImage image("/home/patrick/truck.jpg"); const QPoint imageCoordinates(0,0); QPdfWriter pdfWriter(fileName); pdfWriter.setPageSize(QPageSize(QPageSize::A4)); QPainter painter(&pdfWriter); for (int i=0; i<3; ++i) { painter.drawImage(imageCoordinates, image); pdfWriter.newPage(); } }
-
@Wieland thank you
-
@Wieland I keep getting qpainter::begin() reutnr false
I am even using your exact code
const QString fileName("C:/Qt"); const QImage image("C:/Users/georg/Desktop/testPdf/Untitled.jpg"); const QPoint imageCoordinates(0,0); QPdfWriter pdfWriter(fileName); pdfWriter.setPageSize(QPagedPaintDevice::A1); QPainter painter(&pdfWriter); for (int i=0; i<3; ++i) { painter.drawImage(imageCoordinates, image); pdfWriter.newPage(); qDebug()<<2; }
except for the page size becuase I am using qt 5.2.1
do you have any idea why ebcuase I cannot figure it out the debug still runs i just get the eror and it does not save
-
To add to what @mrjj said, do you have write access to
C:/
? -
This post is deleted!
-
@Wieland
once again thank you for your help it worked when I switch to the drive where the build is saved .However for some reason it is 4 empty pages maybe it's becuase of the size or something I'll try another image and see but again thank you
-
@geor
I think its just a path thingvoid writePdf() { const QString fileName("e:/mydoc.pdf"); QPdfWriter pdfWriter(fileName); pdfWriter.setPageSize(QPageSize(QPageSize::A4)); QPainter painter(&pdfWriter); for (int i=0; i<3; ++i) { painter.drawPixmap(QRect(0,0,pdfWriter.logicalDpiX()*8.3,pdfWriter.logicalDpiY()*11.7), QPixmap("e:/truck.png")); pdfWriter.newPage(); } } works here. makes PDF on e: