Combining (linking) existing PDF files to R

The function below was provided by @Tyler Rinker and can be found here.

Function I am 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 link (merge) several existing PDF files into 1 pdf file. I have several files named according to id , for example: doc_123232.pdf, doc_434324.pdf, etc.

How to install in.file ?

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

EDIT: I tried this now with only 1 page pdf, but not for multiple files:

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

I get the error: had status 127 ==> I do not understand the in.file parameter

+1
source share
1 answer

Here's how to do it with a minimal reproducible example. I believe that you can parse it and figure out how to access your PDF files. The reports package is not needed, 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') 

It was a helper function within plotflow for adide to work inside R. I would most likely use gohstscript myself if I already had PDF files.

+3
source

All Articles