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 15 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
46 changes: 46 additions & 0 deletions lib/prawn/embedded_files.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# 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)
mut_opts = options.dup

if path.directory?
raise ArgumentError, 'Data source can\'t be a directory'
elsif path.file?
data = path.read
mut_opts[:name] ||= src
mut_opts[:creation_date] ||= path.birthtime
mut_opts[:modification_date] ||= path.mtime
else
data = src
end

@file_registry ||= {}

file = EmbeddedFile.new(data, mut_opts)
file_obj = @file_registry[file.checksum]

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

filespec = Filespec.new(file_obj, mut_opts)
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'
46 changes: 46 additions & 0 deletions lib/prawn/embedded_files/embedded_file.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# frozen_string_literal: true

require 'digest/md5'

module Prawn
module EmbeddedFiles
class EmbeddedFile
attr_reader :checksum

def initialize(data, options = {})
@creation_date = options[:creation_date]
unless @creation_date.kind_of?(Time)
@creation_date = Time.now.utc
end

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

@checksum = Digest::MD5.digest(data)
@data = data
end

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

obj << data
obj.stream.compress! if document.compression_enabled?
obj
end

private

attr_reader :data, :creation_date, :modification_date
end
end
end
43 changes: 43 additions & 0 deletions lib/prawn/embedded_files/filespec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# frozen_string_literal: true

module Prawn
module EmbeddedFiles
class Filespec
attr_reader :file_name

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

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

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

@hidden = options[:hidden]
@file = file
end

def hidden?
@hidden
end

def build_pdf_object(document)
obj = document.ref!(
Type: :Filespec,
F: file_name,
EF: { F: file },
dvdalilue marked this conversation as resolved.
Show resolved Hide resolved
UF: file_name
)

obj.data[:Desc] = description if description
obj
end

private

attr_reader :file, :description
end
end
end