3

The function bellow has been kindly provided by @Tyler Rinker and can be found here

Function I'm struggling with:

  mergePDF <-  function(..., file, gsversion = NULL, in.file = NULL) {
                        if (is.null(in.file)) {
                        in.file <- substitute(...())
                        } 
                      infiles <- paste(unlist(lapply(file.folder, function(y) as.character(y))), 
                      collapse = " ")
                      if (is.null(gsversion)) {
                      gsversion <- names(which(Sys.which(c("gswin64c")) != paste("C:/Program Files/gs9.15/bin/gswin64c.exe",sep="")))
                          }   
                      pre = " -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile="
                     system(paste(paste(gsversion, pre, file, sep = ""), infiles, collapse=""))
                          }

I would like to bind (combine) multiple existing pdf file into 1 pdf file. I have multiple files named according to id for example: doc_123232.pdf, doc_434324.pdf etc.

How to set the in.file?

Ideally, I would like to set only the folder where the files are stored: something like: Sys.glob("C:/Path/doc*.pdf")

EDIT: I have tried it for now only with 1 pdf page I have, not for multiple files:

mergePDF(file="C:/1pagepdf.pdf",in.file="C:/path/doc_123232.pdf")

I'm getting error: had status 127 ==> clearly I don't understand the parameter in.file

Community
  • 1
  • 1
Maximilian
  • 4,177
  • 7
  • 46
  • 85
  • What have you tried? By default `in.file` is automatically constructed from the arguments in `...`, but you can also explicitly pass a character vector to the `in.file` argument. – Richie Cotton Nov 10 '14 at 12:05
  • I have tried passing as char vector but still getting the error 127 – Maximilian Nov 10 '14 at 14:02
  • I would use @agstudy's answer on that question instead. Much easier than building an R function to make a system call (to Ghostscript). – Thomas Nov 10 '14 at 14:17
  • sure, but if that function provided works for @Tyler Rinker should also work in general. – Maximilian Nov 10 '14 at 14:34

1 Answers1

7

Here's how to do it with a minimal reproducible example. I believe you'll be able to pick it apart and figure out how to apply to your pdfs. The reports package isn't necessary but I like the use of folder and delete in my workflow so I used it here:

library(plotflow)
library(reports)

## make a folder to store the pdfs
folder(deleteMe)

## create a bunch of various sized pdfs
lapply(1:3, function(i) {
    pdf(sprintf("deleteMe/test%s.pdf", i), width=sample(4:7, 1))
    plot(1:10, 1:10, main = sprintf("Title: test%s.pdf", i))
    dev.off()
})

## paste the paths to pdfs together in one string w/ spaces
plotflow:::mergePDF(
    in.file=paste(file.path("deleteMe", dir("deleteMe")), collapse=" "),
    file="merged.pdf"
)

## delete MWE
delete('deleteMe')

This was a helper function for within plotflow to aide work within R. I'd likely use gohstscript directly myself if I had the pdfs already.

Tyler Rinker
  • 108,132
  • 65
  • 322
  • 519