27
January
2022
19:21

Operations with PDF files from the Linux command line

27 January 2022 19:21

At work, I often have to work 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 (compression).

Preparation:

1) Install the necessary packages:

sudo apt install poppler-utils imagemagick qpdf

2) Configure the xml file of the imagemagick program

Otherwise, an error will occur 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."

We need to edit the policy.xml file:

  • open file for editing /etc/ImageMagick-6/policy.xml(or -7 depending on version):
    sudo nano /etc/ImageMagick-6/policy.xml
    or
    sudo nano /etc/ImageMagick-7/policy.xml
  • find section <policymap> и перейти в его конец перед тегом </policymap>
  • insert line
    <policy domain="coder" rights="read | write" pattern="PDF" />
  • save file changes policy.xml

 

Ready solutions:

Item Function Terminal command
1. Merge several PDFs files into one (Merge several PDF Files into one 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 various files PDF (Merge several pages from PDF files into one File)

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. Isolate from the file pages 5-7 and 12 and save them into a separate file (Split pages 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
Change page order in the input file from 1 2 3 4 to 4 3 2 1 (Pages reorder to 4,3,2,1), save to new file output.pdf (save to new file output.pdf) qpdf 'infile.pdf' --pages . 4,3,2,1 -- output.pdf
From a file with 17 pages,**delete**first (Delete first page from file consisting 16 pages) qpdf infile.pdf out.pdf --pages infile.pdf 2-17 --

or

qpdf 'infile.pdf' --pages . 2-17 -- output.pdf
From a file with 17 pages,**delete**last (Delete last page from file consisting 16 pages) qpdf infile.pdf out.pdf --pages infile.pdf 1-16 --
From a file with 17 pages,delete specified pages: No. 6 and No. 10-12 ( from file consisting 17 pages delete page number 6 and range 10-12) qpdf infile.pdf out.pdf --pages infile.pdf 1-5,7-9,13-17 --
3. Rotate all pages 180 degrees(Rotate all pages by 180 degrees) qpdf in.pdf out.pdf --rotate=+180
Rotate pages #1 and #2 **90 degrees**clockwise qpdf in.pdf' out.pdf --rotate=+90:1-2
Turn page number 3 to **90 degrees**counterclockwise qpdf in.pdf' out.pdf --rotate=-90:3
Rotate all 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 by page, save each page into a separate PDF file (Split pages): infile.pdf -> out-1.pdf, out-2.pdf, ..., out-n.pdf qpdf infile.pdf out.pdf --split-pages
5. Split PDF by page, save each page to a separate PDF file(Split PDF on pages with output file mask)indicating the mask file: 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 a pixel density of 300 dpi and JPEG quality q=30 (high compression), place images of each page in the folder "images", files with the prefix "pg-" page numbering pg-01.jpg, pg-02.jpg, etc. mkdir -p images && pdftoppm -jpeg -r 300 -jpegopt quality=30 'infile.pdf' images/pg
7. Convert *.jpg files to PDF files without merging them(Batch convert JPG to PDF files without combining) for f in $(ls images/*.jpg); do convert $f $f.pdf ; done
8. Merge all PDF files in the images directory into a single PDF file "output.pdf"(Compound all PDFs in folder "images" into single PDF file) qpdf --empty --pages ./images/*.pdf -- output.pdf
9. Clamp one file infile.pdf to compressed.pdf (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. Compress all out*.PDF files in the current directory with one command(Recompress all PDF files in the current folder and put result into "compressed" folder). Place the result in the compressed directory.

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. Perform strong compression of a PDF file with one command(Make ultra compression of PDF file) 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%, do renumbering in order and save to files JPG with JPEG quality q=50(high compression), place the images in the folder "images" 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 brightness (autolevels), a little sharpen(sharpen), do renumbering in order and save to files JPG with JPEG quality q=50(high compression), place the images in the 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 autolevels to JPEG files, increase JPG compression ratio to 40(strong compression), save files under original names (overwrite files over)" mogrify -auto-level -quality 40% *.jpg
15. IN ONE ACTION: compress all *.PDF files into outPDF with parameters: resolution 150 ppi, Q=37%, applying autolevels of brightness(Compress all PDF files with one action, with auto-levels applied, and place the result in the same folder 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 files "*.PDF" into outPDF with parameters: resolution 120 ppi, Q=30%, convert to B/W, applying auto brightness levels and sharpening by 15%(Compress all PDF files *.pdf with one action, with auto-levels and sharpen 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: make scans darker - compress all files "*.PDF" into outPDF with parameters: resolution 200 ppi, Q=37%, applying autolevels, color conversion to gray palette and gamma=0.5(Compress all PDF files *.pdf with one action, making them darker - with auto-levels and grayscale and gamma=0.5 applied, and place the result in the same folder with names out*.pdf) shopt -s nocaseglob && for p in $(ls pvk.pdf); do mkdir -p images && pdftoppm -jpeg -r 200 -jpegopt quality=100 $p images/pg && for f in $(ls images/pg-*.jpg); do mogrify -auto-level -gamma 0.5 -colorspace Gray -quality 37% $f; convert $f $f.pdf ; done && qpdf --empty --pages ./images/*.pdf -- 'out'$p && echo $p && echo $p && rm -R images; done
18. IN ONE ACTION: compress all files "*.PDF" with parameters: resolution 120 ppi, Q=30%, applying autolevels of brightness, place the result in the "compressed" folder(Compress all PDF files with one action, with auto-levels and sharpen 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

Last change: 06/25/2024 - the first section “Preparation” was added.



Related publications