Compress PDF Instructions

Learn about the pdfRest compression API.

Compressing PDF Files

When you use the compression API, select the level of compression you want to apply to your PDF file in the compression_level setting.

We provide three standard values for compressing your PDF document:

  • High: Choose High compression to make the PDF file as small as possible.
    • This may reduce the quality of the output.
    • If you choose High, you may see a difference in the way the PDF document renders when you print it or open it in a viewing tool.
  • Medium: A balance between High and Low compression.
  • Low: Preserve the quality of the PDF file even if the file ends up being larger.

The effect of each setting on each parameter is as follows:

Images

The final quality of images in the PDF file after compression.

  • High: Minimum size. Compress images aggressively to reduce the size of the PDF file.
  • Medium: A balance between high and low.
  • Low: Maximum size. Protect the final appearance of the images at the cost of a larger file size.

Customizing your profile

Your JSON profile file should include a list of settings that define exactly what kinds of changes you want to apply to your PDF document. You can make your custom profile file as long or as short as you need, depending on the types of changes you plan to apply to optimize your PDF document.

We offer a variety of options to use when optimizing a PDF document. The options you select will depend on how you want to change your output PDF documents, and on your goals.

Suppose you have a PDF document that is 18 MB and you want to make it smaller so that the file will be easier to distribute online. If you expect that your readers will be opening the file in a browser window, and it doesn’t matter if the photographs and diagrams in the document appear with a lower resolution, you could make the document smaller by compressing the images included in the file.

On the other hand, if you are working with a large PDF document that your customers are likely to want to print, but you want to make it smaller so that it downloads and prints more quickly, you probably want to leave the graphics alone. They will need to appear as sharp as possible. But you don’t need interactive content, like form fields, bookmarks, comments, or digital signatures. You can use this Custom JSON profile feature to remove items from the PDF document that will not appear on paper.

Or maybe you are building a PDF document that you intend for people to read on smart phones and other mobile devices. In this case you want to compress the document so that it opens as quickly as possible. So you would reduce the size of the images in this case as well, but given that the screens are a lot smaller than a laptop or desktop monitor, you can reduce the resolution of the images in the PDF document to be less than a PDF document that is intended for opening in a browser window.

All of the settings for compressing a document are optional, and are turned off by default. That means then that a setting is only applied if it is included in the JSON file. Flag settings must be enabled, or set to "ON" if the flag is an On/Off value. Settings that are turned off do not need to be defined in the JSON profile file. So if you wanted to you could create a custom JSON file with only a single setting, to compress images. Your JSON file might only hold five or six lines of text.

Only use lowercase characters for the keys and values you add to the JSON profile file.

The methods you can use to optimize a PDF document are sorted into the following categories.

General

These options affect the PDF document as a whole, rather than individual features, like images or fonts or bookmarks. The setting provided under General allows you to force the compression feature to generate a PDF output document even if this document will be larger than the input document it is intended to replace.

Optional. Enable this value by adding it to the profile and setting it equal to "on."

write-output-even-if-increase-in-size

If this value is set to off, if the API finds that the output file will be bigger than the original PDF input file when it is finished processing, the system will not generate an output file at all. Rather, it will write an error message saying that the file could not be optimized.

If you set this value to “on,” the API generates the PDF output file even if the optimization process produces a document bigger than the input file it is meant to replace.

Fonts

PDF documents can travel with the fonts that they need to access to properly render text. Font files can be embedded within the PDF itself. That way, no matter what machine is used to open a PDF file, the PDF is always guaranteed to look the same, and the viewing tool does not need to look for substitute fonts installed on the local desktop or laptop. But these embedded font files can make the PDF larger, maybe a lot larger, if the document needs to express characters from an Asian font set, like Mandarin or Japanese. You can enter settings in your JSON profile file to remove individual font characters or sets that you don’t need, thus reducing the size of the PDF file.

Optional. Enable any of these values by adding it to the profile and setting it equal to "on."

subset-embedded-fonts

Subsetting fonts removes unused characters from font files embedded in the PDF.

It is a best practice when working with PDF documents to embed all of the fonts used in that document into the document itself. That way, the viewing tool (like Acrobat) does not have to look for a font on the local system, or choose a substitute. But embedding font files in a PDF document can make the PDF quite large, especially if the PDF has embedded a font file for an Asian language, such as Mandarin, with tens of thousands of characters. To avoid this, a subset of the characters in the font can be saved in the PDF document. The subset font only includes the characters you expect to need when rendering the pages of that document. This often leads to a much smaller PDF.

