Skip to content

Instantly share code, notes, and snippets.

@ilyasProgrammer
Created April 20, 2021 11:40
Show Gist options
  • Save ilyasProgrammer/f0a9253714d70c69338655c18a54c643 to your computer and use it in GitHub Desktop.
Save ilyasProgrammer/f0a9253714d70c69338655c18a54c643 to your computer and use it in GitHub Desktop.
Rotate PDF
import PyPDF2
to_rotate_path = '/var/tmp/to_rotate.pdf'
fh = open(to_rotate_path, "wb")
fh.write(base64.decodebytes(att.datas))
fh.close()
rotated_path = self.rotate_pdf(to_rotate_path)
def rotate_pdf(self, path):
pdf_in = open(path, 'rb')
pdf_reader = PyPDF2.PdfFileReader(pdf_in)
pdf_writer = PyPDF2.PdfFileWriter()
for pagenum in range(pdf_reader.numPages):
page = pdf_reader.getPage(pagenum)
page.rotateClockwise(90)
pdf_writer.addPage(page)
rotated_path = '/var/tmp/rotated.pdf'
pdf_out = open(rotated_path, 'wb')
pdf_writer.write(pdf_out)
pdf_out.close()
pdf_in.close()
return rotated_path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment