Rotating PDF in R

Hello,

I tried giving the staplr package a go and I did install the required toolkit Pdftk but I still don't get the pdf to flip even though I do get prompted to select an input pdf and an output location with file save. I am assuming I don't have the ability given this is a premium feature in the toolkit.

Is there another R way to rotate the pages in a pdf?

install.packages("staplr")

library(staplr)
rotate_pdf(page_rotation = 180, input_filepath = NULL,
           output_filepath = NULL, overwrite = TRUE)
1 Like

Any help on this?

I would even appreciate help on writing a custom function to perform this very task.

Hello,

I opened a ticket on rotating a pdf initially here: https://community.rstudio.com/t/rotating-pdf-in-r/33883

I do not get the sense there is another work around so I would like to know how you'd go about this from scratch?

Is it worthwhile to go the Reticulate route and then do it via Python or?

@GreyMerchant One option to rotate a PDF is to use the Python package PyPDF2. The code chunk below rotates the manual An Introduction to R by 180 degrees:

import PyPDF2

original = open('R-intro.pdf', 'rb')
pdf_in = PyPDF2.PdfFileReader(original)

rotated = open('R-intro-upside-down.pdf', 'wb')
pdf_out = PyPDF2.PdfFileWriter()

for pg in range(pdf_in.numPages):
    page = pdf_in.getPage(pg)
    page.rotateClockwise(180)
    pdf_out.addPage(page)

pdf_out.write(rotated)

rotated.close()
original.close()

Depending on your use case, you could run these lines manually, convert it to a command-line Python script, or integrate it into an R script via reticulate.

1 Like

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.