Reducing markdown Pdf output file size

I was looking for recommendations to reduce the file size of pdf documents rendered from a markdown document . I have multiple photos, plots, and maps in the document. I have tried changing the graphics device to png and played with dpi arguments to attempt to reduce size, but have not seen much a significant change.

1 Like

Not sure about how to do it with Rmarkdown.

If you use linux or macos, you can reduce file size using gs in the console. For example:

gs -dNOPAUSE -dBATCH -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/screen -sOutputFile=output.pdf input.pdf

Quality level settings are:

  • "/screen," the lowest resolution and lowest file size, but fine for viewing on a screen;
  • "/ebook," a mid-point in resolution and file size;
  • "/printer" and "/prepress," high-quality settings used for printing PDFs.

Hi Bevan,

It might help to know why you need to reduce the size of the pdf, as this might suggest a solution.

For example, in my use case, it wasn't so much the file rendered .pdf that was the problem but the folder of .png images that was the problem. I had lots of images in my .Rmd file which generated large .html and .pdf output. I didn't care so much about the final rendered file size, but I did care about the folder of images that was getting drastically too large for git/bitbucket version control. In mycase, I stopped tracking all files that were not directly source files (like images, etc.) and only version controlled source files (like .Rmd). I put the folder of images in the .gitignore file and this has been working much better and my repositories have been much smaller.

As far as making the .pdf smaller, I would have to think on that. I have noticed that .png screen captures are larger (say 2mb) than when I convert them to say (.jpeg, which on a Mac I can control the quality down to middle or below middle for a size of 300-400kb ). If you need to make the .pdf smaller for sending through email then maybe zipping it before sending?

Again, maybe the reason for making smaller might help determine the solution, perhaps?

Best,

Jason

Just to reduce the size of reports created so not sending large file sizes when they are not necessary. Not worried about the size of the working directory as I would only be sharing the rendered pdf.

In your case then if it were me, I might try to use .jpeg's which seem, from my experience, to be smaller files.
I am also curious about how and how efficiently RMardown writes to a .pdf.
On a first pass I am thinking:

  1. To create a pdf_book using Bookdown (instead of RMarkdown) but keep the .tex file it creates as below (I believe you can put this in an _output.yml file or in the YAML header of your file)

bookdown::pdf_book:
includes:
in_header: preamble.tex
keep_tex: yes
toc: yes

  1. I might then try to take the .tex file and then use your favorite LaTeX build and compiling method. Perhaps the old school compiling LaTeX from command line

latex file.tex then dvips -Ppdf file.pdf

might create a smaller .pdf? Perhaps there are options like -Ppdf that can be passed here at command line to make resulting .pdf file size smaller (as mentioned by the other poster). This would be a first place I might start.

Best,

Jason

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.