问题
The prawn gem has an initialize option :compress
, which is described here.
Apparently it "Compresses content streams before rendering them"
.
With and without compress: true
I generated the same 14 page pdf twice. Each page has 2 photos and all sorts of text. The resulting file size was the exact same.
So my question is: in production with larger quantities of photos will I see a difference in file sizes? Or maybe compress: true
eats more or less RAM during processing? What is its purpose?
回答1:
I finally found the full answers to the three questions
1. In production with larger quantities of photos will I see a difference in file sizes?
- not really. Prawn doesn't downsample or scale any images provided; we embed the image data as-is
2. Or maybe compress: true
eats more or less RAM during processing?
- Anecdotally, for small-ish (<10MB) file sizes the extra RAM consumption associated with
compress: true
is negligible or non-existent. For larger file sizes your results may vary.
3. What is its (:compress
) purpose?
:compress
compresses pdf content streams per the prawn docs- pdf content streams are the binary data of the pdf objects. Multiple pdf objects can be combined into one content stream. Each content stream has an uncompressed text header which tells the PDF parser how to find and extract the PDF objects, but all the PDF objects themselves can be compressed. This makes the PDF smaller, potentially more secure and possibly faster to load.
回答2:
A more effective way to compress PDFs is to postprocess using Ghostscript, using a command such as:
ghostscript -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dUseCIEColor -dPDFSETTINGS=/printer -dNOPAUSE -dQUIET -dBATCH -sOutputFile=<output.pdf> <input.pdf>
This typically reduces the size of my Prawn-generated PDFs by 50% or more. The dPDFSETTINGS
option can be set to different values to change the amount of compression - see the Ghostscript docs.
来源:https://stackoverflow.com/questions/39520470/prawn-pdf-compression-compress-content-streams-what-does-it-do