I have a PDF that is over 6,000 pages long. I would like to split it into separate pdfs that are each 50 pages long (or any other length I choose), and save it to an output folder. I wrote the following code, but it is extremely slow, and took an hour to get through the first 1000 pages. Also, it doesn't split the last few pages that aren't divisible by 50 (although I guess I could just split those manually.) What's the best way to make this faster?
library(pdftools)
filepath = "./data/really_long_pdf.pdf"
total_pages = pdf_info(filepath)$pages
pages_per_output = 50
output_folder = "./data/output"
for (i in seq(1, total_pages, pages_per_output)) {
start_page = i
print(start_page)
end_page = min(i + pages_per_output - 1, total_pages)
output_file = paste0(output_folder, "/output_pages_", start_page, "_", end_page, ".pdf")
pdf_subset(filepath, output_file, pages = start_page:end_page)
}