1

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.

Code

from qgis.gui import *

huff_model = iface.activeLayer()

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

table = QgsComposerAttributeTable(c)
table.setComposerMap(composerMap)
table.setVectorLayer(huff_model)
c.addItem(table)

printer = QPrinter()
printer.setOutputFormat(QPrinter.PdfFormat)
printer.setOutputFileName("C:/Users/Oski/Py_internship/Huff_Model_Oski/Outputs/out.pdf")
printer.setPaperSize(QSizeF(c.paperWidth(), c.paperHeight()), QPrinter.Millimeter)
printer.setFullPage(True)

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

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.

1

before you add the table to the composer try adding:

table.setMaximumNumberOfFeatures(huff_model.featureCount())
  • 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.