Search code examples
pythonreportlabpdfrw

Placing PDF Art on Reportlab Canvas at Specific Location


I am currently working on a project where I would like to place art which I have in PDF format onto a ReportLab canvas page at a specific location. The idea is that I will be dynamically placing 4 separate pieces of art to create one of the pdf pages.

Right now I can place art onto a canvas as follows:

    page = PdfReader(artfile, decompress=False).pages[0]
    p = pagexobj(PageMerge().add(page).render())
    c.doForm(makerl(c, p))

I was trying to find some way to do it similar to drawImage that allows you to provide coordinates, or some sort of canvas set draw start location. Is there a way to do this without first converting to an image file as all art will be received in pdf format so it would be nice to handle it without conversion concerns. All of the examples seem to only place one item per page and I have not been able to find anything in the documentation so far.


Solution

  • rst2pdf can use pdfrw to place PDFs. You can see the code to do the placement in vectorpdf.VectorPdf.drawOn():

    canv.saveState()
    canv.translate(x, y)
    canv.scale(xscale, yscale)
    canv.doForm(xobj_name)
    canv.restoreState()
    

    Disclaimer: I am the primary author of both pdfrw, and the vectorpdf extension of rst2pdf.