File: nested_document.rb

package info (click to toggle)
origami-pdf 2.1.0-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,484 kB
  • sloc: ruby: 17,883; makefile: 8
file content (51 lines) | stat: -rwxr-xr-x 1,112 bytes parent folder | download | duplicates (3)
1
2
3
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/usr/bin/env ruby

begin
    require 'origami'
rescue LoadError
    $: << File.join(__dir__, '../../lib')
    require 'origami'
end
include Origami

require 'stringio'

OUTPUT_FILE = "#{File.basename(__FILE__, ".rb")}.pdf"
EMBEDDED_NAME = "#{('a'..'z').to_a.sample(8).join}.pdf"

#
# Creates the nested document.
# A simple document displaying a message box.
#
output_str = StringIO.new
PDF.write(output_str) do |pdf|
    pdf.onDocumentOpen Action::JavaScript "app.alert('Hello world!');" 
end

output_str.rewind

# The envelope document.
pdf = PDF.new.append_page

# Create an object stream to compress objects.
objstm = ObjectStream.new
objstm.Filter = :FlateDecode

pdf.insert(objstm)

objstm.insert pdf.attach_file(output_str, register: true, name: EMBEDDED_NAME)

# Compress the page tree.
objstm.insert(pdf.Catalog.Pages)
objstm.insert(pdf.pages.first)

# Compress the name tree.
objstm.insert(pdf.Catalog.Names)
objstm.insert(pdf.Catalog.Names.EmbeddedFiles)

# Compress the catalog.
objstm.insert(pdf.Catalog)

pdf.pages.first.onOpen Action::GoToE[EMBEDDED_NAME]

pdf.save(OUTPUT_FILE, noindent: true)