nurettin-prawn 1.0.0.rc1
Sign up to get free protection for your applications and to get access to all the features.
- data/COPYING +2 -0
- data/GPLv2 +340 -0
- data/GPLv3 +674 -0
- data/Gemfile +18 -0
- data/LICENSE +56 -0
- data/README.md +98 -0
- data/Rakefile +45 -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/prawn.png +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/form.pdf +820 -0
- data/data/pdfs/hexagon.pdf +61 -0
- data/data/pdfs/indirect_reference.pdf +86 -0
- data/data/pdfs/multipage_template.pdf +127 -0
- data/data/pdfs/nested_pages.pdf +118 -0
- data/data/pdfs/page_without_mediabox.pdf +193 -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/lib/prawn.rb +29 -0
- data/lib/prawn/compatibility.rb +87 -0
- data/lib/prawn/core.rb +87 -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 +79 -0
- data/lib/prawn/core/literal_string.rb +16 -0
- data/lib/prawn/core/name_tree.rb +177 -0
- data/lib/prawn/core/object_store.rb +320 -0
- data/lib/prawn/core/page.rb +212 -0
- data/lib/prawn/core/pdf_object.rb +125 -0
- data/lib/prawn/core/reference.rb +119 -0
- data/lib/prawn/core/text.rb +268 -0
- data/lib/prawn/core/text/formatted/arranger.rb +294 -0
- data/lib/prawn/core/text/formatted/line_wrap.rb +288 -0
- data/lib/prawn/core/text/formatted/wrap.rb +153 -0
- data/lib/prawn/document.rb +707 -0
- data/lib/prawn/document/bounding_box.rb +510 -0
- data/lib/prawn/document/column_box.rb +132 -0
- data/lib/prawn/document/graphics_state.rb +136 -0
- data/lib/prawn/document/internals.rb +173 -0
- data/lib/prawn/document/page_geometry.rb +136 -0
- data/lib/prawn/document/snapshot.rb +89 -0
- data/lib/prawn/document/span.rb +55 -0
- data/lib/prawn/encoding.rb +121 -0
- data/lib/prawn/errors.rb +99 -0
- data/lib/prawn/font.rb +386 -0
- data/lib/prawn/font/afm.rb +203 -0
- data/lib/prawn/font/dfont.rb +42 -0
- data/lib/prawn/font/ttf.rb +343 -0
- data/lib/prawn/graphics.rb +523 -0
- data/lib/prawn/graphics/cap_style.rb +46 -0
- data/lib/prawn/graphics/color.rb +231 -0
- data/lib/prawn/graphics/dash.rb +82 -0
- data/lib/prawn/graphics/join_style.rb +47 -0
- data/lib/prawn/graphics/patterns.rb +137 -0
- data/lib/prawn/graphics/transformation.rb +156 -0
- data/lib/prawn/graphics/transparency.rb +99 -0
- data/lib/prawn/images.rb +196 -0
- data/lib/prawn/images/image.rb +65 -0
- data/lib/prawn/images/jpg.rb +85 -0
- data/lib/prawn/images/png.rb +362 -0
- data/lib/prawn/layout.rb +20 -0
- data/lib/prawn/layout/grid.rb +259 -0
- data/lib/prawn/measurement_extensions.rb +46 -0
- data/lib/prawn/measurements.rb +71 -0
- data/lib/prawn/outline.rb +326 -0
- data/lib/prawn/repeater.rb +122 -0
- data/lib/prawn/security.rb +269 -0
- data/lib/prawn/soft_mask.rb +94 -0
- data/lib/prawn/stamp.rb +134 -0
- data/lib/prawn/table.rb +609 -0
- data/lib/prawn/table/cell.rb +776 -0
- data/lib/prawn/table/cell/image.rb +70 -0
- data/lib/prawn/table/cell/in_table.rb +27 -0
- data/lib/prawn/table/cell/span_dummy.rb +88 -0
- data/lib/prawn/table/cell/subtable.rb +65 -0
- data/lib/prawn/table/cell/text.rb +152 -0
- data/lib/prawn/table/cells.rb +260 -0
- data/lib/prawn/text.rb +420 -0
- data/lib/prawn/text/box.rb +141 -0
- data/lib/prawn/text/formatted.rb +4 -0
- data/lib/prawn/text/formatted/box.rb +563 -0
- data/lib/prawn/text/formatted/fragment.rb +253 -0
- data/lib/prawn/text/formatted/parser.rb +217 -0
- data/lib/prawn/utilities.rb +44 -0
- data/manual/basic_concepts/adding_pages.rb +27 -0
- data/manual/basic_concepts/basic_concepts.rb +34 -0
- data/manual/basic_concepts/creation.rb +39 -0
- data/manual/basic_concepts/cursor.rb +33 -0
- data/manual/basic_concepts/measurement.rb +25 -0
- data/manual/basic_concepts/origin.rb +38 -0
- data/manual/basic_concepts/other_cursor_helpers.rb +40 -0
- data/manual/bounding_box/bounding_box.rb +39 -0
- data/manual/bounding_box/bounds.rb +49 -0
- data/manual/bounding_box/canvas.rb +24 -0
- data/manual/bounding_box/creation.rb +23 -0
- data/manual/bounding_box/indentation.rb +46 -0
- data/manual/bounding_box/nesting.rb +45 -0
- data/manual/bounding_box/russian_boxes.rb +40 -0
- data/manual/bounding_box/stretchy.rb +31 -0
- data/manual/document_and_page_options/background.rb +27 -0
- data/manual/document_and_page_options/document_and_page_options.rb +31 -0
- data/manual/document_and_page_options/metadata.rb +23 -0
- data/manual/document_and_page_options/page_margins.rb +38 -0
- data/manual/document_and_page_options/page_size.rb +34 -0
- data/manual/example_file.rb +116 -0
- data/manual/example_helper.rb +430 -0
- data/manual/example_package.rb +53 -0
- data/manual/example_section.rb +46 -0
- data/manual/graphics/circle_and_ellipse.rb +22 -0
- data/manual/graphics/color.rb +24 -0
- data/manual/graphics/common_lines.rb +28 -0
- data/manual/graphics/fill_and_stroke.rb +42 -0
- data/manual/graphics/fill_rules.rb +37 -0
- data/manual/graphics/gradients.rb +37 -0
- data/manual/graphics/graphics.rb +58 -0
- data/manual/graphics/helper.rb +17 -0
- data/manual/graphics/line_width.rb +35 -0
- data/manual/graphics/lines_and_curves.rb +41 -0
- data/manual/graphics/polygon.rb +29 -0
- data/manual/graphics/rectangle.rb +21 -0
- data/manual/graphics/rotate.rb +28 -0
- data/manual/graphics/scale.rb +41 -0
- data/manual/graphics/soft_masks.rb +46 -0
- data/manual/graphics/stroke_cap.rb +31 -0
- data/manual/graphics/stroke_dash.rb +43 -0
- data/manual/graphics/stroke_join.rb +30 -0
- data/manual/graphics/translate.rb +29 -0
- data/manual/graphics/transparency.rb +35 -0
- data/manual/images/absolute_position.rb +23 -0
- data/manual/images/fit.rb +21 -0
- data/manual/images/horizontal.rb +25 -0
- data/manual/images/images.rb +40 -0
- data/manual/images/plain_image.rb +18 -0
- data/manual/images/scale.rb +22 -0
- data/manual/images/vertical.rb +28 -0
- data/manual/images/width_and_height.rb +25 -0
- data/manual/layout/boxes.rb +27 -0
- data/manual/layout/content.rb +25 -0
- data/manual/layout/layout.rb +28 -0
- data/manual/layout/simple_grid.rb +23 -0
- data/manual/manual/cover.rb +26 -0
- data/manual/manual/foreword.rb +13 -0
- data/manual/manual/how_to_read_this_manual.rb +41 -0
- data/manual/manual/manual.rb +36 -0
- data/manual/outline/add_subsection_to.rb +61 -0
- data/manual/outline/insert_section_after.rb +47 -0
- data/manual/outline/outline.rb +32 -0
- data/manual/outline/sections_and_pages.rb +67 -0
- data/manual/repeatable_content/page_numbering.rb +54 -0
- data/manual/repeatable_content/repeatable_content.rb +31 -0
- data/manual/repeatable_content/repeater.rb +55 -0
- data/manual/repeatable_content/stamp.rb +41 -0
- data/manual/security/encryption.rb +31 -0
- data/manual/security/permissions.rb +38 -0
- data/manual/security/security.rb +28 -0
- data/manual/syntax_highlight.rb +52 -0
- data/manual/table/basic_block.rb +53 -0
- data/manual/table/before_rendering_page.rb +26 -0
- data/manual/table/cell_border_lines.rb +24 -0
- data/manual/table/cell_borders_and_bg.rb +31 -0
- data/manual/table/cell_dimensions.rb +30 -0
- data/manual/table/cell_text.rb +38 -0
- data/manual/table/column_widths.rb +30 -0
- data/manual/table/content_and_subtables.rb +39 -0
- data/manual/table/creation.rb +27 -0
- data/manual/table/filtering.rb +36 -0
- data/manual/table/flow_and_header.rb +17 -0
- data/manual/table/image_cells.rb +33 -0
- data/manual/table/position.rb +29 -0
- data/manual/table/row_colors.rb +20 -0
- data/manual/table/span.rb +30 -0
- data/manual/table/style.rb +22 -0
- data/manual/table/table.rb +52 -0
- data/manual/table/width.rb +27 -0
- data/manual/templates/full_template.rb +23 -0
- data/manual/templates/page_template.rb +47 -0
- data/manual/templates/templates.rb +26 -0
- data/manual/text/alignment.rb +44 -0
- data/manual/text/color.rb +24 -0
- data/manual/text/column_box.rb +32 -0
- data/manual/text/fallback_fonts.rb +37 -0
- data/manual/text/font.rb +41 -0
- data/manual/text/font_size.rb +45 -0
- data/manual/text/font_style.rb +23 -0
- data/manual/text/formatted_callbacks.rb +60 -0
- data/manual/text/formatted_text.rb +50 -0
- data/manual/text/free_flowing_text.rb +51 -0
- data/manual/text/group.rb +29 -0
- data/manual/text/inline.rb +43 -0
- data/manual/text/kerning_and_character_spacing.rb +39 -0
- data/manual/text/leading.rb +25 -0
- data/manual/text/line_wrapping.rb +41 -0
- data/manual/text/paragraph_indentation.rb +26 -0
- data/manual/text/positioned_text.rb +38 -0
- data/manual/text/registering_families.rb +48 -0
- data/manual/text/rendering_and_color.rb +37 -0
- data/manual/text/right_to_left_text.rb +43 -0
- data/manual/text/rotation.rb +43 -0
- data/manual/text/single_usage.rb +37 -0
- data/manual/text/text.rb +75 -0
- data/manual/text/text_box_excess.rb +32 -0
- data/manual/text/text_box_extensions.rb +45 -0
- data/manual/text/text_box_overflow.rb +44 -0
- data/manual/text/utf8.rb +28 -0
- data/manual/text/win_ansi_charset.rb +59 -0
- data/prawn.gemspec +46 -0
- data/spec/annotations_spec.rb +90 -0
- data/spec/bounding_box_spec.rb +493 -0
- data/spec/cell_spec.rb +584 -0
- data/spec/column_box_spec.rb +33 -0
- data/spec/data/curves.pdf +66 -0
- data/spec/destinations_spec.rb +15 -0
- data/spec/document_spec.rb +736 -0
- data/spec/extensions/encoding_helpers.rb +6 -0
- data/spec/extensions/mocha.rb +44 -0
- data/spec/font_spec.rb +433 -0
- data/spec/formatted_text_arranger_spec.rb +421 -0
- data/spec/formatted_text_box_spec.rb +640 -0
- data/spec/formatted_text_fragment_spec.rb +298 -0
- data/spec/graphics_spec.rb +651 -0
- data/spec/grid_spec.rb +85 -0
- data/spec/images_spec.rb +140 -0
- data/spec/inline_formatted_text_parser_spec.rb +515 -0
- data/spec/jpg_spec.rb +25 -0
- data/spec/line_wrap_spec.rb +333 -0
- data/spec/measurement_units_spec.rb +23 -0
- data/spec/name_tree_spec.rb +112 -0
- data/spec/object_store_spec.rb +170 -0
- data/spec/outline_spec.rb +426 -0
- data/spec/pdf_object_spec.rb +172 -0
- data/spec/png_spec.rb +240 -0
- data/spec/reference_spec.rb +105 -0
- data/spec/repeater_spec.rb +158 -0
- data/spec/security_spec.rb +126 -0
- data/spec/snapshot_spec.rb +186 -0
- data/spec/soft_mask_spec.rb +117 -0
- data/spec/span_spec.rb +49 -0
- data/spec/spec_helper.rb +36 -0
- data/spec/stamp_spec.rb +159 -0
- data/spec/stroke_styles_spec.rb +193 -0
- data/spec/table_spec.rb +1209 -0
- data/spec/template_spec.rb +351 -0
- data/spec/text_at_spec.rb +129 -0
- data/spec/text_box_spec.rb +1029 -0
- data/spec/text_rendering_mode_spec.rb +45 -0
- data/spec/text_spacing_spec.rb +93 -0
- data/spec/text_spec.rb +421 -0
- data/spec/text_with_inline_formatting_spec.rb +35 -0
- data/spec/transparency_spec.rb +89 -0
- metadata +544 -0
@@ -0,0 +1,122 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
# repeater.rb : Implements repeated page elements.
|
4
|
+
# Heavy inspired by repeating_element() in PDF::Wrapper
|
5
|
+
# http://pdf-wrapper.rubyforge.org/
|
6
|
+
#
|
7
|
+
# Copyright November 2009, Gregory Brown. All Rights Reserved.
|
8
|
+
#
|
9
|
+
# This is free software. Please see the LICENSE and COPYING files for details.
|
10
|
+
|
11
|
+
module Prawn
|
12
|
+
|
13
|
+
class Document
|
14
|
+
|
15
|
+
# A list of all repeaters in the document.
|
16
|
+
# See Document#repeat for details
|
17
|
+
#
|
18
|
+
def repeaters
|
19
|
+
@repeaters ||= []
|
20
|
+
end
|
21
|
+
|
22
|
+
# Provides a way to execute a block of code repeatedly based on a
|
23
|
+
# page_filter. Since Stamp is used under the hood, this method is very space
|
24
|
+
# efficient.
|
25
|
+
#
|
26
|
+
# Available page filters are:
|
27
|
+
# :all -- repeats on every page
|
28
|
+
# :odd -- repeats on odd pages
|
29
|
+
# :even -- repeats on even pages
|
30
|
+
# some_array -- repeats on every page listed in the array
|
31
|
+
# some_range -- repeats on every page included in the range
|
32
|
+
# some_lambda -- yields page number and repeats for true return values
|
33
|
+
#
|
34
|
+
# Also accepts an optional second argument for dynamic content which executes the code
|
35
|
+
# in the context of the filtered pages without using a Stamp.
|
36
|
+
#
|
37
|
+
# Example:
|
38
|
+
#
|
39
|
+
# Prawn::Document.generate("repeat.pdf", :skip_page_creation => true) do
|
40
|
+
#
|
41
|
+
# repeat :all do
|
42
|
+
# draw_text "ALLLLLL", :at => bounds.top_left
|
43
|
+
# end
|
44
|
+
#
|
45
|
+
# repeat :odd do
|
46
|
+
# draw_text "ODD", :at => [0,0]
|
47
|
+
# end
|
48
|
+
#
|
49
|
+
# repeat :even do
|
50
|
+
# draw_text "EVEN", :at => [0,0]
|
51
|
+
# end
|
52
|
+
#
|
53
|
+
# repeat [1,2] do
|
54
|
+
# draw_text "[1,2]", :at => [100,0]
|
55
|
+
# end
|
56
|
+
#
|
57
|
+
# repeat 2..4 do
|
58
|
+
# draw_text "2..4", :at => [200,0]
|
59
|
+
# end
|
60
|
+
#
|
61
|
+
# repeat(lambda { |pg| pg % 3 == 0 }) do
|
62
|
+
# draw_text "Every third", :at => [250, 20]
|
63
|
+
# end
|
64
|
+
#
|
65
|
+
# 10.times do
|
66
|
+
# start_new_page
|
67
|
+
# draw_text "A wonderful page", :at => [400,400]
|
68
|
+
# end
|
69
|
+
#
|
70
|
+
# repeat(:all, :dynamic => true) do
|
71
|
+
# text page_number, :at => [500, 0]
|
72
|
+
# end
|
73
|
+
#
|
74
|
+
# end
|
75
|
+
#
|
76
|
+
def repeat(page_filter, options={}, &block)
|
77
|
+
repeaters << Prawn::Repeater.new(self, page_filter, !!options[:dynamic], &block)
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
class Repeater #:nodoc:
|
82
|
+
class << self
|
83
|
+
attr_writer :count
|
84
|
+
|
85
|
+
def count
|
86
|
+
@count ||= 0
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
attr_reader :name
|
91
|
+
|
92
|
+
def initialize(document, page_filter, dynamic = false, &block)
|
93
|
+
@document = document
|
94
|
+
@page_filter = page_filter
|
95
|
+
@dynamic = dynamic
|
96
|
+
@stamp_name = "prawn_repeater(#{Repeater.count})"
|
97
|
+
@document.create_stamp(@stamp_name, &block) unless dynamic
|
98
|
+
@block = block if dynamic
|
99
|
+
@graphic_state = document.state.page.graphic_state.dup
|
100
|
+
|
101
|
+
Repeater.count += 1
|
102
|
+
end
|
103
|
+
|
104
|
+
def match?(page_number)
|
105
|
+
@document.page_match?(@page_filter, page_number)
|
106
|
+
end
|
107
|
+
|
108
|
+
def run(page_number)
|
109
|
+
if !@dynamic
|
110
|
+
@document.stamp(@stamp_name) if match?(page_number)
|
111
|
+
elsif @block && match?(page_number)
|
112
|
+
@document.save_graphics_state(@graphic_state) do
|
113
|
+
@document.send(:freeze_stamp_graphics)
|
114
|
+
@block.call
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
end
|
120
|
+
end
|
121
|
+
|
122
|
+
|
@@ -0,0 +1,269 @@
|
|
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 'rc4'
|
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_contents</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
|
+
RC4.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
|
+
unless PermissionsBits[key]
|
146
|
+
raise ArgumentError, "Unknown permission :#{key}. Valid options: " +
|
147
|
+
PermissionsBits.keys.map { |k| k.inspect }.join(", ")
|
148
|
+
end
|
149
|
+
|
150
|
+
# 0-based bit number, from LSB
|
151
|
+
bit_position = PermissionsBits[key] - 1
|
152
|
+
|
153
|
+
if value # set bit
|
154
|
+
@permissions |= (1 << bit_position)
|
155
|
+
else # clear bit
|
156
|
+
@permissions &= ~(1 << bit_position)
|
157
|
+
end
|
158
|
+
end
|
159
|
+
end
|
160
|
+
|
161
|
+
def permissions_value
|
162
|
+
@permissions || FullPermissions
|
163
|
+
end
|
164
|
+
|
165
|
+
PasswordPadding =
|
166
|
+
"28BF4E5E4E758A4164004E56FFFA01082E2E00B6D0683E802F0CA9FE6453697A".
|
167
|
+
scan(/../).map{|x| x.to_i(16)}.pack("c*")
|
168
|
+
|
169
|
+
# Pads or truncates a password to 32 bytes as per Alg 3.2.
|
170
|
+
def pad_password(password)
|
171
|
+
password = password[0, 32]
|
172
|
+
password + PasswordPadding[0, 32 - password.length]
|
173
|
+
end
|
174
|
+
|
175
|
+
def user_encryption_key
|
176
|
+
@user_encryption_key ||= begin
|
177
|
+
md5 = Digest::MD5.new
|
178
|
+
md5 << pad_password(@user_password)
|
179
|
+
md5 << owner_password_hash
|
180
|
+
md5 << [permissions_value].pack("V")
|
181
|
+
md5.digest[0, 5]
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
# The O (owner) value in the encryption dictionary. Algorithm 3.3.
|
186
|
+
def owner_password_hash
|
187
|
+
@owner_password_hash ||= begin
|
188
|
+
key = Digest::MD5.digest(pad_password(@owner_password))[0, 5]
|
189
|
+
RC4.new(key).encrypt(pad_password(@user_password))
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
# The U (user) value in the encryption dictionary. Algorithm 3.4.
|
194
|
+
def user_password_hash
|
195
|
+
RC4.new(user_encryption_key).encrypt(PasswordPadding)
|
196
|
+
end
|
197
|
+
|
198
|
+
end
|
199
|
+
|
200
|
+
end
|
201
|
+
|
202
|
+
module Core #:nodoc:
|
203
|
+
module_function
|
204
|
+
|
205
|
+
# Like PdfObject, but returns an encrypted result if required.
|
206
|
+
# For direct objects, requires the object identifier and generation number
|
207
|
+
# from the indirect object referencing obj.
|
208
|
+
def EncryptedPdfObject(obj, key, id, gen, in_content_stream=false)
|
209
|
+
case obj
|
210
|
+
when Array
|
211
|
+
"[" << obj.map { |e|
|
212
|
+
EncryptedPdfObject(e, key, id, gen, in_content_stream)
|
213
|
+
}.join(' ') << "]"
|
214
|
+
when LiteralString
|
215
|
+
# FIXME: encrypted?
|
216
|
+
obj = obj.gsub(/[\\\n\(\)]/) { |m| "\\#{m}" }
|
217
|
+
"(#{obj})"
|
218
|
+
when Time
|
219
|
+
# FIXME: encrypted?
|
220
|
+
obj = obj.strftime("D:%Y%m%d%H%M%S%z").chop.chop + "'00'"
|
221
|
+
obj = obj.gsub(/[\\\n\(\)]/) { |m| "\\#{m}" }
|
222
|
+
"(#{obj})"
|
223
|
+
when String
|
224
|
+
PdfObject(
|
225
|
+
ByteString.new(
|
226
|
+
Document::Security.encrypt_string(obj, key, id, gen)),
|
227
|
+
in_content_stream)
|
228
|
+
when Hash
|
229
|
+
output = "<< "
|
230
|
+
obj.each do |k,v|
|
231
|
+
unless String === k || Symbol === k
|
232
|
+
raise Prawn::Errors::FailedObjectConversion,
|
233
|
+
"A PDF Dictionary must be keyed by names"
|
234
|
+
end
|
235
|
+
output << PdfObject(k.to_sym, in_content_stream) << " " <<
|
236
|
+
EncryptedPdfObject(v, key, id, gen, in_content_stream) << "\n"
|
237
|
+
end
|
238
|
+
output << ">>"
|
239
|
+
when NameTree::Value
|
240
|
+
PdfObject(obj.name) + " " +
|
241
|
+
EncryptedPdfObject(obj.value, key, id, gen, in_content_stream)
|
242
|
+
when Prawn::OutlineRoot, Prawn::OutlineItem
|
243
|
+
EncryptedPdfObject(obj.to_hash, key, id, gen, in_content_stream)
|
244
|
+
else # delegate back to PdfObject
|
245
|
+
PdfObject(obj, in_content_stream)
|
246
|
+
end
|
247
|
+
end
|
248
|
+
|
249
|
+
class Reference
|
250
|
+
|
251
|
+
# Returns the object definition for the object this references, keyed from
|
252
|
+
# +key+.
|
253
|
+
def encrypted_object(key)
|
254
|
+
@on_encode.call(self) if @on_encode
|
255
|
+
output = "#{@identifier} #{gen} obj\n" <<
|
256
|
+
Prawn::Core::EncryptedPdfObject(data, key, @identifier, gen) << "\n"
|
257
|
+
if @stream
|
258
|
+
output << "stream\n" <<
|
259
|
+
Document::Security.encrypt_string(@stream, key, @identifier, gen) <<
|
260
|
+
"\nendstream\n"
|
261
|
+
end
|
262
|
+
output << "endobj\n"
|
263
|
+
end
|
264
|
+
|
265
|
+
end
|
266
|
+
end
|
267
|
+
|
268
|
+
end
|
269
|
+
|
@@ -0,0 +1,94 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
#
|
3
|
+
# soft_mask.rb : Implements soft-masking
|
4
|
+
#
|
5
|
+
# Copyright September 2012, Alexander Mankuta. 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::SoftMask module is used to create arbitrary transparency in
|
13
|
+
# document. Using a soft mask allows creaing more visually rich documents.
|
14
|
+
#
|
15
|
+
# You must group soft mask and graphics it's applied to under
|
16
|
+
# save_graphics_state because soft mask is a part of graphic state in PDF.
|
17
|
+
#
|
18
|
+
# Example:
|
19
|
+
# pdf.save_graphics_state do
|
20
|
+
# pdf.soft_mask do
|
21
|
+
# pdf.fill_color "444444"
|
22
|
+
# pdf.fill_polygon [0, 40], [60, 10], [120, 40], [60, 68]
|
23
|
+
# end
|
24
|
+
# pdf.fill_color '000000'
|
25
|
+
# pdf.fill_rectangle [0, 50], 120, 68
|
26
|
+
# end
|
27
|
+
#
|
28
|
+
module SoftMask
|
29
|
+
def soft_mask(&block)
|
30
|
+
min_version(1.4)
|
31
|
+
|
32
|
+
group_attrs = ref!({
|
33
|
+
:Type => :Group,
|
34
|
+
:S => :Transparency,
|
35
|
+
:CS => :DeviceRGB,
|
36
|
+
:I => false,
|
37
|
+
:K => false
|
38
|
+
})
|
39
|
+
|
40
|
+
group = ref!({
|
41
|
+
:Type => :XObject,
|
42
|
+
:Subtype => :Form,
|
43
|
+
:BBox => state.page.dimensions,
|
44
|
+
:Group => group_attrs,
|
45
|
+
})
|
46
|
+
|
47
|
+
state.page.stamp_stream(group, &block)
|
48
|
+
|
49
|
+
mask = ref!({
|
50
|
+
:Type => :Mask,
|
51
|
+
:S => :Luminosity,
|
52
|
+
:G => group
|
53
|
+
})
|
54
|
+
|
55
|
+
g_state = ref!({
|
56
|
+
:Type => :ExtGState,
|
57
|
+
:SMask => mask,
|
58
|
+
|
59
|
+
:AIS => false,
|
60
|
+
:BM => :Normal,
|
61
|
+
:OP => false,
|
62
|
+
:op => false,
|
63
|
+
:OPM => 1,
|
64
|
+
:SA => true,
|
65
|
+
})
|
66
|
+
|
67
|
+
registry_key = {
|
68
|
+
:bbox => state.page.dimensions,
|
69
|
+
:mask => group.stream,
|
70
|
+
:page => state.page_count,
|
71
|
+
}.hash
|
72
|
+
|
73
|
+
if soft_mask_registry[registry_key]
|
74
|
+
[g_state, mask, group, group_attrs].each { |ref| ref.live = false }
|
75
|
+
|
76
|
+
add_content "/#{soft_mask_registry[registry_key]} gs"
|
77
|
+
else
|
78
|
+
masks = page.resources[:ExtGState] ||= {}
|
79
|
+
id = masks.empty? ? 'GS1' : masks.keys.sort.last.succ
|
80
|
+
masks[id] = g_state
|
81
|
+
|
82
|
+
soft_mask_registry[registry_key] = id
|
83
|
+
|
84
|
+
add_content "/#{id} gs"
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
private
|
89
|
+
|
90
|
+
def soft_mask_registry
|
91
|
+
@soft_mask_registry ||= {}
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|