Search code examples
rimagemagickcompression

PDF convert big vector file to small pixel file within R with magick


I like to compress a R generated PDF file inside R

pages <- magick::image_read_pdf("big.pdf")  
magick::image_write(pages, format = "pdf", "small.pdf", density=150, compress="zip")

did the job (100 MB to 5 MB), but is extremly slow compared to the console

magick -density 150 -compress zip big.pdf small.pdf

Can this be done better?


Solution

  • I don't know why the times difference but if the console command is faster, try a system call from within R with system2.
    The arguments line was copied and pasted from the question and reproduced with scan.

    cmd <- "magick"
    args <- scan(text = "-density 150 -compress zip big.pdf small.pdf", what = character())
    system2(cmd, args)