pagedown icon indicating copy to clipboard operation
pagedown copied to clipboard

Is there a way to reduce the file size without cutting content?

Open SunnyYShao opened this issue 3 years ago • 2 comments

I am using pagedown for cv and am having hard time keeping my file under the max file limits when submitting my job application. I was wondering if there is a way to reduce the size of the final pdf file.

SunnyYShao avatar Feb 02 '21 06:02 SunnyYShao

It is possible to use Ghostscript to reduce the pdf file size (see https://www.ghostscript.com/doc/current/VectorDevices.htm#PSPDF_IN).

For instance, you may try the -dPDFSETTINGS option. If Ghostscript is installed on your system and can be found with tools::find_gs_cmd(), you can reduce a pdf file like that:

compress_pdf <- function(input, output, 
                         distill = c("screen", "ebook", "printer", "prepress", "default")) {

  distill <- match.arg(distill)

  system2(
    tools::find_gs_cmd(), 
    args = shQuote(c(
      "-sDEVICE=pdfwrite",
      "-dCompatibilityLevel=1.4",
      sprintf("-dPDFSETTINGS=/%s", distill),
      "-o", output,
      input
    ))
  )
  
  compression_ratio <- 100 * (1 - file.size(output) / file.size(input))
  message(sprintf("Compression ratio: %.2f %%", compression_ratio))
}

input <- pagedown::chrome_print("https://pagedown.rbind.io/html-resume")

compress_pdf(input, "html-resume-screen.pdf", "screen")

Please note that this kind of compression reduces the quality of the pdf.

RLesur avatar Feb 03 '21 23:02 RLesur

Regarding pdf compression, some R helper also exists thanks to ROpensci great work.

See pdftools::pdf_compress() which is exporting qpdf::pdf_compress() from the qpdf package

cderv avatar Feb 04 '21 09:02 cderv