How to Make and Combine PDF Files on the Linux Command Line

Jane Kelly/Shutterstock.com

PDF files are designed to encourage sharing. Anyone can open them – in their web browser if they have nothing else. Linux lets you edit, merge, and split PDF files on the command line.

The Portable Document Format

The Portable Document Format (PDF) has solved a problem. If you created a document on a computer and wanted to share it with someone else, sending the document didn’t always work.

Even if they have the same software package that you used to create your document, their computer may not have the same fonts installed as yours. You could open the document but it would look wrong.

If they didn’t have a copy of the software you used to create the package, they wouldn’t be able to open it at all. If you were using software that was only available on Linux, there was no point in sending this document to someone who only uses Windows.

Adobe developed a new file format in 1992 and called it Portable Document Format. Documents created to this standard – ISO 32000 – contain the images and fonts needed to correctly render the file content. PDF files can be opened by PDF viewers on any platform. It was a cross-platform, simple and elegant solution.

A PDF file is not intended to be malleable like a word processing document. They don’t lend themselves easily to editing. If you need to change the content of a PDF file, it’s always better to go back to the source material, edit it, and create a new PDF file. As opposed to trying to change the content, structurally Manipulations can be made to PDF files relatively easily.

Here are some ways to create PDF files on Linux and perform some of the transformations that can be applied to them.

Creating PDF files on Linux

Many of the applications available on Linux can generate PDF files directly. LibreOffice has a button right on the toolbar that generates a PDF of the current document. It couldn’t be easier.

LibreOffice Writer's PDF button

For fine-grained control over PDF creation, it’s hard to beat the Scribus desktop publishing application.

If you need to create documents with scientific or mathematical content, for example for submission to scientific journals, an application that uses LaTeX, such as B. Texmaker, perfect for you.

If you prefer a plain text workflow, maybe with markdown, you can use pandoc to convert to and from many file formats, including PDF. We have dedicated a guide pandoc but a simple example will show you how easy it is to use.

Install Texmaker first. pandoc relies on some LaTeX libraries for PDF generation. Installing Texmaker is a convenient way to fulfill these dependencies.

That -o (Output) option is used to specify the type of file that will be created. The raw-notes.md file is a plain text markdown file.

pandoc -o new.pdf raw-notes.md

Using Pandoc to create a PDF from a Markdown file

If we open the “new.pdf” file in a PDF viewer, we see that it is a correctly formatted PDF.

Open the PDF created by pandoc

The qpdf command

That qpdf The command allows you to edit existing PDF files while preserving their content. The changes you can make are structurally. With qpdf You can perform tasks like merging PDF files, extracting pages, rotating pages, and setting and removing encryption.

To install qpdf On Ubuntu, use this command:

sudo apt install qpdf

Install qpdf on Ubuntu

The command on Fedora is:

sudo dnf install qpdf

Install qpdf on Fedora

On Manjaro you need to type:

sudo pacman -S qpdf

Install qpdf on Manjaro

Merge PDF files

First some of the qpdf Command line syntax can seem confusing. For example, many of the commands expect a PDF input file.

If a command doesn’t require one, you must use it --empty option instead. That tells qpdf no input file expected. That --pages This option allows you to select pages. If you only specify the PDF names, all pages will be used.

Use this command format to combine two PDF files into a new PDF file.

qpdf --empty --pages first.pdf second.pdf -- combined.pdf

Combine two PDF files to create a new PDF file

This command consists of:

  • qpdf: Calls the qpdf Command.
  • -empty: Told qpdf There is no input PDF. One could argue that “first.pdf” and “second.pdf” are input files, but qpdf consider them as command line parameters.
  • -Pages: Told qpdf We will work with pages.
  • first.pdf second.pdf: The two files from which we will extract the pages. We didn’t use page ranges, so all pages are used.
  • : Indicates the end of the command options.
  • combined.pdf: The name of the PDF file that will be created.

If we search for PDF files with lswe see our two original files – unchanged – and the new PDF named “combined.pdf”.

ls -hl first.pdf second.pdf combined.pdf

Using ls to list the existing and new PDF files

There are two pages in first.pdf and one page in second.pdf. The new PDF file has three pages.

The new PDF file contains all pages of the two original PDF files

You can use wildcards instead of listing a large number of source files. This command creates a new file named “all.pdf” that contains all PDF files in the current directory.

qpdf --empty --pages *.pdf -- all.pdf

Using wildcards in the qpdf command line

We can use page ranges by adding the page numbers or ranges after the filenames from which to extract the pages.

This will extract pages one and two from first.pdf and page two from second.pdf. Note that an existing “combined.pdf” will not be overwritten. It contains the selected pages added to.

