Preflight
Check your PDF files meet the standard you require.
The full PDF spec is a beast, and there are any number of ways of producing a valid PDF. Receivers of PDF files may need to ensure the files they’re sent meet a particular set of requirements, be it PDF/X, PDF/A or just “images must be 300 dpi or greater”. These checks are called preflight.
There’s expensive software around that can preflight for you, but it’s often difficult to script and you know, expensive.
This may not check as comprehensively as the official preflight tools from Adobe and friends, but hopefully it’ll get you most of the way with less stress.
Unlike some preflight tools, this library doesn’t attempt to fix any issues. It only reports them. That may change one day, but for now you will need to fix any issues that are detected by using some other tool.
Installation
gem install preflight
Usage
Standard Profile ( PDF/X-1a )
To test a PDF file against a well known standard and rule set.
require "preflight"
preflight = Preflight::Profiles::PDFX1A.new
puts preflight.check("somefile.pdf").inspect
File.open("somefile.pdf", "rb") do |file|
puts preflight.check(file).inspect
end
Custom Profile
Create a custom set of rules to check against.
require "preflight"
class MyPreflight
include Preflight::Profile
profile_name "simple-pdf"
rule Preflight::Rules::MaxVersion, 1.4
rule Preflight::Rules::NoEncryption
rule Preflight::Rules::DocumentId
end
preflight = MyPreflight.new
puts preflight.check("somefile.pdf").inspect
Extend A Profile
Use an existing rule set as the base for a larger set of rules.
require "preflight"
class MyPreflight
include Preflight::Profile
profile_name "simple-pdf"
import Preflight::Profiles::PDFX1A
rule Preflight::Rules::MaxVersion, 1.4
rule Preflight::Rules::NoEncryption
rule Preflight::Rules::DocumentId
end
preflight = MyPreflight.new
puts preflight.check("somefile.pdf").inspect
Adding Rules to a Profile Instance
Dynamically add rules to a profile instance. Useful when the required set of rules depends on dynamic conditions like user input or database entries.
require "preflight"
preflight = Preflight::Profiles::PDFX1A.new
prefight.rule Preflight::Rules::MaxVersion, 1.4
puts preflight.check("somefile.pdf").inspect
Available Rules
All rules shipped with this gem are in the Preflight::Rules namespace. Explore the classes and related documentation in there to see what’s available.
If you need a rule that we don’t ship it is perfectly valid to build your own. A profile will accept any class that behaves appropriately. If you think your new rule may be of general interest please send me a pull request so I can include it in the next release.
I’m yet to document the API that a rule class must follow. There are two types of rule, each designed to check a different aspect of the target file
-
raw PDF objects and individual pages. At this stage your best option when
writing your own rule is to copy an existing rule as a starting point.
Status
This library is in an early stage of development. Use at your own risk.
Compatibility
This is pure ruby should run on most ruby VMs. I develop on MRI 1.9.2.