Created
March 19, 2012 16:47
-
-
Save wch/2118833 to your computer and use it in GitHub Desktop.
Tests of PDF file sizes using qpdf
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.