Class: RustPdf::Document

Inherits:
Object
  • Object
show all
Defined in:
lib/rustpdf/document.rb

Overview

A PDF document being authored. Call #close (or rely on GC) to free.

Instance Method Summary collapse

Constructor Details

#initializeDocument

Returns a new instance of Document.

Raises:



4
5
6
7
# File 'lib/rustpdf/document.rb', line 4

def initialize
  @ptr = Native.call("pdf_document_new")
  raise Error, "pdf_document_new returned NULL" if @ptr.null?
end

Instance Method Details

#add_bookmark(bookmark) ⇒ Object

Append one outline tree (a RustPdf::Bookmark). Pre-order flattened into parallel arrays and emitted in a single native call.



180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/rustpdf/document.rb', line 180

def add_bookmark(bookmark)
  entries = bookmark.flatten_into(0, [])
  n = entries.size
  levels = entries.map { |e| e[0] }.pack("i!*")
  pages  = entries.map { |e| e[2] }.pack("J*")
  tops   = entries.map { |e| e[3] || 0.0 }.pack("d*")
  has    = entries.map { |e| e[3].nil? ? 0 : 1 }.pack("i!*")
  cstrs  = entries.map { |e| Fiddle::Pointer[e[1].to_s] }
  titles = cstrs.map(&:to_i).pack("J*")
  RustPdf.check(Native.call("pdf_document_add_bookmarks", ptr, n, levels, titles, pages, tops, has))
  cstrs.clear # released after the call returned
  self
end

#add_font(data) ⇒ Object



90
91
92
# File 'lib/rustpdf/document.rb', line 90

def add_font(data)
  RustPdf.out_int { |buf| Native.call("pdf_document_add_font", ptr, data, data.bytesize, buf) }
end

#add_font_file(path) ⇒ Object

---- fonts + text -------------------------------------------------------



86
87
88
# File 'lib/rustpdf/document.rb', line 86

def add_font_file(path)
  RustPdf.out_int { |buf| Native.call("pdf_document_add_font_file", ptr, path, buf) }
end

#add_image_file(path) ⇒ Object

---- images -------------------------------------------------------------



106
107
108
# File 'lib/rustpdf/document.rb', line 106

def add_image_file(path)
  RustPdf.out_int { |buf| Native.call("pdf_document_add_image_file", ptr, path, buf) }
end

#add_image_jpeg(data) ⇒ Object



114
115
116
# File 'lib/rustpdf/document.rb', line 114

def add_image_jpeg(data)
  RustPdf.out_int { |buf| Native.call("pdf_document_add_image_jpeg", ptr, data, data.bytesize, buf) }
end

#add_image_png(data) ⇒ Object



110
111
112
# File 'lib/rustpdf/document.rb', line 110

def add_image_png(data)
  RustPdf.out_int { |buf| Native.call("pdf_document_add_image_png", ptr, data, data.bytesize, buf) }
end

#add_page(width: nil, height: nil) ⇒ Object

---- pages + graphics ---------------------------------------------------



46
47
48
49
50
51
52
# File 'lib/rustpdf/document.rb', line 46

def add_page(width: nil, height: nil)
  RustPdf.check(
    width && height ? Native.call("pdf_document_add_page_sized", ptr, width, height)
                    : Native.call("pdf_document_add_page", ptr)
  )
  self
end

#attach_file(name, mime, data, relationship: Relationship::SOURCE, description: "") ⇒ Object

---- attachments + forms ------------------------------------------------



130
131
132
133
# File 'lib/rustpdf/document.rb', line 130

def attach_file(name, mime, data, relationship: Relationship::SOURCE, description: "")
  RustPdf.check(Native.call("pdf_document_attach_file", ptr, name, mime, data, data.bytesize, relationship, description))
  self
end

#checkbox(name, page, rect, checked) ⇒ Object



141
142
143
144
# File 'lib/rustpdf/document.rb', line 141

