Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow for override of PDF metadata. #206

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/combine_pdf.rb
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
load 'combine_pdf/fonts.rb'
load 'combine_pdf/filter.rb'
load 'combine_pdf/parser.rb'
load 'combine_pdf/pdf_info.rb'
load 'combine_pdf/pdf_public.rb'
load 'combine_pdf/pdf_protected.rb'
load 'combine_pdf/exceptions.rb'
Expand Down
88 changes: 88 additions & 0 deletions lib/combine_pdf/pdf_info.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
module CombinePDF
class PDFInfo

# if we're passed a date object, return a formatted string
# otherwise just use the string we've been passed
def self.format_date(new_date)
return new_date.strftime "D:%Y%m%d%H%M%S%:::z'00" if new_date.respond_to?(:strftime)
new_date
end
end

class PDF

def title
self.info[:Title]
end

def title=(new_title = nil)
self.info[:Title] = new_title
end

def author
self.info[:Author]
end

def author=(new_author = nil)
self.info[:Author] = new_author
end

def subject
self.info[:Subject]
end

def subject=(new_subject = nil)
self.info[:Subject] = new_subject
end

def keywords
self.info[:Keywords]
end

def keywords=(new_keywords = nil)
self.info[:KeyWords] = new_keywords
end

def creator
self.info[:Creator]
end

def creator=(new_creator = nil)
self.info[:Creator] = new_creator
end

def producer
self.info[:Producer]
end

def producer=(new_producer = nil)
self.info[:Producer] = new_producer
end

def creation_date
self.info[:CreationDate]
end

def creation_date=(new_creation_date = nil)
self.info[:CreationDate] = PDFInfo.format_date(new_creation_date)
end

def mod_date
self.info[:ModDate]
end

def mod_date=(new_mod_date = nil)
self.info[:ModDate] = PDFInfo.format_date(new_mod_date)
end

def trapped
self.info[:Trapped]
end

def trapped=(new_trapped = nil)
self.info[:Trapped] = new_trapped
end

end

end
42 changes: 11 additions & 31 deletions lib/combine_pdf/pdf_public.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ def initialize(parser = nil)

# general globals
@set_start_id = 1
@info[:Producer] = "Ruby CombinePDF #{CombinePDF::VERSION} Library"
producer = "Ruby CombinePDF #{CombinePDF::VERSION} Library"
@info.delete :CreationDate
@info.delete :ModDate
end
Expand All @@ -125,33 +125,6 @@ def new_page(mediabox = [0, 0, 612.0, 792.0], _location = -1)
p
end

# get the title for the pdf
# The title is stored in the information dictionary and isn't required
def title
@info[:Title]
end

# set the title for the pdf
# The title is stored in the information dictionary and isn't required
# new_title:: a string that is the new author value.
def title=(new_title = nil)
@info[:Title] = new_title
end

# get the author value for the pdf.
# The author is stored in the information dictionary and isn't required
def author
@info[:Author]
end

# set the author value for the pdf.
# The author is stored in the information dictionary and isn't required
#
# new_title:: a string that is the new author value.
def author=(new_author = nil)
@info[:Author] = new_author
end

# Clears any existing form data.
def clear_forms_data
@forms_data.nil? || @forms_data.clear
Expand All @@ -174,10 +147,17 @@ def save(file_name, options = {})
def to_pdf(options = {})
# reset version if not specified
@version = 1.5 if @version.to_f == 0.0

# set info for merged file
@info[:ModDate] = @info[:CreationDate] = Time.now.strftime "D:%Y%m%d%H%M%S%:::z'00"
@info[:Subject] = options[:subject] if options[:subject]
@info[:Producer] = options[:producer] if options[:producer]
mod_date = Time.now

# set creation date to mod date if it doesn't already exist
creation_date ||= mod_date

# legacy behavior... assign subject and producer to metadata
subject = options[:subject] if options[:subject]
producer = options[:producer] if options[:producer]

# rebuild_catalog
catalog = rebuild_catalog_and_objects
# add ID and generation numbers to objects
Expand Down