Compress PDFs - Optimizer - Ruby Sample Code

Sample code for using Apryse SDK to reduce PDF file size by removing redundant information and compressing data streams using the latest in image compression technology. Samples provided in Python, C++, C#, Java, Node.js (JavaScript), PHP, Ruby and VB. Learn more about our Server SDK.

1#---------------------------------------------------------------------------------------
2# Copyright (c) 2001-2023 by Apryse Software Inc. All Rights Reserved.
3# Consult LICENSE.txt regarding license information.
4#---------------------------------------------------------------------------------------
5
6require '../../../PDFNetC/Lib/PDFNetRuby'
7include PDFNetRuby
8require '../../LicenseKey/RUBY/LicenseKey'
9
10$stdout.sync = true
11
12#---------------------------------------------------------------------------------------
13# The following sample illustrates how to reduce PDF file size using 'pdftron.PDF.Optimizer'.
14# The sample also shows how to simplify and optimize PDF documents for viewing on mobile devices
15# and on the Web using 'pdftron.PDF.Flattener'.
16#
17# @note Both 'Optimizer' and 'Flattener' are separately licensable add-on options to the core PDFNet license.
18#
19# ----
20#
21# 'pdftron.PDF.Optimizer' can be used to optimize PDF documents by reducing the file size, removing
22# redundant information, and compressing data streams using the latest in image compression technology.
23#
24# PDF Optimizer can compress and shrink PDF file size with the following operations:
25# - Remove duplicated fonts, images, ICC profiles, and any other data stream.
26# - Optionally convert high-quality or print-ready PDF files to small, efficient and web-ready PDF.
27# - Optionally down-sample large images to a given resolution.
28# - Optionally compress or recompress PDF images using JBIG2 and JPEG2000 compression formats.
29# - Compress uncompressed streams and remove unused PDF objects.
30#
31# 'pdftron.PDF.Flattener' can be used to speed-up PDF rendering on mobile devices and on the Web by
32# simplifying page content (e.g. flattening complex graphics into images) while maintaining vector text
33# whenever possible.
34#
35# Flattener can also be used to simplify process of writing custom converters from PDF to other formats.
36# In this case, Flattener can be used as first step in the conversion pipeline to reduce any PDF to a
37# very simple representation (e.g. vector text on top of a background image).
38#---------------------------------------------------------------------------------------
39
40 # Relative path to the folder containing the test files.
41 input_path = "../../TestFiles/"
42 output_path = "../../TestFiles/Output/"
43 input_filename = "newsletter"
44
45 # The first step in every application using PDFNet is to initialize the
46 # library and set the path to common PDF resources. The library is usually
47 # initialized only once, but calling Initialize multiple times is also fine.
48 PDFNet.Initialize(PDFTronLicense.Key)
49
50 #--------------------------------------------------------------------------------
51 # Example 1) Simple optimization of a pdf with default settings.
52
53 doc = PDFDoc.new(input_path + input_filename + ".pdf")
54 doc.InitSecurityHandler
55 Optimizer.Optimize(doc)
56
57 doc.Save(output_path + input_filename + "_opt1.pdf", SDFDoc::E_linearized)
58 doc.Close
59
60 #--------------------------------------------------------------------------------
61 # Example 2) Reduce image quality and use jpeg compression for
62 # non monochrome images.
63 doc = PDFDoc.new(input_path + input_filename + ".pdf")
64 doc.InitSecurityHandler
65 image_settings = ImageSettings.new
66
67 # low quality jpeg compression
68 image_settings.SetCompressionMode(ImageSettings::E_jpeg)
69 image_settings.SetQuality(1)
70
71 # Set the output dpi to be standard screen resolution
72 image_settings.SetImageDPI(144,96)
73
74 # this option will recompress images not compressed with
75 # jpeg compression and use the result if the new image
76 # is smaller.
77 image_settings.ForceRecompression(true)
78
79 # this option is not commonly used since it can
80 # potentially lead to larger files. It should be enabled
81 # only if the output compression specified should be applied
82 # to every image of a given type regardless of the output image size
83 #image_settings.ForceChanges(True)
84
85 opt_settings = OptimizerSettings.new
86 opt_settings.SetColorImageSettings(image_settings)
87 opt_settings.SetGrayscaleImageSettings(image_settings)
88
89 # use the same settings for both color and grayscale images
90 Optimizer.Optimize(doc, opt_settings)
91
92 doc.Save(output_path + input_filename + "_opt2.pdf", SDFDoc::E_linearized)
93 doc.Close
94
95 #--------------------------------------------------------------------------------
96 # Example 3) Use monochrome image settings and default settings
97 # for color and grayscale images.
98
99 doc = PDFDoc.new(input_path + input_filename + ".pdf")
100 doc.InitSecurityHandler
101
102 mono_image_settings = MonoImageSettings.new
103
104 mono_image_settings.SetCompressionMode(MonoImageSettings::E_jbig2)
105 mono_image_settings.ForceRecompression(true)
106
107 opt_settings = OptimizerSettings.new
108 opt_settings.SetMonoImageSettings(mono_image_settings)
109
110 Optimizer.Optimize(doc, opt_settings)
111 doc.Save(output_path + input_filename + "_opt3.pdf", SDFDoc::E_linearized)
112 doc.Close
113
114 #----------------------------------------------------------------------
115 # Example 4) Use Flattener to simplify content in this document
116 # using default settings
117
118 doc = PDFDoc.new(input_path + "TigerText.pdf")
119 doc.InitSecurityHandler
120
121 fl = Flattener.new
122
123 # The following lines can increase the resolution of background
124 # images.
125 #fl.SetDPI(300)
126 #fl.SetMaximumImagePixels(5000000)
127
128 # This line can be used to output Flate compressed background
129 # images rather than DCTDecode compressed images which is the default
130 #fl.SetPreferJPG(false)
131
132 # In order to adjust thresholds for when text is Flattened
133 # the following function can be used.
134 #fl.SetThreshold(Flattener::E_threshold_keep_most)
135
136 # We use e_fast option here since it is usually preferable
137 # to avoid Flattening simple pages in terms of size and
138 # rendering speed. If the desire is to simplify the
139 # document for processing such that it contains only text and
140 # a background image e_simple should be used instead.
141 fl.Process(doc, Flattener::E_fast)
142 doc.Save(output_path + "TigerText_flatten.pdf", SDFDoc::E_linearized)
143 doc.Close
144
145
146 # ----------------------------------------------------------------------
147 # Example 5) Optimize a PDF for viewing using SaveViewerOptimized.
148
149 doc = PDFDoc.new(input_path + input_filename + ".pdf")
150 doc.InitSecurityHandler
151
152 opts = ViewerOptimizedOptions.new
153
154 # set the maximum dimension (width or height) that thumbnails will have.
155 opts.SetThumbnailSize(1500)
156
157 # set thumbnail rendering threshold. A number from 0 (include all thumbnails) to 100 (include only the first thumbnail)
158 # representing the complexity at which SaveViewerOptimized would include the thumbnail.
159 # By default it only produces thumbnails on the first and complex pages.
160 # The following line will produce thumbnails on every page.
161 # opts.SetThumbnailRenderingThreshold(0)
162
163 doc.SaveViewerOptimized(output_path + input_filename + "_SaveViewerOptimized.pdf", opts)
164 doc.Close
165 PDFNet.Terminate

Did you find this helpful?

Trial setup questions?

Ask experts on Discord

Need other help?

Contact Support

Pricing or product questions?

Contact Sales