Search code examples
ruby-on-railsrubypdfprawn

How to compress a pdf uploaded by a user in rails using Prawn Gem


I am new in rails and also in programming. I am trying to compress pdf using Prawn gem but I am not sure if it's the right gem to use or is there is another tool. A user is able to upload a pdf, but before save, I want to compress. I am using Active Storage for now and trying the following and is not working

elsif @test.tipo == 'pdf'
      pdf = params[:test][:book]
      mini_pdf = Prawn::Document.new(pdf.tempfile, :compress => true)

All I've done is install the gem. Nothing else.


Solution

  • Prawn is just a library for creating PDFs, not for manipulating them.

    If you want to compress PDFs, have a look at HexaPDF which is a fully-featured PDF library that can compress PDF files (see an example and the compression benchmarks).

    Nota bene: I'm the author of HexaPDF and HexaPDF is AGPL licensed with a commercial license available. It is currently probably the best pure-Ruby solution for handling PDFs.