Appending a pre-existing PDF to a new created by R Markdown


I am compiling an report. Most of the report is knitted programmatically to a PDF from an R Markdown script.

I need to append one or more additional PDF files obtained from a distinct source to the end of the markdown report. I don't want to scrape the PDFs; I need to append the original PDF files or images of the PDF pages and insert images as long as the page order is maintained. Some of these PDFs comprise multiple pages.

There has to be a way to do this programmatically but I'm not finding it.

Does anyone have experience accomplishing this?

Thanks in advance for your help.



I haven't personally used it much, but I believe the staplr package should be able to help you out:

It has a staple_pdf() function that lets you merge multiple pdf files into one.


You can do this programmatically within an rmarkdown document using the latex pdfpages package. Here is a sample rmarkdown document that will include the whole pdf file (adapted from this Stackoverflow answer):

title: "My Title"
output: pdf_document
  - \usepackage{pdfpages}

## My rmarkdown document

This is an R Markdown document.

## External PDF file is included below


If you want to include specific pages, you can do, for example:




You can use the online tools for merging pdf files. I use the one from foxit software.

For appending images saved as pdf files, use latex.


You can also use the standard image options in knitr.

{r, fig.align="center", fig.cap=c("Caption"), echo=FALSE}
1 Like

Thanks, everyone!

It's always nice to have multiple options to try. I appreciate your help.


1 Like

I wanted to add an addendum to my post describing the final solution to this problem.

While I didn't use any of the suggested methods specifically, in the course of investigating all three methods, the specific answer to my problem became clear. Actually, it was pretty straight-forward.

In a script file, I first called and knitted the markdown document using the render() command and saved the markdown PDF output to the same directory where the second PDF was located. In this development case, that happened to be the master project directory. If you need to specify a different directory, use the here() package.

Then, I used pdftools::pdfcombine() to combine the two PDFs and save the output to a specific directory (Again, in this example, that is the master project directory).

#create and output "two_joined_pdfs" PDF


                  output_file = "name_of_markdown_doc.pdf")

# merge "name_of_markdown_doc" PDF with "second_PDF" PDF

pdf_combine(c("name_of_markdown_doc.pdf", "second_PDF.pdf"), 
output = "two_joined_pdfs.pdf")

Thanks to @mara, @joels, and @Timesaver for sharing their insight and suggestions.



This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.