I'm attempting to show the information of a given vector layer that I have by creating a PDF file that shows the attribute table of that same vector layer.

The problem I have is that currently I get an output, but only up to the sixth feature of the attribute table. However I want to display the entire attribute table no matter how many features there are.

  • I have tried using different layers, same result occurs
  • I have looked online for a solution extensively. The code that I am using is based of the sources that I have found by looking online.
  • I am not looking to include any other information other than the attribute table as of now
  • It is not the number of features or attributes. I've tested a layer with 10 features which should easily fit on the page but no features past the sixth feature are shown.

I believe that something with my code needs to be altered, however no matter how I tinker with it, I can't seem to get more than 6 rows to appear on the PDF file. I'm not entirely sure what needs to be changed as it's always 6, and only 6 rows.


from qgis.gui import *

huff_model = iface.activeLayer()

mapRenderer = iface.mapCanvas().mapRenderer()
c = QgsComposition(mapRenderer)
x, y = 0, 0
w, h = c.paperWidth(), c.paperHeight()
composerMap = QgsComposerMap(c, x, y, w, h)

table = QgsComposerAttributeTable(c)

printer = QPrinter()
printer.setPaperSize(QSizeF(c.paperWidth(), c.paperHeight()), QPrinter.Millimeter)

pdfPainter = QPainter(printer)
paperRectMM = printer.pageRect(QPrinter.Millimeter)
paperRectPixel = printer.pageRect(QPrinter.DevicePixel)
c.render(pdfPainter, paperRectPixel, paperRectMM)

This code can be run straight from the python console (I used it like this for testing), provided that there is a valid vector layer with an attribute table.

PDF Output

enter image description here

My apologizes for the poor image, it was difficult to capture the entire page with decent resolution. Nevertheless, it's evident what my output looks like; there are only 6 rows shown despite the fact that the vector layer has over 1000 features. I am aware that not all the attributes fit on the page, I'm only concerned with why the remaining features do not appear.


before you add the table to the composer try adding:

  • Awesome, thanks! It worked like a charm :D +1 – ShockDoctor Dec 27 '17 at 9:39
  • Glad I could help, please accept the answer by clicking the tick – firefly-orange Dec 27 '17 at 11:04
  • Oops sorry, forgot about that! – ShockDoctor Dec 27 '17 at 20:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.