-
Notifications
You must be signed in to change notification settings - Fork 699
Embedded file support for Prawn documents #1214
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
Open
dvdalilue
wants to merge
20
commits into
prawnpdf:master
Choose a base branch
from
dvdalilue:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 6 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
3ecc701
EmbeddedFile PDF object definition
dvdalilue 375f85e
Filespec PDF object definition
dvdalilue 83a0642
Document file attachment method definition
dvdalilue 9461d98
EmbeddedFiles module included in Prawn::Document class and lib
dvdalilue 0fb2b56
Creation and modification dates from file information
dvdalilue 334c19a
:hidden option simplification
dvdalilue 5680677
:hidden option could be a truthy value
dvdalilue 6796763
Look for the Time class in the object's superclasses as well
dvdalilue ca63634
Using MD5 digest instead of the hex-encoded one
dvdalilue b96039b
Avoiding wasteful duplication
dvdalilue f41ed2c
Checksum attribute renamed
dvdalilue f0c0453
Filespec description key set wrongly fixed
dvdalilue a6343c1
Avoiding instance variables outside method
dvdalilue fe64860
Honoring document compression option
dvdalilue 8d4e047
Avoiding changes of parameters
dvdalilue 484d701
Using hex-encoded digest as default name
dvdalilue 097ee85
Setting 'UF' entry in the 'EF' dictionary as well
dvdalilue 8f09896
'file' method documentation
dvdalilue 5d3011e
File registration refactor
dvdalilue 3714d59
'is_a?' is preferable to 'kind_of?'
dvdalilue File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| # 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 = {}) | ||
| 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.
Outdated
Show resolved
Hide resolved
pointlessone marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| options[:creation_date] ||= path.birthtime | ||
| options[:modification_date] ||= path.mtime | ||
| 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' | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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.
Outdated
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.
Outdated
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.
Outdated
Show resolved
Hide resolved
|
||
| @data = data.dup | ||
dvdalilue marked this conversation as resolved.
Outdated
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.
Outdated
Show resolved
Hide resolved
|
||
| obj | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| # frozen_string_literal: true | ||
|
|
||
| module Prawn | ||
| module EmbeddedFiles | ||
| class Filespec | ||
| attr_reader :file_name | ||
|
|
||
| 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 | ||
|
|
||
| @hidden = options[:hidden] == true | ||
| @file = file | ||
| end | ||
|
|
||
| def hidden? | ||
| @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.
Outdated
Show resolved
Hide resolved
pointlessone marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| obj | ||
| end | ||
| end | ||
| end | ||
| end | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.