Partial Page Rendering with QtPDF
-
I am trying to render a portion of a PDF page using QtPDF. The Portion to be rendered is defined by a QRectF specified by the user.
I have set up a QGraphicsView object with a QGraphicsScene and have added to it a QGraphicsPixmap Item who's pixmap is derived from the output of the QPdfDocument.render method (the whole page gets rendered). The user is then asked to draw a rectangle over the area of the scene that requires high resolution rendering. The user-selected QRectF is then returned to a function called 'save_clip', which is supposed to render the selected area and save it to a PNG file. The code i'm using for 'save_clip' is shown below:
def save_clip(rect): pdf = QtPdf.QPdfDocument() pdf.load(filename) rsize = pdf.pagePointSize(0).toSize() rect = rect.toRect() imgsize = QtCore.QSize(rect.width(),rect.height()) roptions = QtPdf.QPdfDocumentRenderOptions() roptions.setScaledSize(rsize) roptions.setScaledClipRect(rect) qimg = pdf.render(0,imgsize,roptions) qimg.save("Test_snip.png")
The code generally works, however I've encountered an odd problem where the region rendered into the PNG does not exactly match the region selected by the user. It is very close, but not exact. The accuracy is very good in the top left of the page (closer to the origin) and gets worse as the region moves away from the origin, up to maybe ~50 pixels offset from the desired region in the bottom right corner.
There is obviously some inaccuracy in the process somewhere, but i havent been able to figure out where. It's unclear whether it is a problem arises from the QGraphicsView region selector or within the PDF renderer itself.
Any help on this one would be greatly appreciated.