Class: PdflibWrapper::Pdf

Inherits:
Object
  • Object
show all
Defined in:
lib/pdflib_wrapper/pdf.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(filepath, metadata = {}, opts = {}) ⇒ Pdf

Returns a new instance of Pdf.



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/pdflib_wrapper/pdf.rb', line 4

def initialize(filepath, ={}, opts={})
  #pdflib likes to override the file, should do a check to see if file already exists
  #also pdflib like to make bad pdf if you dont close pdf after begin_document
  @path = filepath
  @pdf = PDFlib.new

  #options legacy, return, exception
  @pdf.set_parameter("errorpolicy", opts[:errorpolicy] || "exception")

  #TODO: better support for begin document options
  unless opts[:dont_create_document]
  key_values = [:masterpassword, :userpassword, :permissions, :moddate]
  @pdf.set_parameter("licensefile", opts[:license_path] ) if opts[:license_path] #TODO: check if file exists
  @pdf.begin_document(filepath.to_s, OptionListMapper.create_options('', key_values, [], opts)) 

  @pdf.set_info("Subject", [:subject]) if [:subject]
  @pdf.set_info("Title", [:title]) if [:title]
   @pdf.set_info("Creator", [:creator]) if [:creator]
   @pdf.set_info("Author", [:author]) if [:author]
   @pdf.set_info("Keywords", [:keywords].join(', ')) if [:keywords] && [:keywords].is_a?(Array)
   @pdf.set_info("Trapped", [:trapped]) if opts[:trapped] && [true, false].include?(opts[:trapped])

   #TODO: support more of the Global Options of pdflib
   
   @pdf.set_value("compress", opts[:compress] ) if opts[:compress] && opts[:compress].is_a?(Fixnum) #TODO: check range (1..9)

   @current_page = Page.new(@pdf, 1, 1).save if opts[:with_blank_page]
 end
end

Instance Attribute Details

#pathObject

Returns the value of attribute path.



3
4
5
# File 'lib/pdflib_wrapper/pdf.rb', line 3

def path
  @path
end

#pdfObject

Returns the value of attribute pdf.



3
4
5
# File 'lib/pdflib_wrapper/pdf.rb', line 3

def pdf
  @pdf
end

Class Method Details

.open_pdf(filepath, opts = {}) ⇒ Object



94
95
96
97
98
# File 'lib/pdflib_wrapper/pdf.rb', line 94

def open_pdf(filepath, opts={})
  pdf = new('',{},opts[:pdflib_opts] || {}).pdf
  opts.delete(:pdflib_opts) if opts[:pdflib_opts]
  External::Pdf::Document.new(pdf, filepath, opts)
end

Instance Method Details

#create_font(font, encoding, opts = {}) ⇒ Object



67
68
69
70
71
# File 'lib/pdflib_wrapper/pdf.rb', line 67

def create_font(font, encoding, opts={})
  #TODO: support opts
  #  ascender, autocidfont, autosubsetting, capheight, descender, dropcorewidths, embedding, encoding, fallbackfonts, fontname, initialsubset, keepfont, keepnative, linegap, metadata, optimizeinvisible, readfeatures, readkerning, readselectors, readshaping, replacementchar, skipembedding, skipposttable, subsetlimit, subsetminsize, subsetting, unicodemap, vertical, xheight
  @pdf.load_font(font, encoding, "" )
end

#embed_pdf_page(page, x, y, opts = {}) ⇒ Object



89
90
91
# File 'lib/pdflib_wrapper/pdf.rb', line 89

def embed_pdf_page(page, x, y, opts={})
  Page.embed(@pdf, page, x, y, opts)
end

#new_page(width, height, opts = {}) ⇒ Object



73
74
75
76
# File 'lib/pdflib_wrapper/pdf.rb', line 73

def new_page(width, height, opts={})
  #TODO: support opts
  @current_page = Page.new(@pdf, width, height, opts)
end

#open_pdf(filepath, opts = {}) ⇒ Object



78
79
80
81
# File 'lib/pdflib_wrapper/pdf.rb', line 78

def open_pdf(filepath, opts={})
  #TODO: support opts
  @current_pdf = External::Pdf::Document.new(@pdf, filepath, opts)
end

#open_pdf_page(page_number = 1, pdf = nil, opts = {}) ⇒ Object



83
84
85
86
87
# File 'lib/pdflib_wrapper/pdf.rb', line 83

def open_pdf_page(page_number=1, pdf=nil, opts={})
  #TODO: support opts
  current_pdf = pdf || @current_pdf
  descriptor_page = External::Pdf::Page.new(@pdf, current_pdf, page_number, "" )
end

#page_countObject



40
41
42
# File 'lib/pdflib_wrapper/pdf.rb', line 40

def page_count
  #@pdf.get_pdi_value("/Root/Pages/Count", pdf_handle, -1, 0).to_i
end


59
60
61
# File 'lib/pdflib_wrapper/pdf.rb', line 59

def print(text)
  @pdf.show(text)
end


63
64
65
# File 'lib/pdflib_wrapper/pdf.rb', line 63

def print_on_newline(text)
  @pdf.continue_text(text)
end

#saveObject Also known as: close



34
35
36
37
# File 'lib/pdflib_wrapper/pdf.rb', line 34

def save
  @pdf.end_document("")
  self
end

#set_text(font, size) ⇒ Object

TODO: pull font and printing text into new class



46
47
48
# File 'lib/pdflib_wrapper/pdf.rb', line 46

def set_text(font, size)
  @pdf.setfont(font, size)
end

#set_text_and_position(font, size, width, height) ⇒ Object



54
55
56
57
# File 'lib/pdflib_wrapper/pdf.rb', line 54

def set_text_and_position(font,size,width,height)
  set_text(font,size)
  set_text_position(width, height)
end

#set_text_position(width, height) ⇒ Object



50
51
52
# File 'lib/pdflib_wrapper/pdf.rb', line 50

def set_text_position(width, height)
  @pdf.set_text_pos(width, height)
end