Origami – PDF library for Ruby

Origami is a ruby library for crating / editing and Managing PDF files. There is only less ruby PDF library which supports digital signature kind of feature in PDF.

Recently I have used Origami for signing a PDF document.

You can find the details of Origami library here.

Gem version of Origami library is also available in RubyGems

Shell
1
gem install origami

The full source is here.

Sample code to sign a PDF using Origami:

Ruby
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
require 'origami'
 
#method to execute a command using shell
def command?(command)
system("which #{ command} > /dev/null 2>&1")
end
#I used qpdf to check a PDF status like whether it is linearized or xref-streamed, because Origami has no support for those kind of PDFs
raise unless command? 'qpdf'
input = "sample.pdf"
 
mypdf = Origami::PDF.read input
 
#code to convert linearized PDF
if mypdf.is_linearized?
command = "qpdf #{input} --normalize-content=y #{intermediate}"
system("#{ command} > /dev/null ")
mypdf = Origami::PDF.read intermediate
end
 
#code to convert xrefstreamed PDF
if mypdf.revisions.last.xrefstm.nil? == false
command = "qpdf #{input} --ignore-xref-streams #{intermediate}"
system("#{ command} > /dev/null ")
mypdf = Origami::PDF.read intermediate
end
 
#load certificate file
certificate_raw = File.read "server.crt"
certificate = OpenSSL::X509::Certificate.new certificate_raw
#load key file
key_raw = File.read "server.key"
key = OpenSSL::PKey::RSA.new key_raw
#signature image
imageobject = Origami::Graphics::ImageXObject.from_image_file('logo.jpg', 'jpg')
imageobject.Width = 100
imageobject.Height = 100
 
myannot = Origami::Annotation::RichMedia.new
myannot.Rect = Origami::Rectangle[:llx => 120.0, :lly => 386.0, :urx => 190.0, :ury => 353.0]
 
sigannot = Origami::Annotation::Widget::Signature.new
sigannot.Rect = Origami::Rectangle[:llx => 89.0, :lly => 386.0, :urx => 190.0, :ury => 353.0]
sigannot.Contents = "Hello World"
sigannot.Border = [ 1 , 1 , 1 ]
#add the signature page
page = Origami::Page.new
mypdf.append_page(page)
page.add_annot(sigannot)
 
mypdf.sign(certificate, key,
:method => 'adbe.pkcs7.sha1',
:annotation => sigannot,
:location => "India",
:contact => "fred@security-labs.org",
:reason => "Proof of Concept by Sajith"
)
#save the output file
mypdf.save('OUT.pdf')

 

 

 

 

3 Comments

3 Responses to “Origami – PDF library for Ruby”

  1. Anele Ivanova February 1, 2013 at 11:40 am #

    Hi,

    Recently I came across some great articles on your site.
    The other day, I was discussing (http://www.sajithmr.me/origami-pdf-library-for-ruby)with my colleagues and they suggested I submit an article of my own. Your site is just perfect for what I have written!
    Would it be ok to submit the article? It is free of charge, of course!

    Let me know what you think
    Contact me at anelieivanova@gmail.com

    Regards
    Anele Ivanova

    • Mr Me February 13, 2013 at 11:59 am #

      You want to submit your article in my blog ? is that the question ?

      • Aidan February 21, 2013 at 4:30 am #

        Just delete it, it’s blog spam: google.com/search?q=”Would+it+be+ok+to+submit+the+article”

Leave a Reply

More in php (3 of 108 articles)


This post is part of series HTML5 iPhone Website How can we make a tool tip bubble using CSS3. A ...