Menu Close

Merge PDF in Command Line

If you are looking for a command-line tool that can merge PDF on Windows, you've come to the right place. This article describes how easily 2PDF can merge PDF files, text files, images, emails, and other filetypes to PDF from the command line interface (CLI). 

2PDF has different ways to combine documents – PDFs, Word, Excel files, images, and other filetypes. You can split, merge (append & prepend), keep "as is" and even extract specific pages. To set the PDF merge option, use a 2PDF command with the multipage attribute: 

2PDF multipage option to merge files via CLI

Multipage options:

  • as is – keep the original file structure
  • split – split multipage files into separate PDF pages
  • prepend – add new files as pages to the beginning
  • append – add new files as pages to the end

How to merge many PDF files into one multipage document

If you have a few or even hundreds of PDF files and you want to join them together, just create a single multipage PDF document containing them all. 

Use this command with multipage:append attribute as an example: 

Run PDF merge from the command line

2PDF.exe -src "E:\Work\PDF\*.pdf" -dst "C:\Out" -pdf multipage:append combine:"my multipage.pdf"

This command will make 2PDF pick up all .pdf files from a defined folder (E:\Work\PDF) and combine them into a single multipage PDF document. The output PDF file with multiple pages will appear in the output folder (C:\Out).

How to merge multiple text files into a single PDF

Imagine a situation when you need to create a single PDF out of various files. They can be, for instance, DOC, DOCX, TXT or RTF documents (or any other supported filetypes). 

Use a command like this:

Merge multiple Word and RTF files into one PDF

2PDF.exe -src "E:\Work\Word DOCX and RTF files\*.*" -dst "C:\Out" -pdf multipage:append combine:"my multipage.pdf"

Here you can optionally restrict input files by file extension. For example, change *.* to *.docx or *.doc.

Press Enter and the process starts: 

2PDF command line batch conversion – Word to PDF

Once the files are merged into one "my multipage.pdf" file, it will appear in the C:\Out folder. 

Compress output PDF to reduce file size (only for Word)

You can also make output PDFs smaller when converting Word documents. 2PDF can help you store files if you save them for archival purposes, e.g., to keep document or correspondence history compact. Precisely for this task, you can use two the min_size option. 

Example command:

2PDF.exe -src "E:\Work\Word DOCX and DOC files\*.*" -dst "C:\Out" -pdf min_size:yes multipage:append combine:"my multipage.pdf"

How to merge many images into one PDF

Pretty much the same way like processing office files, you can combine different images into a PDF. They can be of the same or different formats: JPEG, TIFF, PNG, BMP, GIF, etc. 2PDF has the support for a large number of image types including rare ones. 

Example command: 

2PDF.exe -src "E:\Work\Images\*.*" -dst "C:\Out" -pdf multipage:append combine:"my multipage.pdf"

How to save multiple emails with attachments as PDF

With 2PDF, you can save multiple emails in PDF format for archival or other purposes. The program can create PDFs from EML, EMLX, MSG, HTML, MHTML, TXT and other file types.

By default, 2PDF creates a PDF document from every email letter. If there are any attachments, they will be saved as separate PDFs. 

Ways to handle email attachments

2PDF recognizes all files attached to the email, such as DOCX, PPTX, or XLSX, and processes them. Here's what 2PDF can do with the source files and their attachments:

  • separate – create separate files for each email and attachments (default option)
  • ignore – convert emails only and ignore all attached files 
  • onlyatt – convert attachments only and ignore the emails 
  • combine – join both email and its attachment to a single PDF (attachments after email)
  • combine_prepend – join both email and its attachment to a single PDF (attachments before email) 

Example command:

2PDF.exe -src "C:\Emails\*.*" -dst "C:\Out" -options attachments:ignore

How to merge all emails with attachment(s) in a folder to one PDF

If you want to create a single PDF from all emails contained in a folder, use the -pdf multipage:append switch: 

2PDF.exe -src "C:\Emails\*.*" -dst "C:\Out" -pdf multipage:append

As a result, you'll get an output "Combined.pdf" file containing your email and all attachments (if there were any). If you want the attachments to go before the email in reverse order, use -pdf multipage:prepend

How to combine emails with attachment(s) to PDFs and keep the original file names

If you want to keep the original file name for each converted file (instead of "Combined.pdf"), use {containername} macro in your command line:

