[PyQt] Exporting an image to a PDF
Vincent Vande Vyvre
vincent.vandevyvre at swing.be
Sat Oct 20 18:55:12 BST 2012
Le 20/10/12 18:21, Ajay Garg a écrit :
>
>
> On Sat, Oct 20, 2012 at 8:43 PM, Vincent Vande Vyvre
> <vincent.vandevyvre at swing.be <mailto:vincent.vandevyvre at swing.be>> wrote:
>
> Le 20/10/12 15:59, Ajay Garg a écrit :
> > Hi all.
> >
> > I have been looking for ways to get things exported to PDF; I
> must say
> > that I am pleasantly surprised by the ease of exporting a
> > text-document to a PDF :)
> >
> > Googling also says that exporting an image to pdf is "easier" than
> > exporting a text-documnt in PyQt; unfortunately for me, I have not
> > been able to find the steps to do that :-\
> > So, I will be grateful if I could be directed to a "Hello World"
> > program to do this.
> >
> >
> > My requirement is very simple ::
> >
> > * Read an image file (png/jpeg, etc).
> > * Export to PDF; simple. No editing of pixels,
> or any
> > other stuff.
> >
> >
> >
> > Looking forward to some pointers :)
> >
> >
> >
> > Regards,
> > Ajay
> >
> >
> >
> > _______________________________________________
> > PyQt mailing list PyQt at riverbankcomputing.com
> <mailto:PyQt at riverbankcomputing.com>
> > http://www.riverbankcomputing.com/mailman/listinfo/pyqt
> Minimalistic example:
>
> #----------------------------------------------------------------------
> image = QtGui.QImage("myImage.jpg")
> printer = QtGui.QPrinter()
> printer.setResolution(300)
> printer.setOutputFormat(QtGui.QPrinter.PdfFormat)
> printer.setOutputFileName("myImage.pdf")
>
> painter = QtGui.QPainter()
> painter.begin(printer)
> painter.drawImage(target, image, source)
> painter.end()
>
> #------------------------------------------------------------------------
>
> where target & source are QtCore.QRectF(sheet) & QtCore.QRectF(image)
>
> A more complete example here:
>
> http://bazaar.launchpad.net/~vincent-vandevyvre/oqapy/serie-1.0/view/head:/oqapy-1.0/printing.py
> <http://bazaar.launchpad.net/%7Evincent-vandevyvre/oqapy/serie-1.0/view/head:/oqapy-1.0/printing.py>
>
>
>
> Thanks a ton Vincent !!
> I could get myself started with image-exporting to pdf (all courtesy
> you :) )
>
>
> One thing I must tell you; right now, I just got myself bootstrapped,
> without truly putting the correct values in "target" and "source".
> I have two queries though ::
>
> a)
> "target" represents the target-canvas, of which the image will take
> the size, right?
>
>
>
> b)
> If yes, then I find it a little odd, that no such target is
> specified, while exporting a text-document to pdf.
> For example, here is my code to export a text-document to pdf ::
>
> ####################################################################################
> from PyQt4.QtGui import *
> import sys
>
> app = QApplication(sys.argv)
>
> text_file_path = open('sample.py').read()
> doc = QTextDocument(text_file_path)
>
> printer = QPrinter(QPrinter.HighResolution)
> printer.setOutputFormat(QPrinter.PdfFormat)
> printer.setOutputFileName('sample.pdf')
>
> doc.print_(printer)
> ####################################################################################
>
>
> Here, we can see that no "target" QtCore.QRectf is passed to the
> printing options.
>
> Any idea of this descrepancy? (as to why the "target" canvas is
> specified while exporting an image; but not when exporting a
> text-document, though the "target" canvas size is usually fixed,
> irrespective of the source mime-type).
Because in my example I've used QPainter.drawImage() and not
QTextDocument.print_().
See the doc:
http://www.riverbankcomputing.co.uk/static/Docs/PyQt4/html/qpainter.html#drawImage
you can also use only the origin of the image instead of target-source,
but you'll need to handle the margin parameters of your printer.
>
>
> Thanks anyways for your already rendered massive help :)
>
>
>
> Regards,
> Ajay
>
--
Vincent V.V.
Oqapy <https://launchpad.net/oqapy> . Qarte
<https://launchpad.net/qarte> . PaQager <https://launchpad.net/paqager>
More information about the PyQt
mailing list