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
33
# 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")
	@pdf.set_parameter("licensefile", opts[:license_path] ) if opts[:license_path] #TODO: check if file exists

	#TODO: better support for begin document options
	unless opts[:dont_create_document]
 	key_values = [:masterpassword, :userpassword, :permissions, :moddate]

 	@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



99
100
101
102
103
# File 'lib/pdflib_wrapper/pdf.rb', line 99

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



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

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_image(image, x, y, opts = {}) ⇒ Object



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

def embed_image(image, x,y, opts={})
	Page.embed_image(@pdf, image, x,y, opts)
end

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



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

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

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



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

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

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



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

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



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

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



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

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


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

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


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

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

#saveObject Also known as: close



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

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

#set_text(font, size) ⇒ Object

TODO: pull font and printing text into new class



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

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

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



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

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



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

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