def checkbox(name, page, rect, checked)
  RustPdf.check(Native.call("pdf_document_checkbox", ptr, name, page, rect[0], rect[1], rect[2], rect[3], checked ? 1 : 0))
  self
end

#closeObject



9
10
11
12
13
14
# File 'lib/rustpdf/document.rb', line 9

def close
  return if @ptr.nil? || @ptr.null?

  Native.call("pdf_document_free", @ptr)
  @ptr = nil
end

#default_size(width, height) ⇒ Object



34
35
36
37
# File 'lib/rustpdf/document.rb', line 34

def default_size(width, height)
  RustPdf.check(Native.call("pdf_document_set_default_size", ptr, width, height))
  self
end

#draw_image(image, x, y, w, h) ⇒ Object



118
119
120
121
# File 'lib/rustpdf/document.rb', line 118

def draw_image(image, x, y, w, h)
  RustPdf.check(Native.call("pdf_page_draw_image", ptr, image, x, y, w, h))
  self
end

options: Array; selected: index or nil



147
148
149
150
151
# File 'lib/rustpdf/document.rb', line 147

def dropdown(name, page, rect, options, selected: nil, size: 0.0)
  RustPdf.check(Native.call("pdf_document_dropdown", ptr, name, page, rect[0], rect[1], rect[2], rect[3],
                            options.join("\n"), selected || -1, size))
  self
end

#facturx(xml, profile: FacturxProfile::EN16931) ⇒ Object

---- ZUGFeRD / Factur-X (Tier 2) ----------------------------------------



196
197
198
199
# File 'lib/rustpdf/document.rb', line 196

def facturx(xml, profile: FacturxProfile::EN16931)
  RustPdf.check(Native.call("pdf_document_facturx", ptr, xml, xml.bytesize, profile))
  self
end

#figure(image, x, y, w, h, alt) ⇒ Object



123
124
125
126
# File 'lib/rustpdf/document.rb', line 123

def figure(image, x, y, w, h, alt)
  RustPdf.check(Native.call("pdf_page_figure", ptr, image, x, y, w, h, alt))
  self
end

#fillObject



74
75
76
77
# File 'lib/rustpdf/document.rb', line 74

def fill
  RustPdf.check(Native.call("pdf_page_fill", ptr))
  self
end

#fill_rgb(r, g, b) ⇒ Object



54
55
56
57
# File 'lib/rustpdf/document.rb', line 54

def fill_rgb(r, g, b)
  RustPdf.check(Native.call("pdf_page_set_fill_rgb", ptr, r, g, b))
  self
end

#info(title: nil, author: nil, subject: nil, keywords: nil, creator: nil) ⇒ Object



39
40
41
42
# File 'lib/rustpdf/document.rb', line 39

def info(title: nil, author: nil, subject: nil, keywords: nil, creator: nil)
  RustPdf.check(Native.call("pdf_document_set_info", ptr, title, author, subject, keywords, creator))
  self
end

#line_width(w) ⇒ Object



64
65
66
67
# File 'lib/rustpdf/document.rb', line 64

def line_width(w)
  RustPdf.check(Native.call("pdf_page_set_line_width", ptr, w))
  self
end

rect = [x0, y0, x1, y1]; link to another page (optional top y-offset).



172
173
174
175
176
# File 'lib/rustpdf/document.rb', line 172

def link_to_page(rect, page_index, top: nil)
  RustPdf.check(Native.call("pdf_page_link_to_page", ptr, rect[0], rect[1], rect[2], rect[3],
                            page_index, top || 0.0, top.nil? ? 0 : 1))
  self
end

rect = [x0, y0, x1, y1]; link to an external URI.



166
167
168
169
# File 'lib/rustpdf/document.rb', line 166

def link_uri(rect, uri)
  RustPdf.check(Native.call("pdf_page_link_uri", ptr, rect[0], rect[1], rect[2], rect[3], uri))
  self
end

#page_countObject

---- output -------------------------------------------------------------



