Skip to content
How to reduce the size of generated PDF reports

How to reduce the size of generated PDF reports

To utilize UniPDF’s compression capabilities, you need to access the PdfWriter and set Optimization options, as follows:

c.SetPdfWriterAccessFunc(func(w *model.PdfWriter) error {
    w.SetOptimizer(optimize.New(optimize.Options{
        CombineDuplicateDirectObjects: true,
        CombineIdenticalIndirectObjects: true,
        CombineDuplicateStreams: true,
        CompressStreams: true,
        UseObjectStreams: true,
        SubsetFonts: true,
        CleanContentstream: true,
    }))

    return nil
 })

Playground Example

The following Play illustrates how to do this and prints out information about the size reduction. The default Play example shows a reduction from 1.65kB down to 1.28kB, which is actually fairly impressive given the simple content of the document. For complex reports with embedded fonts, subsetting and such can be enabled to get significant improvements.