qpdf --empty --pages first.pdf 1-2 second.pdf 1 -- combined.pdf

Using page ranges to select the pages to add to the new file

Page areas can be as detailed as you want. Here we are asking for a very specific set of pages from a large PDF file and creating a summary PDF file.

qpdf --empty --pages large.pdf 1-3,7,11,18-21,55 -- summary.pdf

Using a complicated set of page ranges

The output summary.pdf file contains pages 1-3, 7, 11, 18-21, and 55 of the input PDF file. That is, there are 10 pages in “summary.pdf”

Page 10 of the new PDF file is page 55 of the source file

We can see that page 10 is page 55 from the source PDF.

Split PDF files

The opposite of merging PDF files is splitting PDF files. To split a PDF into separate PDF files, each containing a single page, the syntax is simple.

The file we split is “summary.pdf” and the output file is specified as “page.pdf”. This is used as the base name. A number is added to the base name for each new file. That --split-pages option says qpdf what kind of action we take.

qpdf summary.pdf page.pdf --split-pages

Split a PDF file into many PDF files, each with one page

The output is a series of consecutively numbered PDF files.

ls page*.pdf

Use ls to list the numbered PDF files

If you don’t want to split each page, use page ranges to select the pages you want.

When we run this next command, we’re splitting a collection of single-page PDF files. The page ranges are used to specify the pages or ranges you want, but each page is still saved in an individual PDF file.

qpdf large.pdf section.pdf --pages large.pdf 1-5,11-14,60,70-100 -- --split-pages

Splitting a PDF file with page ranges

The extracted pages have names based on “section.pdf” appended with a sequential number.

ls section*.pdf

Use ls to list the numbered PDF files

If you want to extract a range of pages and save it in a single PDF, use a command of this form. Note that we don’t include those --split-pages Possibility. What we’re actually doing here is a PDF merge, but we’re just “merging” pages from a source file.

qpdf --empty --pages large.pdf 8-13 -- chapter2.pdf

Extract a series of pages from a PDF file and save them in a new PDF file

This creates a single, multi-page PDF named Chapter2.pdf.

Rotating Pages

To rotate a page, we create a new PDF file that is the same as the input PDF with the specified page rotated.

We use that --rotate opportunity to do this. That +90 means the page is rotated 90 degrees clockwise. You can rotate a page 90, 180, or 270 degrees. You can also specify the rotation in degrees counterclockwise using a negative number, but this is not required. A rotation of -90 corresponds to a rotation of +270.

The number ” separated by a colon from the rotation:” is the number of the page you want to rotate. This could be a list of page numbers and page ranges, but we’re only rotating the first page. To rotate all pages, use a page range of 1-z.

qpdf --rotate=+90:1 summary.pdf rotated1.pdf

Rotate the first page of a PDF file

The first side was shot for us.

A PDF file with the first page rotated 90 degrees clockwise

Encrypt and Decrypt

PDF documents can be encrypted so that a password is required to open them. This password is called user password. There is another password that is required to change the security and other permissions settings for a PDF file. It’s called the owner password.

To encrypt a PDF we need to use the --encrypt option and enter both passwords. The user password is the first item on the command line.

We also specify the strength of the encryption to use. You only need to switch from 256-bit encryption to 128-bit if you want to support very old PDF file viewers. We recommend you stick with 256-bit encryption.

We will create an encrypted version of the “summary.pdf” named “secret.pdf”.

qpdf --encrypt hen.rat.squid goose.goat.gibbon 256 -- summary.pdf secret.pdf

Create an encrypted PDF

When we try to open the PDF, the PDF viewer asks us for a password. Entering the user password authorizes the viewer to open the file.

A PDF viewer that prompts for the password to open an encrypted PDF file

Remember it qpdf does not change the existing PDF. It creates a new one with the changes we asked it to make. So when you create an encrypted PDF, you still have the original unencrypted version. Depending on your circumstances, you may want to delete the original PDF or keep it safe.

To decrypt a file, use the --decrypt Possibility. Obviously you must know them owner password for this to work. We have to use those --password Possibility to identify the password.

qpdf --decrypt --password=goose.goat.gibbon secret.pdf unlocked.pdf

Create a decrypted PDF file from an encrypted PDF file

The “unlocked.pdf” can be opened without a password.

qpdf is an excellent tool

We are deeply impressed by qpdf. It offers a flexible and feature-rich toolset for working with PDF files. And it’s very fast too.

Check out her well-written and detailed documentation to see how much more she can do.

Leave a Reply

Your email address will not be published. Required fields are marked *