Article
· Aug 31, 2023 5m read

PDF Compression Using Ghostscript

Introduction.

Have you ever felt like your PDF files take up too much space on your server? As you collect more and more of these files, you might run into storage issues that can be costly to solve. But what if there was a way to shrink the size of these PDFs by 50% to 70% without compromising the document's visual clarity.

Today, we're going to explore a solution for this problem that I really like: GhostScript.

 

What is GhostScript?

Ghostscript is a free and open-source software suite that provides a powerful set of tools for compressing documents, including PDFs. Ghostscript offers several advantages as a compression strategy, including:

High compression rates: Ghostscript offers powerful compression algorithms that can reduce the size of documents by up to 90% or more.
Platform compatibility: Ghostscript is available for Windows, Linux, and Mac OS, making it a versatile solution that can be easily integrated into your existing system.
Free license: Ghostscript is available under the GNU General Public License, which means that it can be used, modified, and distributed freely without any licensing fees or restrictions.

 

Lets explore the pro's and con's

Advantages of Using Ghostscript for PDF Compression:

  1. Compression: Ghostscript offers a plethora of compression options tailored to the specific needs of PDF files. By intelligently compressing various elements within a PDF, such as images and fonts, Ghostscript can substantially reduce file size while maintaining readability.
  2. Quality Retention: A standout feature of Ghostscript is its ability to compress PDF files without compromising content quality. Its compression algorithms are designed to strike a balance between file size reduction and content fidelity.
  3. Customization: Ghostscript empowers users to fine-tune compression settings according to the nature of their PDF files. This level of customization ensures that vital elements within the PDF remain intact while optimizing less crucial components.
  4. Open-Source Nature: Being an open-source solution, Ghostscript offers a cost-effective strategy for PDF compression. Its accessibility enables users to harness its capabilities without the burden of licensing fees.
  5. Cross-Platform Compatibility: Ghostscript's versatility shines through its compatibility with various operating systems like Windows, Linux, and macOS. This universality ensures consistent compression results across different platforms.

Disadvantages of Using Ghostscript for PDF Compression:

  1. Learning Curve: Ghostscript’s technical nature could be a stumbling block for users unfamiliar with its intricacies. Acquiring the necessary expertise might require time and effort.
  2. Compatibility Concerns: Some PDF files, particularly those with intricate structures or unique features, might not interact seamlessly with Ghostscript's compression algorithms. This can potentially lead to errors or quality degradation.
  3. Performance Impact: While Ghostscript excels with moderate-sized PDFs, larger and complex files might suffer from slower compression speeds. This could potentially impact system performance during compression operations.
  4. External Software Dependency: Integrating Ghostscript for PDF compression necessitates the installation and configuration of external software. This introduces an additional layer of complexity and potential maintenance obligations.

 

Using Ghostscript in Windows.

We can use the follwing windows command to compress a PDF file.

gswin64c.exe -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook -dNOPAUSE -dQUIET -dBATCH -sOutputFile=output.pdf input.pdf

 

Here’s what each option does:

  • gswin64c.exe: This is the Ghostscript executable file for 64-bit Windows. If you have a different version or installation location, adjust the command accordingly.
  • -sDEVICE=pdfwrite: This option sets the output device to PDF. Ghostscript has several output devices, but we want to write a new PDF file.
  • -dCompatibilityLevel=1.4: This sets the compatibility level for the output PDF file to 1.4. This is a relatively low level, but it should be sufficient for most purposes.
  • -dPDFSETTINGS=/ebook: This sets the compression level for the output PDF file. /ebook is a medium compression level suitable for ebooks.
  1.    /screen: This is the lowest compression level and is suitable for on-screen viewing. It produces the lowest-quality output.
  2. /ebook: This compression level is suitable for ebooks and produces a medium-quality output.
  3. /printer: This compression level is suitable for printing and produces a high-quality output.
  4. /prepress: This compression level is suitable for prepress use and produces a very high-quality output.
  5. /default: This compression level is the default and produces a balance between file size and output quality.
  • -dNOPAUSE: This option tells Ghostscript not to pause after processing each page. This is useful for batch processing.
  • -dQUIET: This option tells Ghostscript to run silently without displaying any output.
  • -dBATCH: This option tells Ghostscript to exit after processing the input file.
  • -sOutputFile=output.pdf: This sets the name and location of the output file. Change output.pdf to the name and location you want to use.
  • input.pdf: This is the name and location of the input file. Change input.pdf to the name and location of the PDF file you want to compress.

 

Using ghostscript in ObjectScript

ClassMethod CompressPDF(inputFilePath As %String, outputFilePath As %String) As %Boolean
{
    // Set the path to Ghostscript executable
    Set ghostscriptPath = "/usr/bin/gs"
    // Set the compression options
    Set options = "-sDEVICE=pdfwrite -dPDFSETTINGS=/ebook -dCompatibilityLevel=1.4 -dNOPAUSE -dBATCH -sOutputFile=" _ outputFilePath _ " " _ inputFilePath
    
    // Run Ghostscript with the specified options
    Set $ZTrap = "HandleError"
    Set status = $ZF(-1, ghostscriptPath_" "_options)
    Quit 1
HandleError:
    Write "Error compressing PDF file: ", $ZStatus, " ", $ZError
    Quit 0
}

 

Summary

In conclusion, Ghostscript serves as a potent tool to streamline PDF file sizes. Its ability to compress files while preserving content quality is undoubtedly advantageous. However, the decision to adopt Ghostscript should be made after considering its associated downsides. The learning curve, compatibility issues, performance considerations, and reliance on external software must all be weighed against the benefits.

Before implementing Ghostscript for PDF compression, it's recommended to conduct thorough testing on a variety of PDF files to gauge its effectiveness within the HealthShare ecosystem. Additionally, providing training or seeking assistance from individuals with expertise in Ghostscript can help alleviate the learning curve and ensure optimal utilization of the software.

Discussion (0)1
Log in or sign up to continue