Skip to content

Instantly share code, notes, and snippets.

@wch
Created March 19, 2012 16:47
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save wch/2118833 to your computer and use it in GitHub Desktop.
Save wch/2118833 to your computer and use it in GitHub Desktop.
Tests of PDF file sizes using qpdf
# Create the test plot
library(ggplot2)
ggplot(mtcars, aes(disp, mpg)) + geom_point()
ggsave("t.pdf", width=5, height=5, compress=FALSE)
# Use this for large files
#ggplot(diamonds, aes(carat, price, colour=color)) + geom_point()
#ggsave("t.pdf", width=5, height=5, compress=FALSE)
qpdf <- Sys.getenv("R_QPDF", "qpdf")
# Options for qpdf
compress <- c("--stream-data=compress",
"--stream-data=uncompress",
"--stream-data=preserve")
objstream <- c("--object-streams=preserve",
"--object-streams=disable",
"--object-streams=generate")
normalize <- c("--normalize-content=n",
"--normalize-content=y")
opts <- expand.grid(
compress = compress,
objstream = objstream,
normalize = normalize,
stringsAsFactors = FALSE)
library(plyr)
opts <- arrange(opts, compress, objstream, normalize)
opts$num <- rownames(opts)
# Do the conversion with each option
for (i in seq_len(nrow(opts))) {
system2(qpdf, c(as.character(opts[i, 1:3]),
"t.pdf", sprintf("t-%02d.pdf", i)))
}
# Get infomartion about the files
finfo <- file.info(list.files(pattern="^t.*.pdf"))
finfo <- finfo["size"]
finfo$name <- rownames(finfo)
finfo$num <- seq_len(nrow(finfo))
finfo <- merge(finfo,opts,all=TRUE)
# Print the table of information
finfo
@wch
Copy link
Author

wch commented Mar 19, 2012

With --object-stream=uncompress, the resulting PDFs are a constant 6KB smaller than the starting size. This is true for large and small input files.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment