Skip to content

Instantly share code, notes, and snippets.

@alex-kovshovik
Created April 19, 2013 18:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alex-kovshovik/5422059 to your computer and use it in GitHub Desktop.
Save alex-kovshovik/5422059 to your computer and use it in GitHub Desktop.
This works to concatenate PDFs
# This class uses the pdftk toolkit.
class PdfToolkit
# Merges some pdf files to output file
#
# options
# :from_files - array of source pdf files
# :to_file - output file
#
def self.merge(options)
command = "\"#{options[:from_files].join('" "')}\" cat output \"#{options[:to_file]}\""
run(command)
end
# Fills pdf data in pdf form and creates output file
#
# options
# :form_file - pattern file
# :data_file - file with form data
# :to_file - output file
# :flatten - if true then document will be made readonly after filling
def self.fill_form(options)
command = "\"#{options[:form_file]}\" fill_form \"#{options[:data_file]}\" output \"#{options[:to_file]}\" #{"flatten" if options[:flatten]}"
run(command)
end
private
# Runs pdftk util.
def self.run(command)
full_command = "pdftk #{command}"
output = %x[ #{full_command} ]
return_status = $?
if return_status.nil? or return_status.success? != true
raise "Running PDFTK tool failed with: #{full_command} / #{output}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment