Trying to rotate PDF 90 degrees

Hello ! I am trying to export a PDF but i need it rotated 90 degrees.

This is the current way anvil is exporting the PDF:

And this is how i need it

I already tried using the ‘landscape=True’ parameter, like this:

pdf = PDFRenderer(filename='Lista_Empaque.pdf',landscape=True,page_size=(15.24,10.16),margins=0).render_form('Bodega.Despachos.Forma_Despachos',datos)

But it leads to this:

Is there any way I can rotate the PDF with code? or maybe a way to rotate all the components inside the form from which the PDF is made from?

Greetings !

You have available on the server packages like pyPDF2, which supports rotating all the pages in a PDF: https://gist.github.com/jb0hn/760447d7737555793efe48fb4192802c

I don’t know how you’d hook the two together, though.

Edit: the above assumes you’re on a paid plan that has access to server packages. If you don’t have access to server packages, and nobody comes up with a clever way to do it using Anvil itself, there are some web APIs for working with PDFs, e.g. All the help and documentation you need to enjoy our online tools for managing, convert and sign PDFs

2 Likes

THANK YOU ! i’ll check it out.

Im currently on a free plan, so ill check the second option.

If you have a local Python environment capable of doing the PDF manipulation you need, you could also use the Uplink to let that environment act as a server environment for your app!

4 Likes

UPDATE: Ended up paying for the full plan, and succesfully used PyPDF2 to rotate the PDF pages, In case anyone has the same requirement, this is the code I used in the server:

pdf = PDFRenderer(filename='Lista_Empaque.pdf',page_size=(15.24,10.16), margins=0).render_form('Bodega.Despachos.Forma_Despachos',datos)
    
    with anvil.media.TempFile(pdf) as file_name:
      pdfIn = open(file_name, 'rb')
      pdfReader = PyPDF2.PdfFileReader(pdfIn)
      pdfWriter = PyPDF2.PdfFileWriter()
      
      for pageNum in range(pdfReader.numPages):
          page = pdfReader.getPage(pageNum)
          page.rotateClockwise(90)
          pdfWriter.addPage(page)
      
      pdfOut = open('/tmp/rotated.pdf', 'wb')
      pdfWriter.write(pdfOut)
      pdfOut.close()
      pdfIn.close()
      
      PDF=anvil.media.from_file('/tmp/rotated.pdf')
    
    return PDF
1 Like