A Ruby gem for validating PDF files against specific standards and requirements.
The full PDF spec is complex, with numerous ways to produce valid PDF files. Organizations receiving PDF files often need to ensure they meet specific requirements - whether PDF/X, PDF/A, or custom requirements like “images must use specific ICC color profiles”. These validation checks are called preflight.
While expensive commercial preflight software exists, this gem provides a scriptable, cost-effective alternative that integrates easily into your Ruby applications.
Key Features:
-
Validates PDFs against standard profiles (like PDF/X-1a)
-
Supports custom validation rules and profiles
-
Comprehensive image colorspace validation with ICC profile support
-
No modification of source PDFs - validation only
Unlike commercial preflight tools, this library focuses on detection rather than correction of issues.
gem install preflight
To validate a PDF against a standard profile:
require "preflight" preflight = Preflight::Profiles::PDFX1A.new results = preflight.check("somefile.pdf") # Or using a file handle File.open("somefile.pdf", "rb") do |file| results = preflight.check(file) end
Create a custom set of validation rules:
require "preflight" class MyPreflight include Preflight::Profile profile_name "custom-pdf-check" rule Preflight::Rules::MaxVersion, 1.4 rule Preflight::Rules::NoEncryption rule Preflight::Rules::DocumentId # Advanced color space validation rule Preflight::Rules::ImageColorspace, [:DeviceRGB, :DeviceCMYK], blacklist: ['Adobe RGB (1998)'] end preflight = MyPreflight.new results = preflight.check("somefile.pdf")
The ImageColorspace rule provides comprehensive color space validation:
-
Supports common color spaces (DeviceRGB, DeviceCMYK, DeviceGray)
-
ICC profile detection and validation
-
Blacklist/whitelist specific ICC profiles
-
Handles indexed color spaces
-
Alpha channel detection
-
Metadata extraction
Example usage:
# Allow only CMYK and Gray, block specific ICC profiles rule Preflight::Rules::ImageColorspace, [:DeviceCMYK, :DeviceGray], blacklist: ['Adobe RGB (1998)', 'sRGB']
Build upon existing rule sets:
class MyPreflight include Preflight::Profile profile_name "extended-pdfx1a" import Preflight::Profiles::PDFX1A # Add additional rules rule Preflight::Rules::MaxVersion, 1.4 rule Preflight::Rules::ImageColorspace, [:DeviceCMYK] end
Add rules to a profile instance at runtime:
preflight = Preflight::Profiles::PDFX1A.new preflight.rule Preflight::Rules::MaxVersion, 1.4 results = preflight.check("somefile.pdf")
All rules are in the Preflight::Rules namespace. Key rules include:
-
ImageColorspace - Validates image color spaces and ICC profiles
-
MaxVersion - Ensures PDF version compliance
-
NoEncryption - Checks for encrypted content
-
DocumentId - Validates document identification
-
InfoHasKeys - Verifies PDF metadata
Custom rules can be created by implementing the appropriate rule interface.
-
Ruby 3.0 or newer
-
PDF::Reader gem
-
RSpec for testing
-
Clone the repository
-
Run ‘bundle install`
Run the full test suite:
bundle exec rake
Run specific tests:
bundle exec rspec spec/path/to/spec.rb
-
Fork the repository
-
Create your feature branch (‘git checkout -b feature/my-new-feature`)
-
Commit your changes (‘git commit -am ’Add some feature’‘)
-
Push to the branch (‘git push origin feature/my-new-feature`)
-
Create a Pull Request
MIT License - Copyright © James Healy