Issue in QPdfWriter

vincent.vandevyvre at oqapy.eu vincent.vandevyvre at oqapy.eu
Tue Aug 23 09:07:26 BST 2022


Hi,

After an upgrade to Ubuntu 22-04, my new versions of PyQt5 are:

Python      3.10.4
Qt	        5.15.3
PyQt		5.15.6
Sip			6.5.1

And I discover an issue which don't exists before.
The issue is in the QPdfWriter module and can be reproduced with this code:

---------------------------------------------------------------------------
from PyQt5.QtCore import QSize, QRect, QRectF
from PyQt5.QtGui import QPainter, QImage, QPdfWriter, QColor

IMAGE = "_DSC0806.JPG"

class Printing:
    def __init__(self):
        self.printer = PdfPrinter(self)
        self.printer.write_pdf()

class PdfPrinter(QPdfWriter):
    def __init__(self, main):
        self.out_fname = "pdf_test.pdf"
        super().__init__(self.out_fname)
        self.setResolution(300)
        layout = self.pageLayout()

    def write_pdf(self):
        frame = QRectF(self.pageLayout().fullRectPixels(self.resolution()))
        painter = self.paintEngine()
        content = self.build_render()
        print("Create pdf")
        painter.begin(self)
        painter.drawImage(frame, content, frame)
        painter.end()

    def build_render(self):
        psize = self.pageLayout().paintRectPixels(300)
        isize = QSize(int(psize.width()), int(psize.height()))
        p_width = psize.width() - 20
        background = QImage(isize, QImage.Format_ARGB32_Premultiplied)
        background.fill(QColor(0, 0, 0, 0))
        painter = QPainter()
        painter.begin(background)
        print("Create page 1")
        img = QImage(IMAGE)
        ratio = img.height() / img.width()
        source = QRect(0, 0, img.width(), img.height())
        target = QRect(20, 20, p_width, int(p_width*ratio))
        painter.drawImage(target, img, source)
        painter.end()
        return background

if __name__ == "__main__":
    printing = Printing()
----------------------------------------------------------------------------

Running with gdb:
--------------------------------
$ gdb python3 -ex 'set args printpdf.py' -ex run
...
Reading symbols from python3...
Reading symbols from /usr/lib/debug/.build-id/92/425678868befe1e7e62cd5ae24b4bf701608a5.debug...
Starting program: /usr/bin/python3 printpdf.py
[Thread debugging using libthread_db enabled]
Using host libthread_db library "/lib/x86_64-linux-gnu/libthread_db.so.1".
Create page 1
Create pdf

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff42f4ab4 in QPainter::renderHints() const () from /lib/x86_64-linux-gnu/libQt5Gui.so.5
(gdb) bt
#0  0x00007ffff42f4ab4 in QPainter::renderHints() const () from /lib/x86_64-linux-gnu/libQt5Gui.so.5
#1  0x00007ffff4330676 in QPdfEngine::drawImage(QRectF const&, QImage const&, QRectF const&, QFlags<Qt::ImageConversionFlag>) ()
   from /lib/x86_64-linux-gnu/libQt5Gui.so.5
#2  0x00007ffff4744e31 in ?? () from /usr/lib/python3/dist-packages/PyQt5/QtGui.abi3.so
#3  0x00005555556af76e in cfunction_call (func=<built-in method drawImage of QPaintEngine object at remote 0x7ffff7901460>, 
    args=<optimized out>, kwargs=<optimized out>) at ../Objects/methodobject.c:543
#4  0x00005555556a630b in _PyObject_MakeTpCall (tstate=0x555555b5b200, 
    callable=<built-in method drawImage of QPaintEngine object at remote 0x7ffff7901460>, args=<optimized out>, nargs=<optimized out>, 
    keywords=0x0) at ../Objects/call.c:215
#5  0x000055555569ec67 in _PyObject_VectorcallTstate (kwnames=0x0, nargsf=<optimized out>, args=<optimized out>, 
    callable=<built-in method drawImage of QPaintEngine object at remote 0x7ffff7901460>, tstate=<optimized out>)
    at ../Include/cpython/abstract.h:112
#6  _PyObject_VectorcallTstate (kwnames=0x0, nargsf=<optimized out>, args=0x7ffff7ba9150, 
    callable=<built-in method drawImage of QPaintEngine object at remote 0x7ffff7901460>, tstate=<optimized out>)
    at ../Include/cpython/abstract.h:99
#7  PyObject_Vectorcall (kwnames=0x0, nargsf=<optimized out>, args=0x7ffff7ba9150, 
    callable=<built-in method drawImage of QPaintEngine object at remote 0x7ffff7901460>) at ../Include/cpython/abstract.h:123
#8  call_function (kwnames=0x0, oparg=<optimized out>, pp_stack=<synthetic pointer>, trace_info=0x7fffffffd6a0, tstate=<optimized out>)
    at ../Python/ceval.c:5867
#9  _PyEval_EvalFrameDefault (tstate=<optimized out>, f=<optimized out>, throwflag=<optimized out>) at ../Python/ceval.c:4181
#10 0x00005555556bd6c1 in _PyEval_EvalFrame (throwflag=0, 
    f=Frame 0x7ffff7ba8fc0, for file /home/vincent/Images/testing/printpdf.py, line 24, in write_pdf (self=<PdfPrinter(out_fname='pdf_test.pdf') at remote 0x7ffff7b4fd90>, frame=<QRectF at remote 0x7ffff79013f0>, painter=<QPaintEngine at remote 0x7ffff7901460>, content=<QImage at
--Type <RET> for more, q to quit, c to continue without paging--
------------------------------------


More information about the PyQt mailing list