27
January
2022
19:21

Operations with PDF Files from the Linux Command Line

27 January 2022 19:21

At work, I often deal with PDF files - merging files and compressing them. In order not to reboot into Windows, where I previously used the "PDF tools free" utility, I automated this process by selecting the appropriate commands for batch conversion and processing of PDF to JPEG, JPEG to PDF, PDF to PDF (reducing file size).

Item Function Terminal command
1. Merge multiple PDF files into the single File output.pdf
infile1.pdf+infile2.pdf+infile3.pdf = output.pdf
gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile='output.pdf' 'infile1.pdf' 'infile2.pdf' 'infile3.pdf'

or

qpdf --empty --pages infile1.pdf infile2.pdf -- output.pdf
Merge several pages from PDF files into the single File output.pdf

infile1.pdf(p.1, 12-13)+infile2.pdf(p.1-10)+infile3.pdf(p.44) = output.pdf
qpdf --empty --pages infile1.pdf 1,12-13 infile2.pdf 1-10 infile3.pdf 44 -- output.pdf
2. Split pages form PDF file, from 5 to 7 and 12 to the new File output.pdf qpdf input.pdf --pages . 5-7,12 -- output.pdf

or

qpdf --empty --pages input.pdf 5-7,12 -- output.pdf
Reorder pages in PDF file from 1,2,3,4 to 4,3,2,1 and save result to new File output.pdf qpdf 'infile.pdf' --pages . 4,3,2,1 -- output.pdf
Delete first page from input file consisting of 16 pages qpdf infile.pdf out.pdf --pages infile.pdf 2-17 --

or