Transparency

It is possible to stack objects, such as graphics, images, text boxes, and form fields, on top of each other on a PDF document. These objects can be partially or fully transparent, and thus can interact in various ways with objects behind them. If a set of transparencies are stacked in a PDF file, each one contributes to the final result that appears on the page, such as the colors blending together into a final color that appears.

To make a PDF document simpler you can flatten these transparencies. The flattening process combines the layers of content on a PDF page, or a stack of transparent images or colors, and renders the result as a single image, blended color, or set of text. For example, if a digital signature is flattened, the digital certificate key and related properties are removed from the signature field. The name of the person who signed the document and related information, such as the date and time stamp and the signer’s email address, appear on the page as text, but the signature field is no longer interactive.

To flatten transparencies in a PDF input document, add one of these "quality" settings to your JSON profile file. If you don't include the "quality" setting in your JSON profile, the API will not flatten transparencies.

quality

A string. The resolution level to use when flattening transparent objects. The higher the level of quality, the better the final output in print or in a browser window. But the resulting PDF document will also be larger.

  • low-quality: Line art and text 288 DPI, gradients 144 DPI
  • medium-quality: Line art and text 300 DPI, gradients 150 DPI
  • high-quality: Line art and text 1200 DPI, gradients 300 DPI

Objects

Besides graphic images and font files, a variety of other objects can be saved within a PDF document, such as blocks of JavaScript code, thumbnail images, bookmarks, tags, and/or alternate graphics images.

Use the compression feature to remove any of these objects from a PDF document. This serves to make the document smaller and easier to distribute.

Optional. Enable any of these values by adding it to the profile and setting it equal to "on."

discard-javascript-actions

Removes JavaScript content. Blocks of JavaScript code can be added to a PDF to complete a function or calculate a value, such as a user’s age when that person enters his or her birth date in a form field.

User Data

It is possible to edit PDF documents using Adobe Acrobat and other viewing and editing tools. For example, when reviewing the content in a PDF document, a user might want to add a comment. It is also possible to attach external files to a PDF document so that the file is saved as a part of the PDF, or embed a hyperlink to a web page. Finally, a user could add metadata. The compression feature can remove any of this content. It can also remove form fields, such as text boxes, check boxes, and radio buttons.

Optional. Enable any of these values by adding it to the profile and setting it equal to "on."

discard-comments-forms-multimedia

Removes interactive elements from the PDF document. These can include annotations, such as pop-up notes, comments, and highlights, as well as interactive Acrobat form fields and embedded multimedia. Besides removing the interactive elements, this option also removes the visual content associated with these elements.

Cleanup

Use the Cleanup features to set compression values for a PDF document. You can compress the entire PDF document or parts of the content, and you can also remove redundant content or select a compression method to use, as well as other changes designed to make a PDF document open more quickly.

compression

Required. Select the compression action for the file. Enter one of these values:

  • compress-entire-file: Compress document as a single unit
  • compress-document-structure: Compress the document structure only
  • remove-compression: Removes compression from file streams

Images

When a PDF document is created that includes photographs, diagrams or drawings, the original graphic file, such as a JPEG photograph or a PNG image, become images in that PDF file. You can enter settings in the JSON profile file to compress these color, grayscale, or monochrome (black and white) images.

To use, select one of color/grayscale/monochrome, and then set downsample / recompress. Example:

"images": {
    "color": {
        "downsample": {
            "trigger-dpi": 225,
            "target-dpi": 150
        },
        "recompress": {
            "type": "zip"
            "quality": "medium"
        }
    },
}

See documentation for further information.

color downsample

Ability to specify a target resolution and a trigger resolution at which color images will be recompressed.

  • trigger-dpi: Number representing the Dots per Inch. All color images above this resolution will be downsampled.
  • target-dpi: Number representing the Dots per Inch. The new resolution of downsampled color images.
"color": {
    "downsample": {
        "trigger-dpi": 225,
        "target-dpi": 150
},

Color Conversion

You can turn on this feature to convert the colors in a PDF document to a color profile you select before compressing that document.

Optional. Enable this value by adding it to the profile and setting it equal to "on."

"color-conversion": {
    "enabled": "on",
    "color-convert-action": "convert",
    "convert-intent": "profile-intent",
    "convert-profile": "acrobat9-cmyk"
}

See documentation for further information.

enabled

Enables the Datalogics PDF REST APIs to convert colors in a PDF document using a target profile.