piglop-prawn 0.10.2.1
Sign up to get free protection for your applications and to get access to all the features.
- data/COPYING +340 -0
- data/HACKING +49 -0
- data/LICENSE +56 -0
- data/README +140 -0
- data/Rakefile +52 -0
- data/data/encodings/win_ansi.txt +29 -0
- data/data/fonts/Action Man.dfont +0 -0
- data/data/fonts/Activa.ttf +0 -0
- data/data/fonts/Chalkboard.ttf +0 -0
- data/data/fonts/Courier-Bold.afm +342 -0
- data/data/fonts/Courier-BoldOblique.afm +342 -0
- data/data/fonts/Courier-Oblique.afm +342 -0
- data/data/fonts/Courier.afm +342 -0
- data/data/fonts/DejaVuSans.ttf +0 -0
- data/data/fonts/Dustismo_Roman.ttf +0 -0
- data/data/fonts/Helvetica-Bold.afm +2827 -0
- data/data/fonts/Helvetica-BoldOblique.afm +2827 -0
- data/data/fonts/Helvetica-Oblique.afm +3051 -0
- data/data/fonts/Helvetica.afm +3051 -0
- data/data/fonts/MustRead.html +19 -0
- data/data/fonts/Symbol.afm +213 -0
- data/data/fonts/Times-Bold.afm +2588 -0
- data/data/fonts/Times-BoldItalic.afm +2384 -0
- data/data/fonts/Times-Italic.afm +2667 -0
- data/data/fonts/Times-Roman.afm +2419 -0
- data/data/fonts/ZapfDingbats.afm +225 -0
- data/data/fonts/comicsans.ttf +0 -0
- data/data/fonts/gkai00mp.ttf +0 -0
- data/data/images/16bit.alpha +0 -0
- data/data/images/16bit.dat +0 -0
- data/data/images/16bit.png +0 -0
- data/data/images/arrow.png +0 -0
- data/data/images/arrow2.png +0 -0
- data/data/images/barcode_issue.png +0 -0
- data/data/images/dice.alpha +0 -0
- data/data/images/dice.dat +0 -0
- data/data/images/dice.png +0 -0
- data/data/images/dice_interlaced.png +0 -0
- data/data/images/fractal.jpg +0 -0
- data/data/images/letterhead.jpg +0 -0
- data/data/images/page_white_text.alpha +0 -0
- data/data/images/page_white_text.dat +0 -0
- data/data/images/page_white_text.png +0 -0
- data/data/images/pigs.jpg +0 -0
- data/data/images/rails.dat +0 -0
- data/data/images/rails.png +0 -0
- data/data/images/ruport.png +0 -0
- data/data/images/ruport_data.dat +0 -0
- data/data/images/ruport_transparent.png +0 -0
- data/data/images/ruport_type0.png +0 -0
- data/data/images/stef.jpg +0 -0
- data/data/images/tru256.bmp +0 -0
- data/data/images/web-links.dat +1 -0
- data/data/images/web-links.png +0 -0
- data/data/pdfs/complex_template.pdf +0 -0
- data/data/pdfs/contains_ttf_font.pdf +0 -0
- data/data/pdfs/encrypted.pdf +0 -0
- data/data/pdfs/hexagon.pdf +61 -0
- data/data/pdfs/indirect_reference.pdf +86 -0
- data/data/pdfs/nested_pages.pdf +118 -0
- data/data/pdfs/resources_as_indirect_object.pdf +83 -0
- data/data/pdfs/two_hexagons.pdf +90 -0
- data/data/pdfs/version_1_6.pdf +61 -0
- data/data/shift_jis_text.txt +1 -0
- data/examples/bounding_box/bounding_boxes.rb +43 -0
- data/examples/bounding_box/indentation.rb +34 -0
- data/examples/bounding_box/russian_boxes.rb +36 -0
- data/examples/bounding_box/stretched_nesting.rb +67 -0
- data/examples/example_helper.rb +4 -0
- data/examples/general/background.rb +23 -0
- data/examples/general/canvas.rb +15 -0
- data/examples/general/context_sensitive_headers.rb +37 -0
- data/examples/general/float.rb +11 -0
- data/examples/general/margin.rb +36 -0
- data/examples/general/measurement_units.rb +51 -0
- data/examples/general/metadata-info.rb +16 -0
- data/examples/general/multi_page_layout.rb +18 -0
- data/examples/general/outlines.rb +50 -0
- data/examples/general/page_geometry.rb +31 -0
- data/examples/general/page_numbering.rb +15 -0
- data/examples/general/repeaters.rb +47 -0
- data/examples/general/stamp.rb +41 -0
- data/examples/general/templates.rb +13 -0
- data/examples/graphics/basic_images.rb +23 -0
- data/examples/graphics/cmyk.rb +12 -0
- data/examples/graphics/curves.rb +11 -0
- data/examples/graphics/hexagon.rb +13 -0
- data/examples/graphics/image_fit.rb +15 -0
- data/examples/graphics/image_flow.rb +37 -0
- data/examples/graphics/image_position.rb +17 -0
- data/examples/graphics/line.rb +32 -0
- data/examples/graphics/png_types.rb +22 -0
- data/examples/graphics/polygons.rb +16 -0
- data/examples/graphics/remote_images.rb +12 -0
- data/examples/graphics/rounded_polygons.rb +19 -0
- data/examples/graphics/rounded_rectangle.rb +20 -0
- data/examples/graphics/ruport_style_helpers.rb +19 -0
- data/examples/graphics/stroke_bounds.rb +20 -0
- data/examples/graphics/stroke_cap_and_join.rb +45 -0
- data/examples/graphics/stroke_dash.rb +42 -0
- data/examples/graphics/transformations.rb +52 -0
- data/examples/graphics/transparency.rb +26 -0
- data/examples/m17n/chinese_text_wrapping.rb +17 -0
- data/examples/m17n/euro.rb +15 -0
- data/examples/m17n/sjis.rb +28 -0
- data/examples/m17n/utf8.rb +13 -0
- data/examples/m17n/win_ansi_charset.rb +54 -0
- data/examples/security/hello_foo.rb +8 -0
- data/examples/table/bill.rb +53 -0
- data/examples/table/cell.rb +12 -0
- data/examples/table/checkerboard.rb +22 -0
- data/examples/table/header.rb +14 -0
- data/examples/table/inline_format_table.rb +12 -0
- data/examples/table/multi_page_table.rb +9 -0
- data/examples/table/simple_table.rb +24 -0
- data/examples/table/subtable.rb +12 -0
- data/examples/table/widths.rb +20 -0
- data/examples/text/alignment.rb +18 -0
- data/examples/text/dfont.rb +48 -0
- data/examples/text/family_based_styling.rb +24 -0
- data/examples/text/font_calculations.rb +91 -0
- data/examples/text/font_size.rb +33 -0
- data/examples/text/hyphenation.rb +45 -0
- data/examples/text/indent_paragraphs.rb +18 -0
- data/examples/text/inline_format.rb +72 -0
- data/examples/text/kerning.rb +30 -0
- data/examples/text/rotated.rb +98 -0
- data/examples/text/shaped_text_box.rb +31 -0
- data/examples/text/simple_text.rb +17 -0
- data/examples/text/simple_text_ttf.rb +17 -0
- data/examples/text/text_box.rb +88 -0
- data/examples/text/text_box_returning_excess.rb +51 -0
- data/examples/text/text_flow.rb +67 -0
- data/lib/prawn/compatibility.rb +51 -0
- data/lib/prawn/core/annotations.rb +61 -0
- data/lib/prawn/core/byte_string.rb +9 -0
- data/lib/prawn/core/destinations.rb +90 -0
- data/lib/prawn/core/document_state.rb +84 -0
- data/lib/prawn/core/literal_string.rb +16 -0
- data/lib/prawn/core/name_tree.rb +165 -0
- data/lib/prawn/core/object_store.rb +219 -0
- data/lib/prawn/core/page.rb +179 -0
- data/lib/prawn/core/pdf_object.rb +83 -0
- data/lib/prawn/core/reference.rb +112 -0
- data/lib/prawn/core/text/formatted/arranger.rb +260 -0
- data/lib/prawn/core/text/formatted/line_wrap.rb +127 -0
- data/lib/prawn/core/text/formatted/wrap.rb +113 -0
- data/lib/prawn/core/text/line_wrap.rb +198 -0
- data/lib/prawn/core/text/wrap.rb +80 -0
- data/lib/prawn/core/text.rb +115 -0
- data/lib/prawn/core.rb +89 -0
- data/lib/prawn/document/bounding_box.rb +425 -0
- data/lib/prawn/document/graphics_state.rb +48 -0
- data/lib/prawn/document/internals.rb +176 -0
- data/lib/prawn/document/page_geometry.rb +136 -0
- data/lib/prawn/document/snapshot.rb +87 -0
- data/lib/prawn/document.rb +573 -0
- data/lib/prawn/encoding.rb +121 -0
- data/lib/prawn/errors.rb +94 -0
- data/lib/prawn/font/afm.rb +225 -0
- data/lib/prawn/font/dfont.rb +42 -0
- data/lib/prawn/font/ttf.rb +350 -0
- data/lib/prawn/font.rb +342 -0
- data/lib/prawn/graphics/cap_style.rb +38 -0
- data/lib/prawn/graphics/color.rb +205 -0
- data/lib/prawn/graphics/dash.rb +71 -0
- data/lib/prawn/graphics/join_style.rb +38 -0
- data/lib/prawn/graphics/transformation.rb +156 -0
- data/lib/prawn/graphics/transparency.rb +99 -0
- data/lib/prawn/graphics.rb +321 -0
- data/lib/prawn/images/jpg.rb +46 -0
- data/lib/prawn/images/png.rb +226 -0
- data/lib/prawn/images.rb +348 -0
- data/lib/prawn/measurement_extensions.rb +46 -0
- data/lib/prawn/measurements.rb +71 -0
- data/lib/prawn/outline.rb +278 -0
- data/lib/prawn/repeater.rb +129 -0
- data/lib/prawn/security/arcfour.rb +51 -0
- data/lib/prawn/security.rb +262 -0
- data/lib/prawn/stamp.rb +126 -0
- data/lib/prawn/table/accessors.rb +180 -0
- data/lib/prawn/table/cell/in_table.rb +27 -0
- data/lib/prawn/table/cell/subtable.rb +65 -0
- data/lib/prawn/table/cell/text.rb +128 -0
- data/lib/prawn/table/cell.rb +330 -0
- data/lib/prawn/table.rb +421 -0
- data/lib/prawn/text/box.rb +380 -0
- data/lib/prawn/text/formatted/box.rb +215 -0
- data/lib/prawn/text/formatted/fragment.rb +127 -0
- data/lib/prawn/text/formatted/parser.rb +202 -0
- data/lib/prawn/text/formatted.rb +4 -0
- data/lib/prawn/text.rb +442 -0
- data/lib/prawn.rb +25 -0
- data/spec/annotations_spec.rb +90 -0
- data/spec/bounding_box_spec.rb +190 -0
- data/spec/cell_spec.rb +337 -0
- data/spec/destinations_spec.rb +15 -0
- data/spec/document_spec.rb +461 -0
- data/spec/font_spec.rb +314 -0
- data/spec/formatted_text_arranger_spec.rb +400 -0
- data/spec/formatted_text_box_spec.rb +659 -0
- data/spec/formatted_text_fragment_spec.rb +211 -0
- data/spec/graphics_spec.rb +446 -0
- data/spec/images_spec.rb +96 -0
- data/spec/inline_formatted_text_parser_spec.rb +446 -0
- data/spec/jpg_spec.rb +25 -0
- data/spec/line_wrap_spec.rb +341 -0
- data/spec/measurement_units_spec.rb +23 -0
- data/spec/name_tree_spec.rb +112 -0
- data/spec/object_store_spec.rb +160 -0
- data/spec/outline_spec.rb +229 -0
- data/spec/pdf_object_spec.rb +127 -0
- data/spec/png_spec.rb +237 -0
- data/spec/reference_spec.rb +82 -0
- data/spec/repeater_spec.rb +96 -0
- data/spec/security_spec.rb +120 -0
- data/spec/snapshot_spec.rb +138 -0
- data/spec/spec_helper.rb +26 -0
- data/spec/stamp_spec.rb +108 -0
- data/spec/stroke_styles_spec.rb +163 -0
- data/spec/table_spec.rb +583 -0
- data/spec/template_spec.rb +158 -0
- data/spec/text_at_spec.rb +119 -0
- data/spec/text_box_spec.rb +675 -0
- data/spec/text_spacing_spec.rb +57 -0
- data/spec/text_spec.rb +328 -0
- data/spec/text_with_inline_formatting_spec.rb +193 -0
- data/spec/transparency_spec.rb +75 -0
- data/vendor/pdf-inspector/README +18 -0
- data/vendor/pdf-inspector/lib/pdf/inspector/extgstate.rb +18 -0
- data/vendor/pdf-inspector/lib/pdf/inspector/graphics.rb +131 -0
- data/vendor/pdf-inspector/lib/pdf/inspector/page.rb +25 -0
- data/vendor/pdf-inspector/lib/pdf/inspector/text.rb +46 -0
- data/vendor/pdf-inspector/lib/pdf/inspector/xobject.rb +19 -0
- data/vendor/pdf-inspector/lib/pdf/inspector.rb +26 -0
- data/vendor/ttfunk/data/fonts/DejaVuSans.ttf +0 -0
- data/vendor/ttfunk/data/fonts/comicsans.ttf +0 -0
- data/vendor/ttfunk/example.rb +45 -0
- data/vendor/ttfunk/lib/ttfunk/directory.rb +17 -0
- data/vendor/ttfunk/lib/ttfunk/encoding/mac_roman.rb +88 -0
- data/vendor/ttfunk/lib/ttfunk/encoding/windows_1252.rb +69 -0
- data/vendor/ttfunk/lib/ttfunk/reader.rb +44 -0
- data/vendor/ttfunk/lib/ttfunk/resource_file.rb +78 -0
- data/vendor/ttfunk/lib/ttfunk/subset/base.rb +141 -0
- data/vendor/ttfunk/lib/ttfunk/subset/mac_roman.rb +50 -0
- data/vendor/ttfunk/lib/ttfunk/subset/unicode.rb +48 -0
- data/vendor/ttfunk/lib/ttfunk/subset/unicode_8bit.rb +63 -0
- data/vendor/ttfunk/lib/ttfunk/subset/windows_1252.rb +55 -0
- data/vendor/ttfunk/lib/ttfunk/subset.rb +18 -0
- data/vendor/ttfunk/lib/ttfunk/subset_collection.rb +72 -0
- data/vendor/ttfunk/lib/ttfunk/table/cmap/format00.rb +54 -0
- data/vendor/ttfunk/lib/ttfunk/table/cmap/format04.rb +126 -0
- data/vendor/ttfunk/lib/ttfunk/table/cmap/subtable.rb +79 -0
- data/vendor/ttfunk/lib/ttfunk/table/cmap.rb +34 -0
- data/vendor/ttfunk/lib/ttfunk/table/glyf/compound.rb +81 -0
- data/vendor/ttfunk/lib/ttfunk/table/glyf/simple.rb +37 -0
- data/vendor/ttfunk/lib/ttfunk/table/glyf.rb +64 -0
- data/vendor/ttfunk/lib/ttfunk/table/head.rb +44 -0
- data/vendor/ttfunk/lib/ttfunk/table/hhea.rb +41 -0
- data/vendor/ttfunk/lib/ttfunk/table/hmtx.rb +47 -0
- data/vendor/ttfunk/lib/ttfunk/table/kern/format0.rb +62 -0
- data/vendor/ttfunk/lib/ttfunk/table/kern.rb +79 -0
- data/vendor/ttfunk/lib/ttfunk/table/loca.rb +43 -0
- data/vendor/ttfunk/lib/ttfunk/table/maxp.rb +40 -0
- data/vendor/ttfunk/lib/ttfunk/table/name.rb +125 -0
- data/vendor/ttfunk/lib/ttfunk/table/os2.rb +78 -0
- data/vendor/ttfunk/lib/ttfunk/table/post/format10.rb +43 -0
- data/vendor/ttfunk/lib/ttfunk/table/post/format20.rb +35 -0
- data/vendor/ttfunk/lib/ttfunk/table/post/format25.rb +23 -0
- data/vendor/ttfunk/lib/ttfunk/table/post/format30.rb +17 -0
- data/vendor/ttfunk/lib/ttfunk/table/post/format40.rb +17 -0
- data/vendor/ttfunk/lib/ttfunk/table/post.rb +91 -0
- data/vendor/ttfunk/lib/ttfunk/table/simple.rb +14 -0
- data/vendor/ttfunk/lib/ttfunk/table.rb +46 -0
- data/vendor/ttfunk/lib/ttfunk.rb +102 -0
- metadata +364 -0
@@ -0,0 +1,262 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
# encryption.rb : Implements encrypted PDF and access permissions.
|
4
|
+
#
|
5
|
+
# Copyright August 2008, Brad Ediger. All Rights Reserved.
|
6
|
+
#
|
7
|
+
# This is free software. Please see the LICENSE and COPYING files for details.
|
8
|
+
|
9
|
+
require 'digest/md5'
|
10
|
+
require 'prawn/security/arcfour'
|
11
|
+
require 'prawn/core/byte_string'
|
12
|
+
|
13
|
+
module Prawn
|
14
|
+
class Document
|
15
|
+
|
16
|
+
# Implements PDF encryption (password protection and permissions) as
|
17
|
+
# specified in the PDF Reference, version 1.3, section 3.5 "Encryption".
|
18
|
+
module Security
|
19
|
+
include Prawn::Core
|
20
|
+
|
21
|
+
# Encrypts the document, to protect confidential data or control
|
22
|
+
# modifications to the document. The encryption algorithm used is
|
23
|
+
# detailed in the PDF Reference 1.3, section 3.5 "Encryption", and it is
|
24
|
+
# implemented by all major PDF readers.
|
25
|
+
#
|
26
|
+
# +options+ can contain the following:
|
27
|
+
#
|
28
|
+
# <tt>:user_password</tt>:: Password required to open the document. If
|
29
|
+
# this is omitted or empty, no password will be
|
30
|
+
# required. The document will still be
|
31
|
+
# encrypted, but anyone can read it.
|
32
|
+
#
|
33
|
+
# <tt>:owner_password</tt>:: Password required to make modifications to
|
34
|
+
# the document or change or override its
|
35
|
+
# permissions. If this is set to
|
36
|
+
# <tt>:random</tt>, a random password will be
|
37
|
+
# used; this can be useful if you never want
|
38
|
+
# users to be able to override the document
|
39
|
+
# permissions.
|
40
|
+
#
|
41
|
+
# <tt>:permissions</tt>:: A hash mapping permission symbols (see below) to
|
42
|
+
# <tt>true</tt> or <tt>false</tt>. True means
|
43
|
+
# "permitted", and false means "not permitted".
|
44
|
+
# All permissions default to <tt>true</tt>.
|
45
|
+
#
|
46
|
+
# The following permissions can be specified:
|
47
|
+
#
|
48
|
+
# <tt>:print_document</tt>:: Print document.
|
49
|
+
#
|
50
|
+
# <tt>:modify_document</tt>:: Modify contents of document (other than text
|
51
|
+
# annotations and interactive form fields).
|
52
|
+
#
|
53
|
+
# <tt>:copy_contents</tt>:: Copy text and graphics from document.
|
54
|
+
#
|
55
|
+
# <tt>:modify_annotations</tt>:: Add or modify text annotations and
|
56
|
+
# interactive form fields.
|
57
|
+
#
|
58
|
+
# == Examples
|
59
|
+
#
|
60
|
+
# Deny printing to everyone, but allow anyone to open without a password:
|
61
|
+
#
|
62
|
+
# encrypt_document :permissions => { :print_document => false },
|
63
|
+
# :owner_password => :random
|
64
|
+
#
|
65
|
+
# Set a user and owner password on the document, with full permissions for
|
66
|
+
# both the user and the owner:
|
67
|
+
#
|
68
|
+
# encrypt_document :user_password => 'foo', :owner_password => 'bar'
|
69
|
+
#
|
70
|
+
# Set no passwords, grant all permissions (This is useful because the
|
71
|
+
# default in some readers, if no permissions are specified, is "deny"):
|
72
|
+
#
|
73
|
+
# encrypt_document
|
74
|
+
#
|
75
|
+
# == Caveats
|
76
|
+
#
|
77
|
+
# * The encryption used is weak; the key is password-derived and is
|
78
|
+
# limited to 40 bits, due to US export controls in effect at the time
|
79
|
+
# the PDF standard was written.
|
80
|
+
#
|
81
|
+
# * There is nothing technologically requiring PDF readers to respect the
|
82
|
+
# permissions embedded in a document. Many PDF readers do not.
|
83
|
+
#
|
84
|
+
# * In short, you have <b>no security at all</b> against a moderately
|
85
|
+
# motivated person. Don't use this for anything super-serious. This is
|
86
|
+
# not a limitation of Prawn, but is rather a built-in limitation of the
|
87
|
+
# PDF format.
|
88
|
+
#
|
89
|
+
def encrypt_document(options={})
|
90
|
+
Prawn.verify_options [:user_password, :owner_password, :permissions],
|
91
|
+
options
|
92
|
+
@user_password = options.delete(:user_password) || ""
|
93
|
+
|
94
|
+
@owner_password = options.delete(:owner_password) || @user_password
|
95
|
+
if @owner_password == :random
|
96
|
+
# Generate a completely ridiculous password
|
97
|
+
@owner_password = (1..32).map{ rand(256) }.pack("c*")
|
98
|
+
end
|
99
|
+
|
100
|
+
self.permissions = options.delete(:permissions) || {}
|
101
|
+
|
102
|
+
# Shove the necessary entries in the trailer and enable encryption.
|
103
|
+
state.trailer[:Encrypt] = encryption_dictionary
|
104
|
+
state.encrypt = true
|
105
|
+
state.encryption_key = user_encryption_key
|
106
|
+
end
|
107
|
+
|
108
|
+
# Encrypts the given string under the given key, also requiring the
|
109
|
+
# object ID and generation number of the reference.
|
110
|
+
# See Algorithm 3.1.
|
111
|
+
def self.encrypt_string(str, key, id, gen)
|
112
|
+
# Convert ID and Gen number into little-endian truncated byte strings
|
113
|
+
id = [id].pack('V')[0,3]
|
114
|
+
gen = [gen].pack('V')[0,2]
|
115
|
+
extended_key = "#{key}#{id}#{gen}"
|
116
|
+
|
117
|
+
# Compute the RC4 key from the extended key and perform the encryption
|
118
|
+
rc4_key = Digest::MD5.digest(extended_key)[0, 10]
|
119
|
+
Arcfour.new(rc4_key).encrypt(str)
|
120
|
+
end
|
121
|
+
|
122
|
+
private
|
123
|
+
|
124
|
+
# Provides the values for the trailer encryption dictionary.
|
125
|
+
def encryption_dictionary
|
126
|
+
{ :Filter => :Standard, # default PDF security handler
|
127
|
+
:V => 1, # "Algorithm 3.1", PDF reference 1.3
|
128
|
+
:R => 2, # Revision 2 of the algorithm
|
129
|
+
:O => ByteString.new(owner_password_hash),
|
130
|
+
:U => ByteString.new(user_password_hash),
|
131
|
+
:P => permissions_value }
|
132
|
+
end
|
133
|
+
|
134
|
+
# Flags in the permissions word, numbered as LSB = 1
|
135
|
+
PermissionsBits = { :print_document => 3,
|
136
|
+
:modify_contents => 4,
|
137
|
+
:copy_contents => 5,
|
138
|
+
:modify_annotations => 6 }
|
139
|
+
|
140
|
+
FullPermissions = 0b1111_1111_1111_1111_1111_1111_1111_1111
|
141
|
+
|
142
|
+
def permissions=(perms={})
|
143
|
+
@permissions ||= FullPermissions
|
144
|
+
perms.each do |key, value|
|
145
|
+
# 0-based bit number, from LSB
|
146
|
+
bit_position = PermissionsBits[key] - 1
|
147
|
+
|
148
|
+
if value # set bit
|
149
|
+
@permissions |= (1 << bit_position)
|
150
|
+
else # clear bit
|
151
|
+
@permissions &= ~(1 << bit_position)
|
152
|
+
end
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
156
|
+
def permissions_value
|
157
|
+
@permissions || FullPermissions
|
158
|
+
end
|
159
|
+
|
160
|
+
PasswordPadding =
|
161
|
+
"28BF4E5E4E758A4164004E56FFFA01082E2E00B6D0683E802F0CA9FE6453697A".
|
162
|
+
scan(/../).map{|x| x.to_i(16)}.pack("c*")
|
163
|
+
|
164
|
+
# Pads or truncates a password to 32 bytes as per Alg 3.2.
|
165
|
+
def pad_password(password)
|
166
|
+
password = password[0, 32]
|
167
|
+
password + PasswordPadding[0, 32 - password.length]
|
168
|
+
end
|
169
|
+
|
170
|
+
def user_encryption_key
|
171
|
+
@user_encryption_key ||= begin
|
172
|
+
md5 = Digest::MD5.new
|
173
|
+
md5 << pad_password(@user_password)
|
174
|
+
md5 << owner_password_hash
|
175
|
+
md5 << [permissions_value].pack("V")
|
176
|
+
md5.digest[0, 5]
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
# The O (owner) value in the encryption dictionary. Algorithm 3.3.
|
181
|
+
def owner_password_hash
|
182
|
+
@owner_password_hash ||= begin
|
183
|
+
key = Digest::MD5.digest(pad_password(@owner_password))[0, 5]
|
184
|
+
Arcfour.new(key).encrypt(pad_password(@user_password))
|
185
|
+
end
|
186
|
+
end
|
187
|
+
|
188
|
+
# The U (user) value in the encryption dictionary. Algorithm 3.4.
|
189
|
+
def user_password_hash
|
190
|
+
Arcfour.new(user_encryption_key).encrypt(PasswordPadding)
|
191
|
+
end
|
192
|
+
|
193
|
+
end
|
194
|
+
|
195
|
+
end
|
196
|
+
|
197
|
+
module Core #:nodoc:
|
198
|
+
module_function
|
199
|
+
|
200
|
+
# Like PdfObject, but returns an encrypted result if required.
|
201
|
+
# For direct objects, requires the object identifier and generation number
|
202
|
+
# from the indirect object referencing obj.
|
203
|
+
def EncryptedPdfObject(obj, key, id, gen, in_content_stream=false)
|
204
|
+
case obj
|
205
|
+
when Array
|
206
|
+
"[" << obj.map { |e|
|
207
|
+
EncryptedPdfObject(e, key, id, gen, in_content_stream)
|
208
|
+
}.join(' ') << "]"
|
209
|
+
when LiteralString
|
210
|
+
# FIXME: encrypted?
|
211
|
+
obj = obj.gsub(/[\\\n\(\)]/) { |m| "\\#{m}" }
|
212
|
+
"(#{obj})"
|
213
|
+
when Time
|
214
|
+
# FIXME: encrypted?
|
215
|
+
obj = obj.strftime("D:%Y%m%d%H%M%S%z").chop.chop + "'00'"
|
216
|
+
obj = obj.gsub(/[\\\n\(\)]/) { |m| "\\#{m}" }
|
217
|
+
"(#{obj})"
|
218
|
+
when String
|
219
|
+
PdfObject(
|
220
|
+
ByteString.new(
|
221
|
+
Document::Security.encrypt_string(obj, key, id, gen)),
|
222
|
+
in_content_stream)
|
223
|
+
when Hash
|
224
|
+
output = "<< "
|
225
|
+
obj.each do |k,v|
|
226
|
+
unless String === k || Symbol === k
|
227
|
+
raise Prawn::Errors::FailedObjectConversion,
|
228
|
+
"A PDF Dictionary must be keyed by names"
|
229
|
+
end
|
230
|
+
output << PdfObject(k.to_sym, in_content_stream) << " " <<
|
231
|
+
EncryptedPdfObject(v, key, id, gen, in_content_stream) << "\n"
|
232
|
+
end
|
233
|
+
output << ">>"
|
234
|
+
when NameTree::Value
|
235
|
+
PdfObject(obj.name) + " " +
|
236
|
+
EncryptedPdfObject(obj.value, key, id, gen, in_content_stream)
|
237
|
+
else # delegate back to PdfObject
|
238
|
+
PdfObject(obj, in_content_stream)
|
239
|
+
end
|
240
|
+
end
|
241
|
+
|
242
|
+
class Reference
|
243
|
+
|
244
|
+
# Returns the object definition for the object this references, keyed from
|
245
|
+
# +key+.
|
246
|
+
def encrypted_object(key)
|
247
|
+
@on_encode.call(self) if @on_encode
|
248
|
+
output = "#{@identifier} #{gen} obj\n" <<
|
249
|
+
Prawn::Core::EncryptedPdfObject(data, key, @identifier, gen) << "\n"
|
250
|
+
if @stream
|
251
|
+
output << "stream\n" <<
|
252
|
+
Document::Security.encrypt_string(@stream, key, @identifier, gen) <<
|
253
|
+
"\nendstream\n"
|
254
|
+
end
|
255
|
+
output << "endobj\n"
|
256
|
+
end
|
257
|
+
|
258
|
+
end
|
259
|
+
end
|
260
|
+
|
261
|
+
end
|
262
|
+
|
data/lib/prawn/stamp.rb
ADDED
@@ -0,0 +1,126 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
# stamp.rb : Implements a repeatable stamp
|
4
|
+
#
|
5
|
+
# Copyright October 2009, Daniel Nelson. All Rights Reserved.
|
6
|
+
#
|
7
|
+
# This is free software. Please see the LICENSE and COPYING files for details.
|
8
|
+
#
|
9
|
+
|
10
|
+
module Prawn
|
11
|
+
|
12
|
+
# The Prawn::Stamp module is used to create content that will be
|
13
|
+
# included multiple times in a document. Using a stamp has three
|
14
|
+
# advantages over creating content anew each time it is placed on
|
15
|
+
# the page:
|
16
|
+
# i. faster document creation
|
17
|
+
# ii. smaller final document
|
18
|
+
# iii. faster display on subsequent displays of the repeated
|
19
|
+
# element because the viewer application can cache the rendered
|
20
|
+
# results
|
21
|
+
#
|
22
|
+
# Example:
|
23
|
+
# pdf.create_stamp("my_stamp") {
|
24
|
+
# pdf.fill_circle_at([10, 15], :radius => 5)
|
25
|
+
# pdf.draw_text("hello world", :at => [20, 10])
|
26
|
+
# }
|
27
|
+
# pdf.stamp("my_stamp")
|
28
|
+
#
|
29
|
+
module Stamp
|
30
|
+
|
31
|
+
# Renders the stamp named <tt>name</tt> to the page
|
32
|
+
# raises <tt>Prawn::Errors::InvalidName</tt> if name.empty?
|
33
|
+
# raises <tt>Prawn::Errors::UndefinedObjectName</tt> if no stamp
|
34
|
+
# has been created with this name
|
35
|
+
#
|
36
|
+
# Example:
|
37
|
+
# pdf.create_stamp("my_stamp") {
|
38
|
+
# pdf.fill_circle_at([10, 15], :radius => 5)
|
39
|
+
# pdf.text("hello world", :at => [20, 10])
|
40
|
+
# }
|
41
|
+
# pdf.stamp("my_stamp")
|
42
|
+
#
|
43
|
+
def stamp(name)
|
44
|
+
dictionary_name, dictionary = stamp_dictionary(name)
|
45
|
+
add_content "/#{dictionary_name} Do"
|
46
|
+
state.page.xobjects.merge!(dictionary_name => dictionary)
|
47
|
+
end
|
48
|
+
|
49
|
+
# Renders the stamp named <tt>name</tt> at a position offset from
|
50
|
+
# the initial coords at which the elements of the stamp was
|
51
|
+
# created
|
52
|
+
#
|
53
|
+
# Example:
|
54
|
+
# pdf.create_stamp("circle") do
|
55
|
+
# pdf.fill_circle_at([0, 0], :radius => 25)
|
56
|
+
# end
|
57
|
+
# # draws a circle at 100, 100
|
58
|
+
# pdf.stamp_at("circle", [100, 100])
|
59
|
+
#
|
60
|
+
# See stamp() for exceptions that might be raised
|
61
|
+
#
|
62
|
+
def stamp_at(name, point)
|
63
|
+
translate(point[0], point[1]) { stamp(name) }
|
64
|
+
end
|
65
|
+
|
66
|
+
# Creates a re-usable stamp named <tt>name</tt>
|
67
|
+
#
|
68
|
+
# raises <tt>Prawn::Errors::NameTaken</tt> if a stamp already
|
69
|
+
# exists in this document with this name
|
70
|
+
# raises <tt>Prawn::Errors::InvalidName</tt> if name.empty?
|
71
|
+
#
|
72
|
+
# Example:
|
73
|
+
# pdf.create_stamp("my_stamp") {
|
74
|
+
# pdf.fill_circle_at([10, 15], :radius => 5)
|
75
|
+
# pdf.draw_text("hello world", :at => [20, 10])
|
76
|
+
# }
|
77
|
+
#
|
78
|
+
def create_stamp(name, &block)
|
79
|
+
dictionary = create_stamp_dictionary(name)
|
80
|
+
|
81
|
+
state.page.stamp_stream(dictionary, &block)
|
82
|
+
end
|
83
|
+
|
84
|
+
private
|
85
|
+
|
86
|
+
def stamp_dictionary_registry
|
87
|
+
@stamp_dictionary_registry ||= {}
|
88
|
+
end
|
89
|
+
|
90
|
+
def next_stamp_dictionary_id
|
91
|
+
stamp_dictionary_registry.length + 1
|
92
|
+
end
|
93
|
+
|
94
|
+
def stamp_dictionary(name)
|
95
|
+
raise Prawn::Errors::InvalidName if name.empty?
|
96
|
+
if stamp_dictionary_registry[name].nil?
|
97
|
+
raise Prawn::Errors::UndefinedObjectName
|
98
|
+
end
|
99
|
+
|
100
|
+
dict = stamp_dictionary_registry[name]
|
101
|
+
|
102
|
+
dictionary_name = dict[:stamp_dictionary_name]
|
103
|
+
dictionary = dict[:stamp_dictionary]
|
104
|
+
[dictionary_name, dictionary]
|
105
|
+
end
|
106
|
+
|
107
|
+
def create_stamp_dictionary(name)
|
108
|
+
raise Prawn::Errors::InvalidName if name.empty?
|
109
|
+
raise Prawn::Errors::NameTaken unless stamp_dictionary_registry[name].nil?
|
110
|
+
# BBox origin is the lower left margin of the page, so we need
|
111
|
+
# it to be the full dimension of the page, or else things that
|
112
|
+
# should appear near the top or right margin are invisible
|
113
|
+
dictionary = ref!(:Type => :XObject,
|
114
|
+
:Subtype => :Form,
|
115
|
+
:BBox => [0, 0,
|
116
|
+
state.page.dimensions[2], state.page.dimensions[3]])
|
117
|
+
|
118
|
+
dictionary_name = "Stamp#{next_stamp_dictionary_id}"
|
119
|
+
|
120
|
+
stamp_dictionary_registry[name] = { :stamp_dictionary_name => dictionary_name,
|
121
|
+
:stamp_dictionary => dictionary }
|
122
|
+
dictionary
|
123
|
+
end
|
124
|
+
|
125
|
+
end
|
126
|
+
end
|
@@ -0,0 +1,180 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# accessors.rb: Methods for accessing rows, columns, and cells of a
|
4
|
+
# Prawn::Table.
|
5
|
+
#
|
6
|
+
# Copyright December 2009, Brad Ediger. All Rights Reserved.
|
7
|
+
#
|
8
|
+
# This is free software. Please see the LICENSE and COPYING files for details.
|
9
|
+
module Prawn
|
10
|
+
|
11
|
+
class Table
|
12
|
+
|
13
|
+
# Returns a CellProxy that can be used to select and style cells. See the
|
14
|
+
# CellProxy documentation for things you can do with cells.
|
15
|
+
#
|
16
|
+
def cells
|
17
|
+
@cell_proxy ||= CellProxy.new(@cells)
|
18
|
+
end
|
19
|
+
|
20
|
+
# Selects the given rows (0-based) for styling. Returns a CellProxy -- see
|
21
|
+
# the documentation on CellProxy for things you can do with cells.
|
22
|
+
#
|
23
|
+
def rows(row_spec)
|
24
|
+
cells.rows(row_spec)
|
25
|
+
end
|
26
|
+
alias_method :row, :rows
|
27
|
+
|
28
|
+
# Selects the given columns (0-based) for styling. Returns a CellProxy --
|
29
|
+
# see the documentation on CellProxy for things you can do with cells.
|
30
|
+
#
|
31
|
+
def columns(col_spec)
|
32
|
+
cells.columns(col_spec)
|
33
|
+
end
|
34
|
+
alias_method :column, :columns
|
35
|
+
|
36
|
+
# Represents a selection of cells to be styled. Operations on a CellProxy
|
37
|
+
# can be chained, and cell properties can be set one-for-all on the proxy.
|
38
|
+
#
|
39
|
+
# To set vertical borders only:
|
40
|
+
#
|
41
|
+
# table.cells.borders = [:left, :right]
|
42
|
+
#
|
43
|
+
# To highlight a rectangular area of the table:
|
44
|
+
#
|
45
|
+
# table.rows(1..3).columns(2..4).background_color = 'ff0000'
|
46
|
+
#
|
47
|
+
class CellProxy
|
48
|
+
def initialize(cells) #:nodoc:
|
49
|
+
@cells = cells
|
50
|
+
end
|
51
|
+
|
52
|
+
# Iterates over cells in turn.
|
53
|
+
#
|
54
|
+
def each(&b)
|
55
|
+
@cells.each(&b)
|
56
|
+
end
|
57
|
+
|
58
|
+
include Enumerable
|
59
|
+
|
60
|
+
# Limits selection to the given row or rows. +row_spec+ can be anything
|
61
|
+
# that responds to the === operator selecting a set of 0-based row
|
62
|
+
# numbers; most commonly a number or a range.
|
63
|
+
#
|
64
|
+
# table.row(0) # selects first row
|
65
|
+
# table.rows(3..4) # selects rows four and five
|
66
|
+
#
|
67
|
+
def rows(row_spec)
|
68
|
+
CellProxy.new(@cells.select { |c| row_spec === c.row })
|
69
|
+
end
|
70
|
+
alias_method :row, :rows
|
71
|
+
|
72
|
+
# Limits selection to the given column or columns. +col_spec+ can be
|
73
|
+
# anything that responds to the === operator selecting a set of 0-based
|
74
|
+
# column numbers; most commonly a number or a range.
|
75
|
+
#
|
76
|
+
# table.column(0) # selects first column
|
77
|
+
# table.columns(3..4) # selects columns four and five
|
78
|
+
#
|
79
|
+
def columns(col_spec)
|
80
|
+
CellProxy.new(@cells.select { |c| col_spec === c.column })
|
81
|
+
end
|
82
|
+
alias_method :column, :columns
|
83
|
+
|
84
|
+
# Selects cells based on a block.
|
85
|
+
#
|
86
|
+
# table.column(4).select { |cell| cell.content =~ /Yes/ }.
|
87
|
+
# background_color = 'ff0000'
|
88
|
+
#
|
89
|
+
def select(&b)
|
90
|
+
CellProxy.new(@cells.select(&b))
|
91
|
+
end
|
92
|
+
|
93
|
+
# Retrieves a cell based on its 0-based row and column. Returns a Cell,
|
94
|
+
# not a CellProxy.
|
95
|
+
#
|
96
|
+
# table.cells[0, 0].content # => "First cell content"
|
97
|
+
#
|
98
|
+
def [](row, col)
|
99
|
+
@cells.find { |c| c.row == row && c.column == col }
|
100
|
+
end
|
101
|
+
|
102
|
+
# Supports setting multiple properties at once.
|
103
|
+
#
|
104
|
+
# table.cells.style(:padding => 0, :border_width => 2)
|
105
|
+
#
|
106
|
+
# is the same as:
|
107
|
+
#
|
108
|
+
# table.cells.padding = 0
|
109
|
+
# table.cells.border_width = 2
|
110
|
+
#
|
111
|
+
# You can also pass a block, which will be called for each cell in turn.
|
112
|
+
# This allows you to set more complicated properties:
|
113
|
+
#
|
114
|
+
# table.cells.style { |cell| cell.border_width += 12 }
|
115
|
+
#
|
116
|
+
def style(options={}, &block)
|
117
|
+
@cells.each do |cell|
|
118
|
+
options.each { |k, v| cell.send("#{k}=", v) }
|
119
|
+
block.call(cell) if block
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
# Returns the total width of all columns in the selected set.
|
124
|
+
#
|
125
|
+
def width
|
126
|
+
column_widths = {}
|
127
|
+
@cells.each do |cell|
|
128
|
+
column_widths[cell.column] =
|
129
|
+
[column_widths[cell.column], cell.width].compact.max
|
130
|
+
end
|
131
|
+
column_widths.values.inject(0) { |sum, width| sum + width }
|
132
|
+
end
|
133
|
+
|
134
|
+
# Returns minimum width required to contain cells in the set.
|
135
|
+
#
|
136
|
+
def min_width
|
137
|
+
column_min_widths = {}
|
138
|
+
@cells.each do |cell|
|
139
|
+
column_min_widths[cell.column] =
|
140
|
+
[column_min_widths[cell.column], cell.min_width].compact.max
|
141
|
+
end
|
142
|
+
column_min_widths.values.inject(0) { |sum, width| sum + width }
|
143
|
+
end
|
144
|
+
|
145
|
+
# Returns maximum width that can contain cells in the set.
|
146
|
+
#
|
147
|
+
def max_width
|
148
|
+
column_max_widths = {}
|
149
|
+
@cells.each do |cell|
|
150
|
+
column_max_widths[cell.column] =
|
151
|
+
[column_max_widths[cell.column], cell.max_width].compact.min
|
152
|
+
end
|
153
|
+
column_max_widths.values.inject(0) { |sum, width| sum + width }
|
154
|
+
end
|
155
|
+
|
156
|
+
# Returns the total height of all rows in the selected set.
|
157
|
+
#
|
158
|
+
def height
|
159
|
+
row_heights = {}
|
160
|
+
@cells.each do |cell|
|
161
|
+
row_heights[cell.row] =
|
162
|
+
[row_heights[cell.row], cell.height].compact.max
|
163
|
+
end
|
164
|
+
row_heights.values.inject(0) { |sum, width| sum + width }
|
165
|
+
end
|
166
|
+
|
167
|
+
# Supports setting arbitrary properties on a group of cells.
|
168
|
+
#
|
169
|
+
# table.cells.row(3..6).background_color = 'cc0000'
|
170
|
+
#
|
171
|
+
def method_missing(id, *args, &block)
|
172
|
+
@cells.each { |c| c.send(id, *args, &block) }
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
end
|
177
|
+
|
178
|
+
end
|
179
|
+
|
180
|
+
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# Accessors for using a Cell inside a Table.
|
4
|
+
|
5
|
+
module Prawn
|
6
|
+
class Table
|
7
|
+
class Cell
|
8
|
+
|
9
|
+
# This module extends Cell objects when they are used in a table (as
|
10
|
+
# opposed to standalone). Its properties apply to cells-in-tables but not
|
11
|
+
# cells themselves.
|
12
|
+
#
|
13
|
+
module InTable
|
14
|
+
|
15
|
+
# Row number (0-based).
|
16
|
+
#
|
17
|
+
attr_accessor :row
|
18
|
+
|
19
|
+
# Column number (0-based).
|
20
|
+
#
|
21
|
+
attr_accessor :column
|
22
|
+
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,65 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
# subtable.rb: Yo dawg.
|
4
|
+
#
|
5
|
+
# Copyright January 2010, Brad Ediger. All Rights Reserved.
|
6
|
+
#
|
7
|
+
# This is free software. Please see the LICENSE and COPYING files for details.
|
8
|
+
module Prawn
|
9
|
+
class Table
|
10
|
+
class Cell
|
11
|
+
|
12
|
+
# A Cell that contains another table.
|
13
|
+
#
|
14
|
+
class Subtable < Cell
|
15
|
+
|
16
|
+
attr_reader :subtable
|
17
|
+
|
18
|
+
def initialize(pdf, point, options={})
|
19
|
+
super
|
20
|
+
@subtable = options[:content]
|
21
|
+
|
22
|
+
# Subtable padding defaults to zero
|
23
|
+
@padding = [0, 0, 0, 0]
|
24
|
+
end
|
25
|
+
|
26
|
+
# Sets the text color of the entire subtable.
|
27
|
+
#
|
28
|
+
def text_color=(color)
|
29
|
+
@subtable.cells.text_color = color
|
30
|
+
end
|
31
|
+
|
32
|
+
# Proxied to subtable.
|
33
|
+
#
|
34
|
+
def natural_content_width
|
35
|
+
@subtable.cells.width
|
36
|
+
end
|
37
|
+
|
38
|
+
# Proxied to subtable.
|
39
|
+
#
|
40
|
+
def min_width
|
41
|
+
@subtable.cells.min_width
|
42
|
+
end
|
43
|
+
|
44
|
+
# Proxied to subtable.
|
45
|
+
#
|
46
|
+
def max_width
|
47
|
+
@subtable.cells.max_width
|
48
|
+
end
|
49
|
+
|
50
|
+
# Proxied to subtable.
|
51
|
+
#
|
52
|
+
def natural_content_height
|
53
|
+
@subtable.cells.height
|
54
|
+
end
|
55
|
+
|
56
|
+
# Draws the subtable.
|
57
|
+
#
|
58
|
+
def draw_content
|
59
|
+
@subtable.draw
|
60
|
+
end
|
61
|
+
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|