Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Embedded file support for Prawn documents #1214

Open
wants to merge 20 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/prawn.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def verify_options(accepted, actual)
require_relative 'prawn/images/image'
require_relative 'prawn/images/jpg'
require_relative 'prawn/images/png'
require_relative 'prawn/embedded_files'
require_relative 'prawn/stamp'
require_relative 'prawn/soft_mask'
require_relative 'prawn/security'
Expand Down
1 change: 1 addition & 0 deletions lib/prawn/document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class Document
include Prawn::Text
include Prawn::Graphics
include Prawn::Images
include Prawn::EmbeddedFiles
include Prawn::Stamp
include Prawn::SoftMask
include Prawn::TransformationStack
Expand Down
43 changes: 43 additions & 0 deletions lib/prawn/embedded_files.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# frozen_string_literal: true

module Prawn
module EmbeddedFiles
# include PDF::Core::EmbeddedFiles
# Link to PR (https://github.com/prawnpdf/pdf-core/pull/47)

def file(src, options = {})
dvdalilue marked this conversation as resolved.
Show resolved Hide resolved
path = Pathname.new(src)

if path.directory?
raise ArgumentError, 'Data source can\'t be a directory'
elsif path.file?
data = path.read
options[:name] ||= src
dvdalilue marked this conversation as resolved.
Show resolved Hide resolved
pointlessone marked this conversation as resolved.
Show resolved Hide resolved
else
data = src
end

@file_registry ||= {}

file = EmbeddedFile.new(data, options)
file_obj = @file_registry[file.chksum]

if file_obj.nil?
file_obj = file.build_pdf_object(self)
@file_registry[file.chksum] = file_obj
end

filespec = Filespec.new(file_obj, options)
filespec_obj = filespec.build_pdf_object(self)

unless filespec.hidden
# Wait for pdf-core PR

# attach_file(filespec.file_name, filespec_obj)
end
end
end
end

require_relative 'embedded_files/embedded_file'
require_relative 'embedded_files/filespec'
42 changes: 42 additions & 0 deletions lib/prawn/embedded_files/embedded_file.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# frozen_string_literal: true

require 'digest/md5'

module Prawn
module EmbeddedFiles
class EmbeddedFile
attr_reader :chksum
pointlessone marked this conversation as resolved.
Show resolved Hide resolved

def initialize(data, options = {})
@creation_date = options[:creation_date]
unless @creation_date.instance_of?(Time)
dvdalilue marked this conversation as resolved.
Show resolved Hide resolved
@creation_date = Time.now.utc
end

@mod_date = options[:modification_date]
unless @mod_date.instance_of?(Time)
@mod_date = Time.now.utc
end

@chksum = Digest::MD5.hexdigest(data)
dvdalilue marked this conversation as resolved.
Show resolved Hide resolved
@data = data.dup
dvdalilue marked this conversation as resolved.
Show resolved Hide resolved
end

def build_pdf_object(document)
obj = document.ref!(
Type: :EmbeddedFile,
Params: {
CreationDate: @creation_date,
ModDate: @mod_date,
CheckSum: PDF::Core::LiteralString.new(@chksum),
Size: @data.length
}
)

obj << @data
obj.stream.compress!
pointlessone marked this conversation as resolved.
Show resolved Hide resolved
obj
end
end
end
end
42 changes: 42 additions & 0 deletions lib/prawn/embedded_files/filespec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# frozen_string_literal: true

module Prawn
module EmbeddedFiles
class Filespec
attr_reader :file_name, :not_hidden

def initialize(file, options = {})
name = options[:name] || file.chksum

@file_name = PDF::Core::LiteralString.new(name)

if options[:description]
desc_str = options[:description].to_s
@description = PDF::Core::LiteralString.new(desc_str)
end

if options[:hidden].nil? || options[:hidden] != true
@not_hidden = true
end
dvdalilue marked this conversation as resolved.
Show resolved Hide resolved

@file = file
end

def hidden
!@not_hidden
end

def build_pdf_object(document)
obj = document.ref!(
Type: :Filespec,
F: @file_name,
EF: { F: @file },
UF: @file_name
)

obj[:Desc] = @description if @description
pointlessone marked this conversation as resolved.
Show resolved Hide resolved
pointlessone marked this conversation as resolved.
Show resolved Hide resolved
obj
end
end
end
end