Class: PdflibWrapper::Pdf
- Inherits:
-
Object
- Object
- PdflibWrapper::Pdf
- Defined in:
- lib/pdflib_wrapper/pdf.rb
Instance Attribute Summary collapse
-
#path ⇒ Object
Returns the value of attribute path.
-
#pdf ⇒ Object
Returns the value of attribute pdf.
Class Method Summary collapse
Instance Method Summary collapse
- #create_font(font, encoding, opts = {}) ⇒ Object
- #embed_pdf_page(page, x, y, opts = {}) ⇒ Object
-
#initialize(filepath, metadata = {}, opts = {}) ⇒ Pdf
constructor
A new instance of Pdf.
- #new_page(width, height, opts = {}) ⇒ Object
- #open_pdf(filepath, opts = {}) ⇒ Object
- #open_pdf_page(page_number = 1, pdf = nil, opts = {}) ⇒ Object
- #page_count ⇒ Object
- #print(text) ⇒ Object
- #print_on_newline(text) ⇒ Object
- #save ⇒ Object (also: #close)
-
#set_text(font, size) ⇒ Object
TODO: pull font and printing text into new class.
- #set_text_and_position(font, size, width, height) ⇒ Object
- #set_text_position(width, height) ⇒ Object
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.('', 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
#path ⇒ Object
Returns the value of attribute path.
3 4 5 |
# File 'lib/pdflib_wrapper/pdf.rb', line 3 def path @path end |
#pdf ⇒ Object
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 (page, x, y, opts={}) Page.(@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_count ⇒ Object
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 |
#print(text) ⇒ Object
59 60 61 |
# File 'lib/pdflib_wrapper/pdf.rb', line 59 def print(text) @pdf.show(text) end |
#print_on_newline(text) ⇒ Object
63 64 65 |
# File 'lib/pdflib_wrapper/pdf.rb', line 63 def print_on_newline(text) @pdf.continue_text(text) end |
#save ⇒ Object 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 |