2PDF.exe -src "E:\Work\Emails\*.*" -dst "C:\Out" -pdf multipage:append combine:{containername}.pdf

How to batch convert EMLX emails

Converting Apple Mail EMLX files can be performed with 2PDF. By default, it is not activated, but you can associate it with EML, and 2PDF will process it. To set EMLX file association, run this command: 

2PDF.exe -ini

Go to File extension aliases, add EMLX string next to EML file type and click OK:

How to batch convert EMLX emails to PDF
What happens with text inside emails after converting? If the original emails contain text, the resultant PDFs will keep it. They will be searchable, so you'll be able to select and copy the text in them.


Looking for other ways to process emails in bulk? Check our article by clicking the link below.

Learn about other ways of saving emails as PDF →

How to split multipage PDF into separate pages

Splitting PDFs is the opposite of merging – you create many separate files out of a single multipage document. If you want to split one or many multipage files into pages, use the multipage:split attribute. Splitting large files is easy with 2PDF. 

Split PDF into multiple pages using CLI

To do it, use a command like this:

Split a large PDF into pages from command line

2PDF.exe -src "E:\Work\PDF\*.*" -dst "C:\Out" -pdf multipage:split

Extract certain pages

The command above splits the entire source file into pages. But if you need to extract only certain pages, add a page range (pages) to the command. Like this:

2PDF.exe -src "E:\Work\PDF\*.*" -dst "C:\Out" -options pages:1,3-6 -pdf multipage:split

As a result, in this case, you'll get single-page PDF files – only pages 1, 4, 5, 6. 

How to add new pages to an existing PDF 

If you already have a PDF document and want to add more data to it from time to time, you can use multipage:append attribute for this task also. In this case, you'll need to have an existing PDF file "my multipage.pdf" located in the output folder beforehand. Here's an example command:

2PDF.exe -src "E:\Work\PDF\*.*" -dst "C:\Out" -pdf multipage:append combine:"my multipage.pdf"

The new files from the source folder will be added to the existing PDF after using this kind of command. Please note: the existing file must have the same filename as the one you specify for combined file (combine:"my multipage.pdf").

Batch merge to PDF all files listed in a text file

All examples above described how to batch merge files through command line if they are contained in a folder (like E:\Work\PDF in previous cases). There's another feature you can use – specify a TXT with a list of files as a source (-src) instead of indicating a folder path. 

This a handy method especially if:

  • you already have paths to files listed as a text (e.g., generated by other software) 
  • your source files are contained within different folders

For this method, you'll need a file listing as a TXT file. Afterward, execute a command like this:

2PDF.exe -src "@E:\Work\source files list.txt" -dst "C:\Out" -pdf multipage:append combine:"my multipage.pdf"

It doesn't matter if your source files are PDF or of other formats – this works for both cases. 

Don't have a list of file paths yet? Here's how to manually create one

  1. Select the source files you need to convert
  2. While holding Shift, right-click them and select Copy as path:
    Get a list of all files in a folder
  3. Open a newly created TXT file, e.g., "source files list" and paste file paths (Ctrl + V):
    File paths listed in a TXT file
  4. If you have more files to add to the list which located in other folders, repeat the steps 1 and 2 and then add them to the TXT file.

To sum up, we tried to describe as many cases on the topic as we could and provide you with different examples. If you find something tricky or have a specific task which can be potentially solved with 2PDF, please contact our specialists.

Other ways to merge to PDF

If combining files via command line interface is not your cup of tea, we have more PDF merge tools to do it in batch mode, automatically, on schedule or via a virtual printer. Check some other solutions below.

DocuFreezer

Desktop solution

If you are looking for a simple way to merge PDF without using the command line, but via a graphical interface (GUI), try our another tool – DocuFreezer. It can also merge and split documents.
Learn more →

Virtual printer solution

Virtual printer solution

Another way is to merge files from a virtual printer interface. This way, you can print to PDF using a virtual printer with Append all documents to the existing file or a similar option. Universal Document Converter is a virtual printer that we recommend using.
Learn more →

FolderMill

Server-side solution

If you need a fully automated method to work with files, try a real-time file processor called FolderMill. Based on Hot Folders, it can work on a server or desktop PC. You simply drop files into folders, and FolderMill automatically creates combined PDF files from them.
Learn more →