Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Split PDF according to pages in R

I have a pdf file with multiple pages, but I am interested in only a subgroup of them. For example, my original PDF has 30 pages and I want only the pages 10 to 16.

I tried using the function split_pdf from tabulizer package, that only splits the pdf page to page (resulting in 200 files, one for each page), followed by merge_pdfs(which merge pdf files). It worked properly, but is taking ages (and I have around 2000 pdf files I have to split).

This is the code I am using:

split = split_pdf('file_path')

start = 10
end = 16

merge_pdfs(split[start:end], 'saving_path')

I couldn't find any better option to do this. Any help would appreciated.

like image 992
Giovana Stein Avatar asked Mar 06 '23 15:03

Giovana Stein


2 Answers

Unfortunatly, I find it a bit unclear what kind of data is in your PDF and what you are trying to extract from it. So I outline two approaches.

  1. If you have tables in the pdf, you should be able to extract the data from said pages using using:

    tab <- tabulizer::extract_tables(file = "path/file.pdf", pages = 10:16)

  2. If you only want the text, you should use pdftools which is a lot faster:

    text <- pdftools::pdf_text("path/file.pdf")[10:16]

like image 51
JBGruber Avatar answered Mar 09 '23 05:03

JBGruber


Install pdftk (if you don't already have it). Assuming it is on your path and myfile.pdf is in the current directory run this from R:

system("pdftk myfile.pdf cat 10-16 output myfile_10to16.pdf")
like image 34
G. Grothendieck Avatar answered Mar 09 '23 06:03

G. Grothendieck