Combine Multiple Images Into a Single PDF
-
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:
-
@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 -
it doesn't seem to chnage much unless I am using it wrong? here is my code 1870 and 994 is the size of my widget.making the pixmap the same as the size of my widget gives me excatly the same ouput as before so i made it smaller it's better but cuts most of the widget out
ui->page1->setBackgroundRole(QPalette::BrightText); QPixmap pixMap1(600,900); ui->page1->render(&pixMap1,QPoint(),QRegion(0,0,1870,994)); const QString fileName("D:/mydoc2.pdf"); QPdfWriter pdfWriter(fileName); pdfWriter.setPageSize(QPagedPaintDevice::A4); QPainter painter(&pdfWriter); painter.drawPixmap(QRect(0,0,pdfWriter.logicalDpiX()*8.3,pdfWriter.logicalDpiY()*11.7), pixMap1); pdfWriter.newPage();
-
@geor
hi
Sorry, didn't read the right docs.
The rect is for clipping not resize the widget.
So render will not help you to render it smaller.so yes, i guess to render it in different size, you must take widget out , call resize +render and put it back it.
Not sure how well it will work if there is other stuff in the layout.
If sole widget, it should work ok. -
@mrjj Yeah I tried this method and It works but it's not perfect even if I resize the widget to the size of an A4 and then take it's pixmap fore some reason in the pdf it loses some quality (it looks a lot better in the user interface then in the pdf )even tho they are both the same size.I tried different functions from the painter class to draw the pixamp but the all seam the same not so good .If I draw the pixmap withouth any scaling it is very small in the pdf .. any ideas?
-
@geor
maybe you could try to scale it before
drawing it to pdf.
the scale function can be adjusted to aim for quality
http://doc.qt.io/qt-5/qpixmap.html#scaled
with Qt::SmoothTransformationnote. it returns the scaled pixmap not modify the original.
so something like
mypixmap = mypixmap.scaled(xx) -
@geor
well it was worth a shot.
I assume you play around with different scales?
Have you tried huge image like 1920x1080 and scale it down?
You are using http://doc.qt.io/qt-5/qpaintdevice.html#logicalDpiX
to calc how much to scale it?I dont remember quality being so bad but i was using a fairly
large resolution so the widget was not that small. -
@mrjj yes I am using it the quality is not so good however if I save it into an image its gives me a good quality so it seems taking an image instead of a pixmap a lot better.
just like this
ui->myWidget->grab().save("image.png");
but this make me save 5 usless Images and I could not find a way to just use it temporarly..