R: Compact PDF Files

compactPDF {tools} | R Documentation

## Compact PDF Files

### Description

Re-save PDF files (especially vignettes) more compactly. Support function for `R CMD build –compact-vignettes`.

### Usage

    compactPDF(paths,
               qpdf = Sys.which(Sys.getenv("R_QPDF", "qpdf")),
               gs_cmd = Sys.getenv("R_GSCMD", ""),
               gs_quality = Sys.getenv("GS_QUALITY", "none"),
               gs_extras = character())

    ## S3 method for class 'compactPDF'
    format(x, ratio = 0.9, diff = 1e4, ...)

### Arguments

`paths` | A character vector of paths to PDF files, or a length-one character vector naming a directory, when all ‘.pdf’ files in that directory will be used.
`qpdf` | Character string giving the path to the `qpdf` command. If empty, `qpdf` will not be used.
`gs_cmd` | Character string giving the path to the GhostScript executable, if that is to be used. On Windows this is the path to ‘gswin32c.exe’ or ‘gswin64c.exe’. If `""` (the default), the function will try to find a platform-specific path to GhostScript where required.
`gs_quality` | A character string indicating the quality required: the options are `"none"` (so GhostScript is not used), `"printer"` (300dpi), `"ebook"` (150dpi) and `"screen"` (72dpi).
`gs_extras` | An optional character vector of further options to be passed to GhostScript.
`x` | An object of class `"compactPDF"`.
`ratio, diff` | Limits for reporting: files are only reported whose sizes are reduced both by a factor of `ratio` and by `diff` bytes.
`...` | Further arguments to be passed to or from other methods.

### Details

This by default makes use of `qpdf`, available from http://qpdf.sourceforge.net/ (including as a Windows binary) and included with the CRAN OS X distribution of R. If `gs_cmd` is non-empty and `gs_quality != "none"`, GhostScript will used first, then `qpdf` if it is available. If `gs_quality != "none"` and `gs_cmd` is `""`, an attempt will be made to find a GhostScript executable.

`qpdf` and/or `gs_cmd` are run on all PDF files found, and those which are reduced in size by at least 10% and 10Kb are replaced.

The strategy of our use of `qpdf` is to (losslessly) compress both PDF streams and objects. GhostScript compresses streams and more (including downsampling and compressing embedded images) and consequently is much slower and may lose quality (but can also produce much smaller PDF files). However, quality `"ebook"` is perfectly adequate for screen viewing and printing on laser printers.

Where PDF files are changed they will become PDF version 1.5 files: these have been supported by Acrobat Reader since version 6 in 2003, so this is very unlikely to cause difficulties.

Stream compression is what most often has large gains. Most PDF documents are generated with object compression, but this does not seem to be the default for MiKTeX's `pdflatex`. For some PDF files (and especially package vignettes), using GhostScript can dramatically reduce the space taken by embedded images (often screenshots).

Where both GhostScript and `qpdf` are selected (when `gs_quality != "none"` and both executables are found), they are run in that order and the size reductions apply to the total compression achieved.

### Value

An object of class `c("compactPDF", "data.frame")`. This has two columns, the old and new sizes in bytes for the files that were changed.

There are `format` and `print` methods: the latter passes `...` to the format method, so will accept `ratio` and `diff` arguments.

### Note

The external tools used may change in future releases.

Versions of GhostScript 9.06 and later give several times better compression than 9.05 on some vignettes in CRAN packages.

### See Also

`resaveRdaFiles`.

Many other (and sometimes more effective) tools to compact PDF files are available, including Adobe Acrobat (not Reader). See the ‘Writing R Extensions’ manual.

[Package tools version 3.1.2 Index]