Linux/PDF

From Omnia
Jump to navigation Jump to search

pdftk is better than ghostscript

Use pdftk!

Merge PDFs

Merge multiple PDFs with GhostScript: [1] [2]

gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -sOutputFile=temp.pdf pdf1.pdf pdf2.pdf

Merge multiple PDFs with pdftk: [3]

# must install first
pdftk pdf1.pdf pdf2.pdf cat output temp.pdf

Merge multiple PDFs with ImageMagick: [4]

convert -density 150 $(ls -rt *pdf) output.pdf
convert -density 150 *.pdf output.pdf
convert -density 600 file1.pdf file2.pdf -resize 50% new.pdf

Remove Page

Remove a page with GhostScript:

# print pages from [START_PAGE] to [END_PAGE]
gs -dBATCH -dNOPAUSE -q -sDEVICE=pdfwrite -dQUIET -dFirstPage=[START_PAGE] -dLastPage=[END_PAGE] -sOutputFile=output.pdf input.pdf

For reasons outlined by KenS, Ghostscript is not the best tool for what you want to achieve. A better tool for this task is pdftk [5]

# print pages 1, and 3-end (3 to end)
pdftk  input.pdf  cat 1 3-end  output output.pdf

Remove Secure

If signed (secure), this can be removed with qpdf:

qpdf --decrypt in.pdf out.pdf
yum install qpdf

---

Assuming it's simply a 'rights' (owner) password that restricts things like editing, printing, and copying (i.e. the password does not need to be entered to open the file) the following will remove the restrictions:

  1. Grab http://qpdf.sourceforge.net/
  2. Unzip/Install and navigate to the bin folder that holds qpdf.exe (or similar for your platform)
  3. Place the PDF you wish to work on in the same folder
  4. Run: qpdf --decrypt InputFile.pdf OutputFile.pdf (use "s if the file name has spaces).
  5. Do what you like with the OutputFile.

If your PDF file is user password protected, change step 4 to qpdf --decrypt --password=yourpassword InputFile OutputFile

ref: https://superuser.com/questions/179064/how-to-remove-security-from-a-pdf-file

This won't work for Adobe Digital Editions and may have issues with digitally-signed files.

Some discussion on the background of these owner password restrictions at https://lwn.net/Articles/335415/

Select Pages and Merge

# copy pages 55 60-end to new file
pdftk book1.pdf cat 55 60-end output book1_small.pdf
# copy last page to new file
pdftk book2.pdf cat end output book2_small.pdf
# merge files
pdftk book1_small.pdf book2_small.pdf cat output book_combined.pdf

Linux PDF

Install:

yum install ghostscript poppler-utils pdftk ImageMagick

-

Convert JPEG to PDF

Convert JPGs to PDF: (using ImageMagick) [6] [7]

convert *.jpg pictures.pdf
convert page1.jpg page2.jpg +compress file.pdf
convert `ls -1v` file.pdf
convert -page A4 -compress A4 *.png CH00.pdf
convert a.png b.png -compress jpeg -resize 1240x1753 -units PixelsPerInch -density 150x150 multipage.pdf
convert a.png b.png -compress jpeg -resize 1240x1753 \
                     -extent 1240x1753 -gravity center \
                     -units PixelsPerInch -density 150x150 multipage.pdf
convert in.jpg -resize 1240x1750 -background black -compose Copy\
              -gravity center -extent 1240x1750\
              -units PixelsPerInch -density 150 out.pdf
convert in.jpg -units PixelsPerInch -set density '%[fx:w/8.27]'\
              -repage a4 out.pdf

Set density attributes (but does not reduce actual image sizes!)

convert -page 2400x2400 -density 300 *.jpg out.pdf   # 8in x 8in @ 300dpi
convert -page 3375x2625 -density 300 *.jpg out.pdf   # 11.25in x 8.75in @ 300dpi

Reduce image quality and set density attribute:

convert -resize 400x400 -page 400x400 -density 100 *.jpg out2.pdf  # 4in x 4in @ 100dpi (low res 8x8)
convert -resize 675x525 -page 675x525 -density 100 *.jpg out2.pdf  # 6.75in x 5.25in @ 100dpi (low res 11.25x8.75)

Convert PostScript to PDF

#ps2pdf links to ps2pdf12
ps2pdf input.ps 
ps2pdf input.ps output.ps
ps2pdf12 input.ps output.ps
ps2pdf13 input.ps output.ps
ps2pdf14 input.ps output.ps

Optimize PDF

pdfopt input.pdf output.pdf

Standard Thirteen PDF Fonts

Standard Thirteen PDF Fonts (generally don't embed) [8]

*Symbol
*Times (plus four variants)
*Helvetica (plus four variants)
*Courier (plus four variants)

List Fonts in PDF

pdffonts input.pdf

References: Creating PDFs

Merge multiple PDFs

yum install pdfmerge  # rpmforge

Usage:

Syntax:  pdfmerge file1.pdf file2.pdf... fileN.pdf outfile.pdf

Has some issues: [9]

pdftk

BEST OPTION!

pdftk - PDF Tool Kit

Better Merge

pdftk in1.pdf in2.pdf cat output out1.pdf

pdftk description:

If PDF is electronic paper, then pdftk is an electronic
          : staple-remover, hole-punch, binder, secret-decoder-ring, and
          : X-Ray-glasses. Pdftk is a simple tool for doing everyday things
          : with PDF documents. Keep one in the top drawer of your desktop and
          : use it to:
          :
          :     * Merge PDF Documents
          :     * Split PDF Pages into a New Document
          :     * Decrypt Input as Necessary (Password Required)
          :     * Encrypt Output as Desired
          :     * Burst a PDF Document into Single Pages
          :     * Report on PDF Metrics, including Metadata and Bookmarks
          :     * Uncompress and Re-Compress Page Streams
          :     * Repair Corrupted PDF (Where Possible)

pdftk

See Linux/pdftk

Crop Margins

PDFcrop

Install:

yum install texlive-utils
apt-get install texlive-extra-utils

If you wish to crop a pdf with left, top, right and bottom margins of 5, 10, 20, and 30 pt (points), then run: [10]

pdfcrop --margins '5 10 20 30' input.pdf output.pdf


Crop 5pts all margins:

pdfcrop --margins 5 input.pdf output.pdf

Rulebook example:

pdfcrop --margins "-50 -35 -50 -35" STF-Rulebook.pdf out.pdf

keywords