Combine Multiple Images Into a Single PDF
-
wrote on 29 Jun 2016, 15:27 last edited by
Hi! No need for external libraries. QPdfWriter can create documents with multiple pages.
-
wrote on 29 Jun 2016, 15:34 last edited by
I thought that if somebody asks this question, the answer from the documentation it is not suitable for him. I made a mistake.
-
Hi! No need for external libraries. QPdfWriter can create documents with multiple pages.
wrote on 29 Jun 2016, 18:02 last edited by@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
-
wrote on 29 Jun 2016, 18:08 last edited by
@koronabora thank you but I prefer not to use 3rd party lib
-
@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
@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.
-
@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
wrote on 29 Jun 2016, 18:26 last edited by@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(); } }
-
@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(); } }
wrote on 30 Jun 2016, 18:28 last edited by@Wieland thank you
-
wrote on 30 Jun 2016, 18:29 last edited by
@geor :-)
-
@geor :-)
wrote on 20 Jul 2016, 19:53 last edited by@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
-
@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
@geor said:
const QString fileName("C:/Qt");
that is the actual filename ?
I would expect something like
const QString fileName("c:/mydoc.pdf"); -
wrote on 20 Jul 2016, 20:08 last edited by
To add to what @mrjj said, do you have write access to
C:/
? -
@geor said:
const QString fileName("C:/Qt");
that is the actual filename ?
I would expect something like
const QString fileName("c:/mydoc.pdf"); -
To add to what @mrjj said, do you have write access to
C:/
?wrote on 20 Jul 2016, 20:57 last edited byThis post is deleted! -
To add to what @mrjj said, do you have write access to
C:/
?wrote on 20 Jul 2016, 21:03 last edited by@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:
-
@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
hi
You will / might need to scale the picture or its really small. -
wrote on 21 Jul 2016, 21:10 last edited by
@mrjj one more question lol
the size of my widget is too big to fit well on a A4 paper without looking deformed. So I want to break the layout of it's parent, resize my widget it and then take it's pixmap to put it in an A4 so it is not deformed (with the grab function) but the problem is I can't break the layout of the parent i can only delete (in code) and when I delete it it deletes everything in it any tips?
-
@mrjj one more question lol
the size of my widget is too big to fit well on a A4 paper without looking deformed. So I want to break the layout of it's parent, resize my widget it and then take it's pixmap to put it in an A4 so it is not deformed (with the grab function) but the problem is I can't break the layout of the parent i can only delete (in code) and when I delete it it deletes everything in it any tips?
@geor
Hi you can use the takeAt method to get your widget back from a layout.
But maybe you can just ask the widget to render directly?
it has a render function that can draw itself to anything you like.
http://doc.qt.io/qt-5/qwidget.html#render