Wednesday, September 24, 2014

Changing PDF Meta Data in Ruby

According to the home page of pdf-toolkit gem :

pdf-toolkit allows you to access pdf metadata in read-write in a very simple way, through the pdftk commandline tool.

1. Install pdftk version 2.02

$ pdftk -version

pdftk 2.02 a Handy Tool for Manipulating PDF Documents
Copyright (c) 2003-13 Steward and Lee, LLC - Please Visit: www.pdftk.com
This is free software; see the source code for copying conditions. There is
NO warranty, not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

2. gem install pdf-toolkit

3. Read or Write pdf meta data.

require 'pdf/toolkit'

pdf = PDF::Toolkit.open('tmux_p3_0.pdf')
#puts pdf.public_methods(false).sort

puts pdf.author
pdf.author='Daffy Duck'
if pdf.save
  'PDF meta-data saved successfully'
else
  'Failed to save pdf meta-data'
end
# save! to raise exception on failure

puts "after change"
puts "Author"
puts pdf.author
puts 'Title'
puts pdf.title
puts 'Subject'
puts pdf.subject
puts 'Page count'
puts pdf.page_count
puts 'Creator (Publisher)'
puts pdf.creator
puts 'Created At'
puts pdf.created_at
puts 'keywords'
puts pdf.keywords
puts 'producer'
puts pdf.producer
puts 'version'
puts pdf.version
puts 'Updated at'
puts pdf.updated_at

4. Verify the meta-data in Preview -> Tools -> Show Inspector. You will see the author, title, subject, producer, creator, creation date, updated date, number of pages etc.

5. Read the tests in the pdf-toolkit source code to see the available methods.