Difference between revisions of "PDF"

From braindump
Jump to navigation Jump to search
 
(17 intermediate revisions by the same user not shown)
Line 1: Line 1:
[[Category: Hacks]] [[Category: PDF]]
== Reduce the size of a PDF file consisting of scanned images ==
== Reduce the size of a PDF file consisting of scanned images ==

=== Ghostscript ===
Platforms: Linux, Unix, MacOS X, Windows<br />
Requires: [http://www.ghostscript.com/ Ghostscript]

Ghostscript delivers, in my opinion, better results than ImageMagick when down-sampling PDF documents entirely comprised of images. Although the arguments are rather lengthy if used often it makes sense to write a script for it.

The assumption is that the documents to be converted were scanned at a high resolution. Ghostscripts <tt>pdfwrite</tt> device has a switch called <tt>PDFSETTINGS</tt> that predefines the output settings. The names are aligned with Adobe Distiller and yield similar results.

Lowest resolution with the presets is <tt>/screen</tt> and results in a very small pdf file that can be viewed on screen but is not fit for printing.
gs -q -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -dPDFSETTINGS=<span class="highlight">/screen</span> -sOutputFile=<span class="input">OUTPUT.pdf INPUT.pdf</span>
A pdf that should at least print without too many artifacts should be converted with <tt>/printer</tt>. Additionally we use a device independent color conversion strategy.
gs -q -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -dPDFSETTINGS=<span class="highlight">/printer</span> \
<span class="hightlight">-sColorConversionStrategy=/UseDeviceIndependentColor</span> \
-sOutputFile=<span class="input">OUTPUT.pdf INPUT.pdf</span>

At the time of this writing the following <tt>PDFSETTINGS</tt> were available. In order of quality, lowest first.
* <tt>/screen</tt>
* <tt>/ebook</tt>
* <tt>/default</tt>
* <tt>/printer</tt>
* <tt>/prepress</tt>

For more information consult the documentation for ps2pdf of your ghostscript version.

=== ImageMagick ===
=== ImageMagick ===
Platforms: Linux, Unix, MacOS X, Windows<br />
Platforms: Linux, Unix, MacOS X, Windows<br />
Line 7: Line 31:
It is wise to scan documents at the highest resolution possible as downsampling can be done at any point. The fastest way I found so far the tools of the [http://imagemagick.org/ ImageMagick] suite or with GhostScript.
It is wise to scan documents at the highest resolution possible as downsampling can be done at any point. The fastest way I found so far the tools of the [http://imagemagick.org/ ImageMagick] suite or with GhostScript.


Downsampling to 150 dpi without changing the type of image, aka -compress is as below.
Downsampling to 150 dpi.<br />
'''Note:''' Not setting the <tt>-compress</tt> will set the internal image type to TIFF. For full control of the outcome it is advisable to set the <tt>-compress</tt> option.
convert -density 150 INPUT.PDF OUTPUT.pdf
convert -density 150 <span class="input">INPUT.pdf OUTPUT.pdf</span>


Downsampling a PDF with images scanned at a high resolution to 150dpi converting the internal image to JPEG at a ratio of 80%. Useful for sending by mail.
Downsampling a PDF with images scanned at a high resolution to 150dpi converting the internal image to JPEG at a ratio of 80%. Useful for sending by mail.
convert -density 150 -compression jpeg -quality 80 INPUT.pdf OUTPUT.pdf
convert -density 150 -compression jpeg -quality 80 <span class="input">INPUT.pdf OUTPUT.pdf</span>


== Convert images to PDF documents ==
== Convert images to PDF documents ==
Line 23: Line 48:


Assuming you have a few images laying around that need to be converted to a PDF file.
Assuming you have a few images laying around that need to be converted to a PDF file.
convert -repage ''<format>'' -compress ''<algorithm>'' [-quality ''<quality in %>''] INPUT.tif OUTPUT.pdf
convert [-repage <span class="input">''<format>''</span>] -compress <span class="input">''<algorithm>''</span> [-quality <span class="input">''<quality in %>''</span>] <span class="input">INPUT.tif OUTPUT.pdf</span>


Creating a A4 PDF with lossy JPEG compression at a compression ration of 80%. A higher number under quality yields a clearer image but requires more disk space.
Creating a A4 PDF with lossy JPEG compression at a compression ration of 80%. A higher number under quality yields a clearer image but requires more disk space. <br />
'''Note:''' the -quality option is optional but if you want to retain full control over the outcome I would suggest you use it.
'''Note:''' If the page is already the correct size <tt>-repage</tt> is not required. The -quality option is optional but if you want to retain full control over the outcome I would suggest you use it.
convert -repage a4 -compress jpeg -quality 80 INPUT01.tif INPUT02.tif INPUT03.tif OUTPUT.pdf
convert -repage a4 -compress jpeg -quality 80 <span class="input">INPUT01.tif INPUT02.tif INPUT03.tif OUTPUT.pdf</span>


For lossless PDFs in size A4 using the the TIFF format for storage there are two options either LZW compression or ZIP. ZIP seems to be a bit more efficient. Note the -quality field is not required.
For lossless PDFs in size A4 using the the TIFF format for storage there are two options either LZW compression or ZIP. ZIP seems to be a bit more efficient. Note the -quality field is not required.
convert -repage a4 -compress lzw INPUT01.tif INPUT02.tif INPUT03.tif OUTPUT.pdf
convert -repage a4 -compress lzw <span class="input">INPUT01.tif INPUT02.tif INPUT03.tif OUTPUT.pdf</span>
or
or
convert -repage a4 -compress zip INPUT01.tif INPUT02.tif INPUT03.tif OUTPUT.pdf
convert -repage a4 -compress zip <span class="input">INPUT01.tif INPUT02.tif INPUT03.tif OUTPUT.pdf</span>


=== GUI Tools ===
=== GUI Tools ===
Line 45: Line 70:


Getting all the images out of a PDF file can be quite a task. The Poppler library comes with some handy tools that can be of tremendous help. To extract images from pdf '''pdfimages''' is easy to use.
Getting all the images out of a PDF file can be quite a task. The Poppler library comes with some handy tools that can be of tremendous help. To extract images from pdf '''pdfimages''' is easy to use.
pdfimages INPUT.pdf INPUT
pdfimages <span class="input">INPUT.pdf PREFIX</span>
will result in files with the names
will result in files with the names
INPUT-000.ppm
INPUT-000.ppm
Line 54: Line 79:
INPUT-999.ppm
INPUT-999.ppm
A better way is to preserve embedded JPEG images as such. Assuming a PDF with only embedded JPEGs we use the -j option.
A better way is to preserve embedded JPEG images as such. Assuming a PDF with only embedded JPEGs we use the -j option.
pdfimages '''-j''' INPUT.pdf INPUT
pdfimages <span class="highlight">'''-j'''</span> <span class="input">INPUT.pdf PREFIX</span>
will yield the following files
will yield the following files
INPUT-000.jpg
INPUT-000.jpg
Line 71: Line 96:
In the below examples we set the dpi to 300
In the below examples we set the dpi to 300


convert -density 300x300 MULTIPAGE.pdf OUPUT-%d.png
convert -density 300x300 <span class="input">MULTIPAGE.pdf OUPUT-<span class="highlight">%d</span>.png</span>
or
or
convert -density 300x300 -quality 100 MULTIPAGE.pdf OUPUT-%d.jpg
convert -density 300x300 -quality 100 <span class="input">MULTIPAGE.pdf OUPUT-<span class="highlight">%d</span>.jpg</span>


=== Ghostscript ===
=== Ghostscript ===
Line 83: Line 108:
To convert to a 16bit PNG with a resolution of 300 dpi the following will do.
To convert to a 16bit PNG with a resolution of 300 dpi the following will do.
'''Note:''' Not setting the <tt>-r</tt> option will most likely result in a low quality image.
'''Note:''' Not setting the <tt>-r</tt> option will most likely result in a low quality image.
gs -q -dBATCH -dNOPAUSE -sDEVICE=png16 -r300 -sOutputFile=OUTPUT-%03d.png INPUT.pdf
gs -q -dBATCH -dNOPAUSE -sDEVICE=png16 -r300 -sOutputFile=<span class="input">OUTPUT-<span class="highlight">%03d</span>.png INPUT.pdf</span>
To do the same but produce a colored TIFF
To do the same but produce a colored TIFF
gs -q -dBATCH -dNOPAUSE -sDEVICE=tiff24nc -r300 -sOutputFile=OUTPUT-%03d.tif INPUT.pdf
gs -q -dBATCH -dNOPAUSE -sDEVICE=tiff24nc -r300 -sOutputFile=<span class="input">OUTPUT-<span class="highlight">%03d</span>.tif INPUT.pdf</span>


=== GUI Tools ===
=== GUI Tools ===
Line 96: Line 121:


Merging more than one PDF into single document can be done with <tt>pdfunite</tt> coming with Poppler.
Merging more than one PDF into single document can be done with <tt>pdfunite</tt> coming with Poppler.
pdfunite INPUT01.pdf INPUT02.pdf OUTPUT.pdf
pdfunite <span class="input">INPUT01.pdf INPUT02.pdf OUTPUT.pdf</span>
'''Note:''' <tt>pdfunite</tt> does not alert you the output pdf already exists and will happily clobber it!
'''Note:''' <tt>pdfunite</tt> does not alert you if the output pdf already exists and will happily clobber it!


=== pdftk ===
=== pdftk ===
Line 104: Line 129:


<tt>pdftk</tt> will do the job as well but it's syntax is a bit more verbose.
<tt>pdftk</tt> will do the job as well but it's syntax is a bit more verbose.
pdftk INPUT01.pdf INPUT02.pdf cat output OUTPUT.pdf
pdftk <span class="input">INPUT01.pdf INPUT02.pdf</span> cat output <span class="input">OUTPUT.pdf</span>


<!--
<!--
Line 113: Line 138:
Ghostscript is not unlike ImageMagick re-writing the input files.
Ghostscript is not unlike ImageMagick re-writing the input files.


gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=OUTPUT.pdf -dBATCH INPUT01.pdf INPUT02.pdf
gs -dNOPAUSE -sDEVICE=pdfwrite -sOUTPUTFILE=<span class="input">OUTPUT.pdf</span> -dBATCH <span class="input">INPUT01.pdf INPUT02.pdf</span>
-->
-->
=== ImageMagick ===
=== ImageMagick ===
Line 121: Line 146:
<span style="color: #FF3300;">'''Disclaimer:''' Only use ImageMagick for PDFs consisting entirely of images. Compound documents will end up being converted to one image per page.</span><br />
<span style="color: #FF3300;">'''Disclaimer:''' Only use ImageMagick for PDFs consisting entirely of images. Compound documents will end up being converted to one image per page.</span><br />
ImageMagick is not really efficient joining PDFs but it will allow for downsampling of images on the fly.<br />
ImageMagick is not really efficient joining PDFs but it will allow for downsampling of images on the fly.<br />
'''Note:''' Not using <tt>-compress</tt> and <tt>-density</tt> may result in a low quality PDF.
'''Note:''' According to my tests not using <tt>-compress</tt> and <tt>-density</tt> will create a PDF with embeded TIFF images. If the original PDF was compressed in a JPEG format this may result in a file larger in size than the original it was converted from.
convert -compress jpeg -quality 90 -density 300x300 -adjoin INPUT01.pdf INPUT02.pdf OUTPUT.pdf
convert -compress jpeg -quality 90 -density 300x300 -adjoin <span class="input">INPUT01.pdf INPUT02.pdf OUTPUT.pdf</span>


=== GUI Tools ===
=== GUI Tools ===
Line 130: Line 155:
* [http://www.pdfsam.org/ PDF Split and Merge] - Linux, MacOS, Windows - FOSS
* [http://www.pdfsam.org/ PDF Split and Merge] - Linux, MacOS, Windows - FOSS
* [http://live.gnome.org/PdfMod PDFMod] - Linux/Unix - FOSS - utilizing Poppler
* [http://live.gnome.org/PdfMod PDFMod] - Linux/Unix - FOSS - utilizing Poppler

== Rotate pages ==
=== pdftk ===
Platforms: Linux, MacOS X, Windows<br />
Requirments: [http://www.pdflabs.com/tools/pdftk-the-pdf-toolkit/ pdftk]

<tt>pdftk</tt> is the easiest tool I found for this purpose. Not only can it rotate all pages in the PDF but individual pages as well.

Rotation in pdftk is either in absolute values or relative to the current page orientation.
* '''N''' 0&deg; (North; absolute)
* '''E''' 90&deg; (East; absolute)
* '''S''' 180&deg; (South; absolute)
* '''W''' 270&deg; (West; absolute)
* '''L''' -90&deg; (Left; relative)
* '''R''' +90&deg; (Right; relative)
* '''D''' +180&deg; (relative)

Rotate all pages by 90&deg;. <tt>cat</tt> expects a page range which is from the first page <tt>1</tt> to the last page <tt>end</tt> which then in turn is followed by the rotation instruction for East. <tt>E</tt>
pdftk <span class="input">INPUT01.pdf</span> cat 1-endE output <span class="input">OUTPUT.pdf</span>
Alternatively <tt>L</tt> or <tt>R</tt> can be used.
pdftk <span class="input">INPUT01.pdf</span> cat 1-endL output <span class="input">OUTPUT.pdf</span>

Assuming a PDF document with 4 pages and every page requires a different rotation the following command will do the trick.
pdftk <span class="input">INPUT01.pdf</span> cat 1E 2S 3W 4N output <span class="input">OUTPUT.pdf</span>

=== GUI Tools ===
* [http://pdfchain.sourceforge.net/ PDF Chain] - Linux/Unix - FOSS - Frontend for pdftk
* [http://www.angusj.com/pdftkb/#pdftkbuilder PDFTK Builder] - Windows - FOSS - Frontend for pdftk
* [http://www.paehl.de/pdf/gui_pdftk.html GUIPDFTK] - Linux, Windows - FOSS - Frontend for pdftk
* [http://www.pdfsam.org/ PDF Split and Merge] - Linux, MacOS, Windows - FOSS


== References ==
== References ==
* http://ardvaark.net/useful-pdf-imagemagick-recipes
* http://ardvaark.net/useful-pdf-imagemagick-recipes
[[Category: Hacks]] [[Category: PDF]] [[Category: ImageMagick]]

Latest revision as of 19:55, 19 March 2013

Reduce the size of a PDF file consisting of scanned images

Ghostscript

Platforms: Linux, Unix, MacOS X, Windows
Requires: Ghostscript

Ghostscript delivers, in my opinion, better results than ImageMagick when down-sampling PDF documents entirely comprised of images. Although the arguments are rather lengthy if used often it makes sense to write a script for it.

The assumption is that the documents to be converted were scanned at a high resolution. Ghostscripts pdfwrite device has a switch called PDFSETTINGS that predefines the output settings. The names are aligned with Adobe Distiller and yield similar results.

Lowest resolution with the presets is /screen and results in a very small pdf file that can be viewed on screen but is not fit for printing.

gs -q -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -dPDFSETTINGS=/screen -sOutputFile=OUTPUT.pdf  INPUT.pdf

A pdf that should at least print without too many artifacts should be converted with /printer. Additionally we use a device independent color conversion strategy.

gs -q -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -dPDFSETTINGS=/printer \
  -sColorConversionStrategy=/UseDeviceIndependentColor \ 
  -sOutputFile=OUTPUT.pdf  INPUT.pdf

At the time of this writing the following PDFSETTINGS were available. In order of quality, lowest first.

  • /screen
  • /ebook
  • /default
  • /printer
  • /prepress

For more information consult the documentation for ps2pdf of your ghostscript version.

ImageMagick

Platforms: Linux, Unix, MacOS X, Windows
Requires: ImageMagick, Ghostscript

It is wise to scan documents at the highest resolution possible as downsampling can be done at any point. The fastest way I found so far the tools of the ImageMagick suite or with GhostScript.

Downsampling to 150 dpi.
Note: Not setting the -compress will set the internal image type to TIFF. For full control of the outcome it is advisable to set the -compress option.

convert -density 150 INPUT.pdf OUTPUT.pdf 

Downsampling a PDF with images scanned at a high resolution to 150dpi converting the internal image to JPEG at a ratio of 80%. Useful for sending by mail.

convert -density 150 -compression jpeg -quality 80 INPUT.pdf OUTPUT.pdf

Convert images to PDF documents

ImageMagick

Platforms: Linux, Unix, MacOS X, Windows
Requires: ImageMagick, Ghostscript

PDFs from scans are a very common occurence these days. Depending on the purpose conversion is sometimes required. It helps to understand how PDFs store the raster data internally to make to choose best option for the task at hand.

An overview of the can be found at wikipedia In short the below examples produce either an embedded JPEG or TIFF.

Assuming you have a few images laying around that need to be converted to a PDF file.

convert [-repage <format>] -compress <algorithm> [-quality <quality in %>] INPUT.tif OUTPUT.pdf

Creating a A4 PDF with lossy JPEG compression at a compression ration of 80%. A higher number under quality yields a clearer image but requires more disk space.
Note: If the page is already the correct size -repage is not required. The -quality option is optional but if you want to retain full control over the outcome I would suggest you use it.

convert -repage a4 -compress jpeg -quality 80 INPUT01.tif INPUT02.tif INPUT03.tif  OUTPUT.pdf

For lossless PDFs in size A4 using the the TIFF format for storage there are two options either LZW compression or ZIP. ZIP seems to be a bit more efficient. Note the -quality field is not required.

convert -repage a4 -compress lzw INPUT01.tif INPUT02.tif INPUT03.tif  OUTPUT.pdf

or

convert -repage a4 -compress zip INPUT01.tif INPUT02.tif INPUT03.tif  OUTPUT.pdf

GUI Tools

There are some Windows GUI tools that can do the task as well

Extract images from PDF files

pdfimages

Platforms: Linux, MacOS X [not confirmed]
Requirments: Poppler

Getting all the images out of a PDF file can be quite a task. The Poppler library comes with some handy tools that can be of tremendous help. To extract images from pdf pdfimages is easy to use.

pdfimages INPUT.pdf PREFIX 

will result in files with the names

INPUT-000.ppm
INPUT-001.ppm
.
.
.
INPUT-999.ppm

A better way is to preserve embedded JPEG images as such. Assuming a PDF with only embedded JPEGs we use the -j option.

pdfimages -j INPUT.pdf PREFIX

will yield the following files

INPUT-000.jpg
INPUT-001.jpg
.
.
.
INPUT-999.jpg

ImageMagick

Platforms: Linux, Unix, MacOS X, Windows
Requires: ImageMagick, Ghostscript

ImageMagick offers some hand here as well and is particularly handy when you want to convert to some format that the pdfimages command does not offer. Always use the -density option or ImageMagick will downsample the images. When converting to JPEGs set the -quality option to keep get the picture quality you desire.

In the below examples we set the dpi to 300

convert -density 300x300 MULTIPAGE.pdf OUPUT-%d.png

or

convert -density 300x300 -quality 100 MULTIPAGE.pdf OUPUT-%d.jpg

Ghostscript

Platforms: Linux, Unix, MacOS X, Windows
Requires: Ghostscript

Ghostscript can do the job here as well but it is rather verbose in its syntax. To get the write image type the output device has to be specified.

To convert to a 16bit PNG with a resolution of 300 dpi the following will do. Note: Not setting the -r option will most likely result in a low quality image.

gs -q -dBATCH -dNOPAUSE -sDEVICE=png16 -r300 -sOutputFile=OUTPUT-%03d.png  INPUT.pdf

To do the same but produce a colored TIFF

gs -q -dBATCH -dNOPAUSE -sDEVICE=tiff24nc -r300 -sOutputFile=OUTPUT-%03d.tif  INPUT.pdf

GUI Tools

  • PDFMod - Linux/Unix - FOSS - utilizing Poppler.

Merge multiple PDF files

pdfunite

Platforms: Linux, MacOS X [not confirmed]
Requirments: Poppler

Merging more than one PDF into single document can be done with pdfunite coming with Poppler.

pdfunite INPUT01.pdf INPUT02.pdf OUTPUT.pdf

Note: pdfunite does not alert you if the output pdf already exists and will happily clobber it!

pdftk

Platforms: Linux, MacOS X, Windows
Requirments: pdftk

pdftk will do the job as well but it's syntax is a bit more verbose.

pdftk INPUT01.pdf INPUT02.pdf cat output OUTPUT.pdf

ImageMagick

Platforms: Linux, Unix, MacOS X, Windows
Requires: ImageMagick, Ghostscript

Disclaimer: Only use ImageMagick for PDFs consisting entirely of images. Compound documents will end up being converted to one image per page.
ImageMagick is not really efficient joining PDFs but it will allow for downsampling of images on the fly.
Note: According to my tests not using -compress and -density will create a PDF with embeded TIFF images. If the original PDF was compressed in a JPEG format this may result in a file larger in size than the original it was converted from.

convert -compress jpeg -quality 90 -density 300x300 -adjoin INPUT01.pdf INPUT02.pdf OUTPUT.pdf

GUI Tools

Rotate pages

pdftk

Platforms: Linux, MacOS X, Windows
Requirments: pdftk

pdftk is the easiest tool I found for this purpose. Not only can it rotate all pages in the PDF but individual pages as well.

Rotation in pdftk is either in absolute values or relative to the current page orientation.

  • N 0° (North; absolute)
  • E 90° (East; absolute)
  • S 180° (South; absolute)
  • W 270° (West; absolute)
  • L -90° (Left; relative)
  • R +90° (Right; relative)
  • D +180° (relative)

Rotate all pages by 90°. cat expects a page range which is from the first page 1 to the last page end which then in turn is followed by the rotation instruction for East. E

pdftk INPUT01.pdf cat 1-endE output OUTPUT.pdf

Alternatively L or R can be used.

pdftk INPUT01.pdf cat 1-endL output OUTPUT.pdf

Assuming a PDF document with 4 pages and every page requires a different rotation the following command will do the trick.

pdftk INPUT01.pdf cat 1E 2S 3W 4N output OUTPUT.pdf

GUI Tools

References