R merge pdf into one pdf
Install pdftk and ensure it is on your path (or if not on your path use the full pathname when referring to it in the system
command below). Then run the code below. No packages are used.
setwd("...directory where pdf files are located...") ##
infiles <- Sys.glob("148-*.pdf") ##
outfile <- "148.pdf" ##
system(paste("pdftk", paste(infiles, collapse = " "), "cat output", outfile))
There are some packages that provide wrappers around pdftk:
the staplr package. Unfortunately, it is probably not too useful here because it does not allow specification of the files or their order in the output -- one can only specify the input and output directories. ** Update ** The current version of staplr now allows specification of the files as mentioned in the comments.
the animation package. The
pdftk
command that this package provides is a slightly simpler alternative than usingsystem
withpdftk
directly. For concatenating it would be the following where we assume that the 3 lines marked ## above have already been run.library(animation)
ani.options(pdftk = "/path/to/pdftk") # or if on path: ani.options(pdftk = "pdftk")
pdftk(infiles, "cat", outfile, "")This link has an example of using the animation package with pdftk to burst pages.
Merging PDF easily with pdftools
The function list.files()
gets you most of the way to what you want, if you want all the files in your path folder that contain "pdf" in the name to be merged, you could do something like:
pdf_combine(list.files(path, pattern="pdf", full.names=TRUE), output = pdf_merged)
Combine (bind) existing pdf files in R
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.
combine multiple pdf plots into one file
You can use sweave/knitr
to get more flexibility and merge easily new plots ,old ones and texts:
\documentclass{article}
\usepackage{pdfpages}
\begin{document}
this my plot 1: % write some texts here
\includepdf{1.pdf}
this my plot 2:
\includepdf{2.pdf}
this my plot 3:
\includepdf{3.pdf}
this my plot 4:
\includepdf{4.pdf}
a new plot:
<<echo=FALSE>>= % chunk for new plots
x <- rnorm(100)
hist(x)
@
\end{document}
Combine multiple pages of .pdf into RMarkdown
I'm able to successfully include two different multi-page PDFs in your example document using pdfpages
:
---
title: <center> <h1>Analysis Data</h1> </center>
mainfont: Arial
output:
pdf_document:
latex_engine: xelatex
sansfont: Arial
fig_crop: false
toc: true
classoption: landscape
fontsize: {10}
geometry: margin=0.30in
header-includes:
- \usepackage{booktabs}
- \usepackage{sectsty} \sectionfont{\centering}
- \renewcommand{\contentsname}{}\vspace{-2cm}
- \usepackage{pdfpages}
---
# File One
\includepdf[pages={-}]{pdf1.pdf}
\newpage
# File Two
\includepdf[pages={-}]{pdf2.pdf}
I have two sets of pdf from different folders that i went to join as one based on the same name and output in the same folder of first pdf group
Suppose your filenames are stored in
files_1 <- c("123456.pdf", "234567.pdf", "345678.pdf", "456789.pdf")
files_2 <- c("123456_Jon.pdf","234567_Mike.pdf", "345678_Bill.pdf","456789_Ralph.pdf","Random_file.pdf")
library(qpdf)
for (file in files_1) {
ext_num <- sub("(^\\d{6}).*", "\\1", file)
target <- grepl(paste0("^", ext_num), files_2)
if (!any(target)) next
pdf_combine(c(file, file.path(directory2, files_2[target])),
output = paste(directory1, ext_num, "Join.pdf", sep = "_"))
}
should give you your desired output.
Related Topics
Force Ggplot to Evaluate Counter Variable
Geom_Smooth with Facet_Grid and Different Fitting Functions
Is There an Efficient Way to Parallelize Mapply
Fastest Way to Parse a Date-Time String to Class Date
Piecewise Function Fitting with Nls() in R
Extracting "((Adj|Noun)+|((Adj|Noun)(Noun-Prep))(Adj|Noun))Noun" from Text (Justeson & Katz, 1995)
Dplyr Row_Number Error in Rank
Install Previous Versions of R on Ubuntu
Adding Text Labels to Tmap Plot
Robust Standard Errors for Mixed-Effects Models in Lme4 Package of R
Include Non-Cran Package in Cran Package
Importing an Excel File with Greek Characters into R in The Correct Encoding
R -Apply- Convert Many Columns from Numeric to Factor