Class: PdfDocument

Inherits:
Object
  • Object
show all
Defined in:
lib/majorsilence_pdf/pdf_native.rb

Instance Method Summary collapse

Constructor Details

#initialize(fns) ⇒ PdfDocument

Returns a new instance of PdfDocument.



153
154
155
156
157
158
159
# File 'lib/majorsilence_pdf/pdf_native.rb', line 153

def initialize(fns)
  @fns = fns
  h = fns[:pdf_doc_create].call
  raise "pdf_doc_create failed: #{last_error}" if h.nil? || h.zero?

  @handle = h
end

Instance Method Details

#add_page(width = 595.28, height = 841.89) ⇒ Object

Add a page and return a PdfCanvas for drawing. width / height in points. A4 = 595.28 x 841.89.



183
184
185
186
187
188
# File 'lib/majorsilence_pdf/pdf_native.rb', line 183

def add_page(width = 595.28, height = 841.89)
  h = @fns[:pdf_doc_add_page].call(@handle, width.to_f, height.to_f)
  raise "pdf_doc_add_page failed: #{last_error}" if h.nil? || h.zero?

  PdfCanvas.new(@fns, h)
end

#closeObject



208
209
210
211
212
213
# File 'lib/majorsilence_pdf/pdf_native.rb', line 208

def close
  return unless @handle

  @fns[:pdf_doc_close].call(@handle)
  @handle = nil
end

#save(path) ⇒ Object

Write the completed document to path.



191
192
193
# File 'lib/majorsilence_pdf/pdf_native.rb', line 191

def save(path)
  check(@fns[:pdf_doc_save_file].call(@handle, c_str(path)), 'pdf_doc_save_file')
end

#save_to_memoryObject

Write to a temp file and return the PDF bytes.



196
197
198
199
200
201
202
203
204
205
206
# File 'lib/majorsilence_pdf/pdf_native.rb', line 196

def save_to_memory
  tmp = Tempfile.new(['pdfnative', '.pdf'], binmode: true)
  tmp_path = tmp.path
  tmp.close
  begin
    save(tmp_path)
    File.binread(tmp_path)
  ensure
    File.delete(tmp_path) if File.exist?(tmp_path)
  end
end

#set_author(author) ⇒ Object



162
# File 'lib/majorsilence_pdf/pdf_native.rb', line 162

def set_author(author)  = check(@fns[:pdf_doc_set_author].call(@handle,  c_str(author)),  'pdf_doc_set_author')

#set_creator(creator) ⇒ Object



164
# File 'lib/majorsilence_pdf/pdf_native.rb', line 164

def set_creator(creator)= check(@fns[:pdf_doc_set_creator].call(@handle, c_str(creator)), 'pdf_doc_set_creator')

#set_security(user_password: '', owner_password: nil, permissions: PERM_ALL, aes256: true) ⇒ Object

Apply password-based AES encryption. user_password - password to open; empty string = no open password owner_password - full-control password; nil = same as user_password permissions - bitmask of PERM_* constants; -1 = allow all aes256 - true = AES-256 (default), false = AES-128



171
172
173
174
175
176
177
178
179
# File 'lib/majorsilence_pdf/pdf_native.rb', line 171

def set_security(user_password: '', owner_password: nil, permissions: PERM_ALL, aes256: true)
  enc_version = aes256 ? 0 : 1
  owner_ptr   = owner_password ? c_str(owner_password) : Fiddle::NULL
  check(
    @fns[:pdf_doc_set_security].call(@handle, c_str(user_password), owner_ptr, permissions, enc_version),
    'pdf_doc_set_security'
  )
  self
end

#set_subject(subject) ⇒ Object



163
# File 'lib/majorsilence_pdf/pdf_native.rb', line 163

def set_subject(subject)= check(@fns[:pdf_doc_set_subject].call(@handle, c_str(subject)), 'pdf_doc_set_subject')

#set_title(title) ⇒ Object



161
# File 'lib/majorsilence_pdf/pdf_native.rb', line 161

def set_title(title)    = check(@fns[:pdf_doc_set_title].call(@handle,   c_str(title)),   'pdf_doc_set_title')