Assembling PDF Pages on Linux

guide

An online application accepted only one PDF, but the information I needed to submit was spread across several files: an explanatory note and three monthly bank statements. I only wanted the first page of each statement, rather than sharing pages of unrelated transactions. The upload also had a file-size limit.

I needed to assemble a new document from selected pages, preserve the original PDFs, and keep financial information off third-party PDF websites. Installing a full PDF editor for that felt unnecessary.

On Linux, this is a small job for command-line utilities. The only complication was on the statements: pdfunite refused to merge them because the PDF was encrypted.

Select the pages, not the whole statements

The source documents were aug.pdf, sep.pdf, and jul.pdf, along with note.pdf. I needed a four-page result: the note followed by page one of each statement.

pdfseparate, from Poppler’s poppler-utils package, extracts individual pages into their own PDF files:

pdfseparate -f 1 -l 1 jul.pdf jul-page-%d.pdf
pdfseparate -f 1 -l 1 aug.pdf aug-page-%d.pdf
pdfseparate -f 1 -l 1 sep.pdf sep-page-%d.pdf

-f 1 and -l 1 limit the extraction to the first page. The %d in the destination pattern becomes the page number, producing files such as aug-page-1.pdf.

The reason for extracting pages first was practical: I could examine exactly what would be included before assembling the upload. The original statements remained untouched.

If the utilities are not installed on Debian or Ubuntu:

sudo apt install poppler-utils

Combine the files in the required order

pdfunite, included in the same package, takes the source PDFs in the order supplied and writes one output PDF:

pdfunite note.pdf jul-page-1.pdf aug-page-1.pdf sep-page-1.pdf submission.pdf

The first file becomes the first page of the output. That matters here because the explanatory note needs to precede the evidence. No page editor or intermediate image conversion is required.

But the command failed:

Unimplemented Feature: Could not merge encrypted files ('aug-page-1.pdf')

The page had already been extracted. The failure occurred at the merge operation, which has a different requirement: pdfunite does not accept encrypted input PDFs. This is a documented limitation, not an indication that the file is corrupt.

Why a PDF can open but still be encrypted

A PDF does not have to ask for an opening password to contain encryption. Its security settings can allow ordinary viewing while the file remains marked as encrypted. A viewer being able to display it therefore tells us little about whether a merging utility can process it.

To inspect the extracted page:

pdfinfo aug-page-1.pdf | grep '^Encrypted:'

pdfinfo, also from Poppler, reports the PDF’s encryption status. This distinguishes an actual document property from a guess about why pdfunite failed.

For a PDF I own or am authorized to process, qpdf can create an unencrypted working copy:

qpdf --decrypt aug-page-1.pdf aug-page-1-clear.pdf

Unlike printing the page to an image and building a new PDF, qpdf works on the PDF structure. The aim here is not to change the displayed content. It is to produce an input that the merging utility can accept. The original encrypted page remains unchanged.

If qpdf is not already available on Debian or Ubuntu:

sudo apt install qpdf

A document that requires an opening password may require that password for qpdf too.

Now the merge can be retried using the unencrypted copies:

pdfunite note.pdf jul-page-1-clear.pdf aug-page-1-clear.pdf sep-page-1-clear.pdf submission.pdf

Only the problematic input needed to change. There was no reason to rebuild the other extracted pages or convert all four PDFs into images.

Check the result before uploading

Successful execution is not the whole test. The output still needs the correct number of pages, the correct order, readable financial information, and a size accepted by the upload form.

pdfinfo submission.pdf | grep -E '^(Pages|Encrypted):'
ls -lh submission.pdf

pdfinfo reports the page count and encryption status. ls -lh shows the file size. Neither command checks the visual content, so I still open submission.pdf and inspect the four pages before sending it.

Because qpdf --decrypt creates an unprotected copy, I also remove unnecessary temporary files after verifying the result. Keeping the processing local avoids uploading bank statements to an unrelated PDF service, but it does not remove the responsibility to handle the local copies carefully.

What this changes for the next PDF job

The useful distinction is between selecting pages, joining files, and handling document encryption. These are separate operations with separate constraints.

The sequence I used was pdfseparate to select pages, pdfunite to assemble the document, and qpdf when the merge exposed an encrypted input. If I know a source PDF is encrypted before starting, I can decrypt an authorized working copy first, then extract the required page from that copy. Poppler’s pdfseparate manual also specifies unencrypted input, even though the extraction happened to succeed in this case.

I don’t need a PDF editor for every small PDF task. I need to know which operation is required, what its input constraints are, and how to verify the resulting document.

References