203
204
205
# File 'lib/rustpdf/document.rb', line 203

def page_count
  Native.call("pdf_document_page_count", ptr)
end

#paragraph(font, size, x, y, width, text, align: Align::LEFT) ⇒ Object



99
100
101
102
# File 'lib/rustpdf/document.rb', line 99

def paragraph(font, size, x, y, width, text, align: Align::LEFT)
  RustPdf.check(Native.call("pdf_page_paragraph", ptr, font, size, x, y, width, align, text))
  self
end

#pdfa(level = nil) ⇒ Object

---- configuration ------------------------------------------------------



18
19
20
21
22
23
# File 'lib/rustpdf/document.rb', line 18

def pdfa(level = nil)
  RustPdf.check(
    level ? Native.call("pdf_document_pdfa_level", ptr, level) : Native.call("pdf_document_pdfa", ptr)
  )
  self
end

#radio_group(name, page, buttons, selected: nil) ⇒ Object

buttons: Array<[ [x0,y0,x1,y1], "export" ]>; selected: index or nil



154
155
156
157
158
159
160
161
# File 'lib/rustpdf/document.rb', line 154

def radio_group(name, page, buttons, selected: nil)
  rects = buttons.flat_map { |(r, _)| r }.pack("d*")
  cstrs = buttons.map { |(_, e)| Fiddle::Pointer[e.to_s] }
  addrs = cstrs.map(&:to_i).pack("J*")
  RustPdf.check(Native.call("pdf_document_radio_group", ptr, name, page, buttons.size, rects, addrs, selected || -1))
  cstrs.clear # released after the call returned
  self
end

#rect(x, y, w, h) ⇒ Object



69
70
71
72
# File 'lib/rustpdf/document.rb', line 69

def rect(x, y, w, h)
  RustPdf.check(Native.call("pdf_page_rect", ptr, x, y, w, h))
  self
end

#save(path) ⇒ Object



212
213
214
# File 'lib/rustpdf/document.rb', line 212

def save(path)
  RustPdf.check(Native.call("pdf_document_save", ptr, path))
end

#show_text(font, size, x, y, text, heading_level: 0) ⇒ Object



94
95
96
97
# File 'lib/rustpdf/document.rb', line 94

def show_text(font, size, x, y, text, heading_level: 0)
  RustPdf.check(Native.call("pdf_page_show_text", ptr, font, size, x, y, text, heading_level))
  self
end

#strokeObject



79
80
81
82
# File 'lib/rustpdf/document.rb', line 79

def stroke
  RustPdf.check(Native.call("pdf_page_stroke", ptr))
  self
end

#stroke_rgb(r, g, b) ⇒ Object



59
60
61
62
# File 'lib/rustpdf/document.rb', line 59

def stroke_rgb(r, g, b)
  RustPdf.check(Native.call("pdf_page_set_stroke_rgb", ptr, r, g, b))
  self
end

#taggedObject



25
26
27
28
# File 'lib/rustpdf/document.rb', line 25

def tagged
  RustPdf.check(Native.call("pdf_document_tagged", ptr))
  self
end

#text_field(name, page, rect, value: "", size: 0.0) ⇒ Object

rect = [x0, y0, x1, y1]



136
137
138
139
# File 'lib/rustpdf/document.rb', line 136

def text_field(name, page, rect, value: "", size: 0.0)
  RustPdf.check(Native.call("pdf_document_text_field", ptr, name, page, rect[0], rect[1], rect[2], rect[3], value, size))
  self
end

#to_bytesObject



207
208
209
210
# File 'lib/rustpdf/document.rb', line 207

def to_bytes
  p = ptr
  RustPdf.take_bytes { |pp, pn| Native.call("pdf_document_write", p, pp, pn) }
end

#version=(v) ⇒ Object



30
31
32
# File 'lib/rustpdf/document.rb', line 30

def version=(v)
  RustPdf.check(Native.call("pdf_document_set_version", ptr, v))
end