qpdf 'infile.pdf' --pages . 2-17 -- output.pdf
Delete last page from PDF file consisting of 16 pages qpdf infile.pdf out.pdf --pages infile.pdf 1-16 --
Delete some pages from input PDF file consisting of 17 pages: delete page number 6 and pages in range 10-12 qpdf infile.pdf out.pdf --pages infile.pdf 1-5,7-9,13-17 --
3. Rotate all pages in the PDF file by 180 degrees qpdf in.pdf out.pdf --rotate=+180
Rotate pages #1 and #2 to 90 degrees clockwise qpdf in.pdf' out.pdf --rotate=+90:1-2
Rotate page #3 to 90 degrees counterclockwise qpdf in.pdf' out.pdf --rotate=-90:3
Rotate all pages in PDF files in the current directory to 90 degrees clockwise for f in $(ls ./*.pdf); do qpdf $f $f.out --rotate=+90; rm $f; mv $f.out $f; done
4. Split PDF file to pages, each page to save in separate file : infile.pdf -> out-1.pdf, out-2.pdf, ..., out-n.pdf qpdf infile.pdf out.pdf --split-pages
5. Split PDF to pages, with given output file name (mask) outXX.pdf:
infile.pdf -> out01.pdf, out02.pdf, ..., outNN.pdf (Split pages with file mask %d)
qpdf --split-pages=1 infile.pdf out%d.pdf
6. Convert PDF to JPG with pixel density 300 dpi and JPEG quality q=30 (high compression), place images of each page in the "images" folder, all files with the prefix "pg-" for pagination pg-01 .jpg, pg-02.jpg, etc. mkdir -p images && pdftoppm -jpeg -r 300 -jpegopt quality=30 'infile.pdf' images/pg
7. Batch convert each JPG file to PDF one for f in $(ls images/*.jpg); do convert $f $f.pdf ; done
8. Combine all PDF files in folder "images" into single PDF file qpdf --empty --pages ./images/*.pdf -- output.pdf
9. Recompress single PDF file (Note: executing this command sometimes does not reduce the file size: it depends on the compression ratio of the source file) gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/prepress -dNOPAUSE -dQUIET -dBATCH -sOutputFile=compressed.pdf infile.pdf
10. Recompress all PDF files in the current folder and put result into "compressed" folder). Результат положить в каталог compressed.

See note above.
mkdir -p compressed && for f in $(ls out*.pdf); do gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/prepress -dNOPAUSE -dQUIET -dBATCH -sOutputFile=./compressed/$f $f; done
11. Ultra-compress PDF file with the single command mkdir -p images && pdftoppm -jpeg -r 96 -jpegopt quality=30 'infile.pdf' images/pg && for f in $(ls images/pg-*.jpg); do convert $f $f.pdf ; done && qpdf --empty --pages ./images/*.pdf -- outfile.pdf && rm -R images
12. Reduce JPG resolution to 25%, renumber in order and save to JPG files with JPEG quality q=50 (high compression), place images in the "images" folder mkdir -p images && i=1 ; for f in $(ls *.jpg); do convert -resize 25% -quality 50 $f ./images/img$i.jpg ; let i=i+1; done
13. Reduce JPG resolution to 700x525, apply Auto Levels, apply sharpen filter, do renumbering in order and save to JPG files with JPEG quality q=50 (high compression), place images in folder "images" mkdir -p images && i=1 ; for f in $(ls *.jpg); do convert -resize 700x525 -auto-level -unsharp 0.65x0.65+1+0.9+0.04 -quality 50 $f ./images/img$i.jpg ; let i=i+1; done
14. NEW: Apply auto levels to JPEG files, increase JPG compression level to 40 (high compression), save files under original names (overwrite files over)" mogrify -auto-level -quality 40% *.jpg
15. IN ONE ACTION: Compress all PDF files, with Auto-Levels applied, and place the result in the folder "out" with names out*.pdf for p in $(ls *.pdf); do mkdir -p images && pdftoppm -jpeg -r 150 -jpegopt quality=100 $p images/pg && for f in $(ls images/pg-*.jpg); do mogrify -auto-level -quality 37% $f; convert $f $f.pdf ; done && qpdf --empty --pages ./images/*.pdf -- 'out'$p && echo $p && echo $p && rm -R images; done
16. IN ONE ACTION: Compress all PDF files *.pdf, convert pages to grayscale, with Auto Levels and Sharpen filters applied, and place the result in the same folder with names out*.pdf shopt -s nocaseglob && for p in $(ls *.pdf); do mkdir -p images && pdftoppm -jpeg -r 150 -jpegopt quality=100 $p images/pg && for f in $(ls images/pg-*.jpg); do mogrify -colorspace gray -auto-level -sharpen 15% -quality 30% $f; convert $f $f.pdf ; done && qpdf --empty --pages ./images/*.pdf -- 'out'$p && echo $p && rm -R images; done
17. IN ONE ACTION: Compress all PDF files *.pdf with Auto Levels, Resize and Sharpen filters applied, place in the folder named "compressed" mkdir compressed && shopt -s nocaseglob && for p in $(ls *.pdf); do mkdir -p images && pdftoppm -jpeg -r 150 -jpegopt quality=100 $p images/pg && for f in $(ls images/pg-*.jpg); do mogrify -auto-level -quality 37% $f; convert $f $f.pdf ; done && qpdf --empty --pages ./images/*.pdf -- './compressed/'$p && echo $p && rm -R images; done

If an error occurs when running one of the commands "convert-im6.q16: attempt to perform an operation not allowed by the security policy PDF' @ error/constitute.c/IsCoderAuthorized/421."`, you need to do the following:

  • open file policy.xml in editor, for example, nano. The XML file location depends on ImageMagick's version /etc/ImageMagick-6/policy.xml (or -7):
    sudo nano /etc/ImageMagick-6/policy.xml
    or
    sudo nano /etc/ImageMagick-7/policy.xml
  • find the <policymap> section and go to the end before the </policymap> tag
  • insert string
    <policy domain="coder" rights="read | write" pattern="PDF" />
  • save changes of the policy.xml file and exit editor.

Last change: 11/21/2023 - line No. 16 was changed and line No. 17 was added.
Translation to English: 02/16/2024.



Related publications