Combine Multiple PDF Files Into One Using pdfLaTeX
Tags: tutorial, writing, latex, pdf
Published: 2022-09-14 14:07:57

I use LaTeX for virtually all written documents I produce. It is a necessity when writing academic papers in computer science journals and conferences. But I also use it to format my CV, to write recommendation letters for students, among many other things. It is ideal for any writing that involves formatting mathematics, and it greatly simplifies bibliography generation. Occasionally, it is necessary to produce a single pdf from a combination of documents, some of which may be beyond my control, or may be in a format that I cannot easily embed within another. For example, perhaps I need to include an appendix written by someone else in something horrid like Microsoft Word. The solution is to convert each document to a pdf, as a common format, and then combine into a single file. LaTeX is natively compiled into pdf, and just about any other format can generally convert to pdf without much difficulty.

So you have multiple pdf files that you want to combine into one, but you don't have a version of Acrobat that will do this. Here's an easy way to combine multiple pdf files using pdfLaTeX. If you don't use LaTeX, don't worry about it. You don't really need to know LaTeX to use this trick, and I have a [repository on GitHub](https://github.com/cicirello/combine-pdf-files) with a LaTeX file you can edit with the details of the pdfs you want to combine. It doesn't matter how the original pdf files were produced.

__Table of Contents:__
* [How to Combine Multiple PDFs with pdfLaTeX](#how-to-combine-multiple-pdfs-with-pdflatex)
* [GitHub Repository](#github-repository) with the full example that you can download and edit as needed
* [Where You Can Find Me](#where-you-can-find-me)


## How to Combine Multiple PDFs with pdfLaTeX

Here are the steps to combining multiple pdfs using pdfLaTeX.

### Step 0: Install a LaTeX Distribution

If you don't have LaTeX installed on your system already, then you'll need to begin by installing a LaTeX distribution. For example, [TeX Live](https://www.tug.org/texlive/) is a good choice.

### Step 1: Create a LaTex Source File

Create a LaTeX source file with a `tex` extension and named how you want the final `pdf` named (other than extension). For example, if you want your pdf named `combined.pdf`, then name your tex file `combined.tex`. It will save you from renaming files later.

In that `combined.tex` file (or whatever you named it), add the following with your favorite text editor.

```latex
\documentclass[11pt,letterpaper]{article}

\usepackage[final]{pdfpages}

\begin{document}

% The following lines are the only ones you need to edit.
\includepdf[pages=-]{file1.pdf}
\includepdf[pages=-]{file2.pdf}
\includepdf[pages=-]{file3.pdf}

\end{document}
```

The above example combines three pdf files `file1.pdf`, `file2.pdf`, and `file3.pdf` in their entirety and in that order. Simply edit these lines with the names of your pdf files.

A statement like: `\includepdf[pages=-]{file1.pdf}` will add the entire contents of `file1.pdf` to the pdf that results. If you want to include only part of one of the files, you can specify a page range. For example, perhaps you want all of `file1.pdf`, but only pages 3 to 7 of `file2.pdf`, and then all of `file3.pdf`. You can accomplish something like that with the following. 

```latex
\documentclass[11pt,letterpaper]{article}

\usepackage[final]{pdfpages}

\begin{document}

% The following lines are the only ones you need to edit.
\includepdf[pages=-]{file1.pdf}
\includepdf[pages=3-7]{file2.pdf}
\includepdf[pages=-]{file3.pdf}

\end{document}
```

### Step 2: Run pdfLaTeX.

You can now use pdfLaTeX to combine the pdfs. At the command line, in the directory containing the LaTeX source file you created above and your existing pdfs, run the following (change the `combined.tex` file to whatever filename you used above):

```Shell
pdflatex combined.tex
```

This will produce a pdf named `combined.pdf`.


## GitHub Repository

I have a GitHub repository with a LaTeX file that you can download and edit with the details of your pdfs to get you started.

{% github cicirello/combine-pdf-files %}


## Where You Can Find Me

On the Web:

{% embed https://www.cicirello.org/ %}

Follow me here on DEV:

{% user cicirello %}

Follow me on GitHub:

{% github cicirello/cicirello %}