Merging existing PDF files using R

前端 未结 1 784
一向
一向 2020-12-19 15:02

I want to merge PDF files that already exist (already saved in my computer) using R.

I already tried to use open source softwares to merge them and it works fine bu

相关标签:
1条回答
  • 2020-12-19 15:35

    If you install pdftk (found here), then you can use the function below:

    concatenate_pdfs <- function(input_filepaths, output_filepath) {
      # Take the filepath arguments and format them for use in a system command
      quoted_names <- paste0('"', input_filepaths, '"')
      file_list <- paste(quoted_names, collapse = " ")
      output_filepath <- paste0('"', output_filepath, '"')
      # Construct a system command to pdftk
      system_command <- paste("pdftk",
                              file_list,
                              "cat",
                              "output",
                              output_filepath,
                              sep = " ")
      # Invoke the command
      system(command = system_command)
    }
    

    Which could be called as follows:

    concatenate_pdfs(input_filepaths = c("My First File.pdf", "My Second File.pdf"),
                     output_filepath = "My Combined File.pdf")
    

    This is just a user-friendly way of invoking the following system command:

    pdftk "My First File.pdf" "My Second File.pdf" cat output "My Combined File.pdf"
    
    0 讨论(0)
提交回复
热议问题