Class: RustPdf::EditableDoc
- Inherits:
-
Object
- Object
- RustPdf::EditableDoc
- Defined in:
- lib/rustpdf/editable_doc.rb
Overview
An existing PDF loaded for manipulation. Call #close (or rely on GC) to free.
Class Method Summary collapse
-
.load(data, password: nil) ⇒ Object
Load a PDF from bytes (optionally with a password).
- .load_file(path, password: nil) ⇒ Object
Instance Method Summary collapse
- #close ⇒ Object
- #compact(on = true) ⇒ Object
-
#convert_to_pdfa(level = Pdfa::A2B) ⇒ Object
Convert the document to PDF/A (B-levels only: A1B=0, A2B=1, A3B=3).
- #delete_page(index) ⇒ Object
-
#encrypt(method: Cipher::AES256, user: "", owner: "", read_only: false) ⇒ Object
Encrypt on save (requires a license).
- #extract_pages(indices) ⇒ Object
-
#field_names ⇒ Object
Returns the list of AcroForm field names.
-
#fill_text_field(name, value) ⇒ Object
Returns whether the field existed.
-
#flatten_forms ⇒ Object
Flatten all AcroForm fields into page content (removes interactivity).
- #get_info(key) ⇒ Object
- #info=(pair) ⇒ Object
-
#initialize(ptr) ⇒ EditableDoc
constructor
A new instance of EditableDoc.
- #merge(other) ⇒ Object
- #optimize ⇒ Object
- #overlay_page(index, content) ⇒ Object
- #page_count ⇒ Object
-
#redact(page_index, rects) ⇒ Object
Black out rectangles on a page.
- #reorder_pages(order) ⇒ Object
- #rotate_page(index, degrees) ⇒ Object
- #save(path) ⇒ Object
-
#set_checkbox(name, checked = true) ⇒ Object
Set a checkbox by field name.
-
#set_choice(name, value) ⇒ Object
Set a choice (dropdown/list) value by field name.
- #set_info(key, value) ⇒ Object
-
#set_radio(name, export_value) ⇒ Object
Select a radio button by field name + export value.
- #set_xmp(xml) ⇒ Object
- #to_bytes ⇒ Object
- #to_bytes_incremental(original) ⇒ Object
-
#watermark_image_file(path, width, height, opacity: 0.30) ⇒ Object
Stamp an image watermark (from a file) across every page.
-
#watermark_text(text, size: 64.0, color: [0.5, 0.5, 0.5], opacity: 0.30, rotation_deg: 45.0) ⇒ Object
Stamp a diagonal text watermark across every page.
Constructor Details
#initialize(ptr) ⇒ EditableDoc
Returns a new instance of EditableDoc.
20 21 22 |
# File 'lib/rustpdf/editable_doc.rb', line 20 def initialize(ptr) @ptr = ptr end |
Class Method Details
.load(data, password: nil) ⇒ Object
Load a PDF from bytes (optionally with a password).
5 6 7 8 9 10 11 12 13 14 |
# File 'lib/rustpdf/editable_doc.rb', line 5 def self.load(data, password: nil) ptr = if password Native.call("pdf_editable_load_password", data, data.bytesize, password) else Native.call("pdf_editable_load", data, data.bytesize) end raise Error, RustPdf.last_error if ptr.null? new(ptr) end |
.load_file(path, password: nil) ⇒ Object
16 17 18 |
# File 'lib/rustpdf/editable_doc.rb', line 16 def self.load_file(path, password: nil) load(File.binread(path), password: password) end |
Instance Method Details
#close ⇒ Object
25 26 27 28 29 30 |
# File 'lib/rustpdf/editable_doc.rb', line 25 def close return if @ptr.nil? || @ptr.null? Native.call("pdf_editable_free", @ptr) @ptr = nil end |
#compact(on = true) ⇒ Object
164 165 166 167 |
# File 'lib/rustpdf/editable_doc.rb', line 164 def compact(on = true) RustPdf.check(Native.call("pdf_editable_compact", ptr, on ? 1 : 0)) self end |
#convert_to_pdfa(level = Pdfa::A2B) ⇒ Object
Convert the document to PDF/A (B-levels only: A1B=0, A2B=1, A3B=3).
154 155 156 157 |
# File 'lib/rustpdf/editable_doc.rb', line 154 def convert_to_pdfa(level = Pdfa::A2B) RustPdf.check(Native.call("pdf_editable_convert_to_pdfa", ptr, level)) self end |
#delete_page(index) ⇒ Object
46 47 48 49 |
# File 'lib/rustpdf/editable_doc.rb', line 46 def delete_page(index) RustPdf.check(Native.call("pdf_editable_delete_page", ptr, index)) self end |
#encrypt(method: Cipher::AES256, user: "", owner: "", read_only: false) ⇒ Object
Encrypt on save (requires a license).
170 171 172 173 |
# File 'lib/rustpdf/editable_doc.rb', line 170 def encrypt(method: Cipher::AES256, user: "", owner: "", read_only: false) RustPdf.check(Native.call("pdf_editable_encrypt", ptr, method, user, owner, read_only ? 1 : 0)) self end |
#extract_pages(indices) ⇒ Object
57 58 59 60 61 62 |
# File 'lib/rustpdf/editable_doc.rb', line 57 def extract_pages(indices) buf = indices.pack("J*") out = Fiddle::Pointer.malloc(Native::SIZEOF_SZ, Fiddle::RUBY_FREE) RustPdf.check(Native.call("pdf_editable_extract_pages", ptr, buf, indices.size, out)) EditableDoc.send(:new, out.ptr) end |
#field_names ⇒ Object
Returns the list of AcroForm field names.
123 124 125 126 127 128 |
# File 'lib/rustpdf/editable_doc.rb', line 123 def field_names p = ptr text = RustPdf.take_bytes { |pp, pn| Native.call("pdf_editable_field_names", p, pp, pn) } .force_encoding(Encoding::UTF_8) text.split("\n").reject(&:empty?) end |
#fill_text_field(name, value) ⇒ Object
Returns whether the field existed.
91 92 93 94 |
# File 'lib/rustpdf/editable_doc.rb', line 91 def fill_text_field(name, value) found = RustPdf.out_int { |buf| Native.call("pdf_editable_fill_text_field", ptr, name, value, buf) } found != 0 end |
#flatten_forms ⇒ Object
Flatten all AcroForm fields into page content (removes interactivity).
117 118 119 120 |
# File 'lib/rustpdf/editable_doc.rb', line 117 def flatten_forms RustPdf.check(Native.call("pdf_editable_flatten_forms", ptr)) self end |
#get_info(key) ⇒ Object
74 75 76 77 78 |
# File 'lib/rustpdf/editable_doc.rb', line 74 def get_info(key) p = ptr RustPdf.take_bytes { |pp, pn| Native.call("pdf_editable_get_info", p, key, pp, pn) } .force_encoding(Encoding::UTF_8) end |
#info=(pair) ⇒ Object
64 65 66 67 |
# File 'lib/rustpdf/editable_doc.rb', line 64 def info=(pair) key, value = pair RustPdf.check(Native.call("pdf_editable_set_info", ptr, key, value)) end |
#merge(other) ⇒ Object
36 37 38 39 |
# File 'lib/rustpdf/editable_doc.rb', line 36 def merge(other) RustPdf.check(Native.call("pdf_editable_merge", ptr, other.send(:ptr))) self end |
#optimize ⇒ Object
159 160 161 162 |
# File 'lib/rustpdf/editable_doc.rb', line 159 def optimize RustPdf.check(Native.call("pdf_editable_optimize", ptr)) self end |
#overlay_page(index, content) ⇒ Object
85 86 87 88 |
# File 'lib/rustpdf/editable_doc.rb', line 85 def (index, content) RustPdf.check(Native.call("pdf_editable_overlay_page", ptr, index, content, content.bytesize)) self end |
#page_count ⇒ Object
32 33 34 |
# File 'lib/rustpdf/editable_doc.rb', line 32 def page_count Native.call("pdf_editable_page_count", ptr) end |
#redact(page_index, rects) ⇒ Object
Black out rectangles on a page. rects = [[x0,y0,x1,y1], ...]. Returns whether the page existed.
147 148 149 150 151 |
# File 'lib/rustpdf/editable_doc.rb', line 147 def redact(page_index, rects) flat = rects.flatten.pack("d*") found = RustPdf.out_int { |buf| Native.call("pdf_editable_redact", ptr, page_index, flat, rects.size, buf) } found != 0 end |
#reorder_pages(order) ⇒ Object
51 52 53 54 55 |
# File 'lib/rustpdf/editable_doc.rb', line 51 def reorder_pages(order) buf = order.pack("J*") RustPdf.check(Native.call("pdf_editable_reorder_pages", ptr, buf, order.size)) self end |
#rotate_page(index, degrees) ⇒ Object
41 42 43 44 |
# File 'lib/rustpdf/editable_doc.rb', line 41 def rotate_page(index, degrees) RustPdf.check(Native.call("pdf_editable_rotate_page", ptr, index, degrees)) self end |
#save(path) ⇒ Object
187 188 189 |
# File 'lib/rustpdf/editable_doc.rb', line 187 def save(path) RustPdf.check(Native.call("pdf_editable_save", ptr, path)) end |
#set_checkbox(name, checked = true) ⇒ Object
Set a checkbox by field name. Returns whether the field existed.
99 100 101 102 |
# File 'lib/rustpdf/editable_doc.rb', line 99 def set_checkbox(name, checked = true) found = RustPdf.out_int { |buf| Native.call("pdf_editable_set_checkbox", ptr, name, checked ? 1 : 0, buf) } found != 0 end |
#set_choice(name, value) ⇒ Object
Set a choice (dropdown/list) value by field name. Returns whether found.
111 112 113 114 |
# File 'lib/rustpdf/editable_doc.rb', line 111 def set_choice(name, value) found = RustPdf.out_int { |buf| Native.call("pdf_editable_set_choice", ptr, name, value, buf) } found != 0 end |
#set_info(key, value) ⇒ Object
69 70 71 72 |
# File 'lib/rustpdf/editable_doc.rb', line 69 def set_info(key, value) RustPdf.check(Native.call("pdf_editable_set_info", ptr, key, value)) self end |
#set_radio(name, export_value) ⇒ Object
Select a radio button by field name + export value. Returns whether found.
105 106 107 108 |
# File 'lib/rustpdf/editable_doc.rb', line 105 def set_radio(name, export_value) found = RustPdf.out_int { |buf| Native.call("pdf_editable_set_radio", ptr, name, export_value, buf) } found != 0 end |
#set_xmp(xml) ⇒ Object
80 81 82 83 |
# File 'lib/rustpdf/editable_doc.rb', line 80 def set_xmp(xml) RustPdf.check(Native.call("pdf_editable_set_xmp", ptr, xml, xml.bytesize)) self end |
#to_bytes ⇒ Object
175 176 177 178 |
# File 'lib/rustpdf/editable_doc.rb', line 175 def to_bytes p = ptr RustPdf.take_bytes { |pp, pn| Native.call("pdf_editable_to_bytes", p, pp, pn) } end |
#to_bytes_incremental(original) ⇒ Object
180 181 182 183 184 185 |
# File 'lib/rustpdf/editable_doc.rb', line 180 def to_bytes_incremental(original) p = ptr RustPdf.take_bytes do |pp, pn| Native.call("pdf_editable_to_bytes_incremental", p, original, original.bytesize, pp, pn) end end |
#watermark_image_file(path, width, height, opacity: 0.30) ⇒ Object
Stamp an image watermark (from a file) across every page.
138 139 140 141 |
# File 'lib/rustpdf/editable_doc.rb', line 138 def watermark_image_file(path, width, height, opacity: 0.30) RustPdf.check(Native.call("pdf_editable_watermark_image_file", ptr, path, width, height, opacity)) self end |
#watermark_text(text, size: 64.0, color: [0.5, 0.5, 0.5], opacity: 0.30, rotation_deg: 45.0) ⇒ Object
Stamp a diagonal text watermark across every page.
131 132 133 134 135 |
# File 'lib/rustpdf/editable_doc.rb', line 131 def watermark_text(text, size: 64.0, color: [0.5, 0.5, 0.5], opacity: 0.30, rotation_deg: 45.0) r, g, b = color RustPdf.check(Native.call("pdf_editable_watermark_text", ptr, text, size, r, g, b, opacity, rotation_deg)) self end |