A question that often comes across various help lists is how to combine or split an output from an R graphics device. Maybe you have looped/combined multiple visuals into a single pdf to avoid cluttering your working directory and now you want to pull various pages out. Or maybe you have several different pdfs of various sizes you’d like to combine into a single multi page file (example-click here-). This post utilizes 2 short videos to demonstrate combining and splitting R produced pdfs.
This post serves two purposes:
- To show Windows users how to combine and split pdf’s (sorry this works only for Windows users)
- To challenge R bloggers who use other operating systems to perform the same combine and split tasks
First you’ll need to download PDF24 Editor (a free program)

Click Here
Combining Multiple R pdf Graphics in a Single File
Splitting R pdf Pages into Separate Files
Pretty easy. Now I challenge R bloggers who use Mac and Linux to provide the same “FREE” functionality for their platforms. Ideally, someone has an approach that spans multiple platforms.
If you have an alternate method for any operating system please provide a link to your blog in the comments below.
Tyler,
Your challenge is actually pretty easy. Just install Ghostscript, and make sure it is in your path. Then, to combine PDFs (no matter what size the original pages were), simply do:
gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=output.pdf input1.pdf input2.pdf input3.pdfSplitting apart a multi-page PDF to separate pages is similarly easy:
gs -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER \-dFirstPage=1 -dLastPage=3 \
-sOutputFile=output%3d.pdf input.pdf
This will create files named
output001.pdf,output002.pdf, and so on.I think the only thing different across platforms is how Ghostscrpit is invoked.
Of course, if you want to stick in R, you can also whip up a function like the following using
system()(assuming, of course, you already have Ghostscript installed).mergePDF <- function(infiles, outfile, os = "UNIX") {version = switch(os,
UNIX = "gs",
Win32 = "gswin32c",
Win64 = "gswin64c")
pre = " -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile="
system(paste(paste(version, pre, outfile, sep = ""), infiles, collapse = " "))
}
Then, you can use it as follows (note the format for
infiles; the function would probably need to be modified for dealing with files with spaces in the names):mergePDF("file1.pdf file2.pdf file3.pdf", "mergefromR.pdf", "Win32")@Ananda I recently had the need to merge multiple plots together many times. THis was an awesome tool. Thanks for sharing.
Use pdfsam: http://www.pdfsam.org/?page_id=32 Multiplatform, easy to use.
Linux cli: use pdftk. E.g, for joining:
pdftk in1.pdf in2.pdf cat output out1.pdf
For splitting use the cat option (see man page of pdftk for plenty of examples)
I haven’t tested this, but I guess that you can do this from within R using a system() call once you have pdftk installed.
-grtz
Easy, free (GNU GPL) and cross-platform: pdftk (http://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/). You merge in a few seconds:
pdftk PDF1.pdf PDF2.pdf PDF3.pdf cat output PDF-merge.pdf
and you split in a matter of seconds too (in this example, p.1):
pdftk PDF-merge.pdf cat 1 output PDF1.pdf
And many more things… Just look at the doc!
Mathieu
pdftk. You can use it from script (e.g. from within R), and is multiplataform (runs on Windows too).
For fun:
splitPDF <- function(infile, outfile, digits) {OS = paste(.Platform$OS.type, .Machine$sizeof.pointer, sep = "")
version = switch(OS,
unix4 = "gs",
unix8 = "gs",
windows4 = "gswin32c",
windows8 = "gswin64c")
out = paste(outfile, "%0", digits, "d.pdf", sep = "")
pre = " -sDEVICE=pdfwrite -dNOPAUSE -dBATCH -dSAFER -sOutputFile="
system(paste(paste(version, pre, out, sep = ""),
infile, collapse = " "))
}
Note that this function has in-built OS detection (to the degree that might be most commonly required). A similar approach can be used with the earlier function.
digitsis the number of digits you want as part of the file name. Usage would be:splitPDF('"Input File With Spaces In The Name.pdf"', "R-wrote-me", 4)Just for the record I use PDF sam in a similar (though not as elegant) way as Ananda. A ghostscript or PDFsam package would be nice!
Nothing beats pdftk, especially not a windows program that is probably going to be maintained only for a short time, and then left abandoned…