-
Notifications
You must be signed in to change notification settings - Fork 38
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
Add support for PDF/A-1b #34
Open
Backbone81
wants to merge
10
commits into
prawnpdf:master
Choose a base branch
from
Backbone81:add_support_for_pdfa_1b
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.
+390
−13
Open
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
033eaa5
Added support for PDF/A-1b
Backbone81 c94f468
Moved some ignores from local ignore file to global ignore file.
Backbone81 0cae866
Made trailer ID deterministic
Backbone81 815c711
Trailer ID generation does not use seek any more.
Backbone81 65532fb
Replaced nokogiri with string interpolation for XMP metadata generation
Backbone81 b60414d
Moved ICC profile to data directory.
Backbone81 ada2c8e
Code cleanup including rubocop warnings
Backbone81 c6f3661
XMP metadata is only added to PDF/A-1b compliant documents
Backbone81 1ca0ac9
Added XMP packet wrapper
Backbone81 21ecc4e
Added missing timezone information to XMP timestamps
Backbone81 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
Binary file not shown.
This file contains 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 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 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 |
---|---|---|
|
@@ -19,6 +19,20 @@ def initialize(opts = {}) | |
|
||
@info ||= ref(opts[:info] || {}).identifier | ||
@root ||= ref(Type: :Catalog).identifier | ||
|
||
if opts[:enable_pdfa_1b] | ||
# PDF/A-1b requirement: XMP metadata | ||
@xmp_metadata ||= ref(Type: :Metadata, Subtype: :XML).identifier | ||
root.data[:Metadata] = xmp_metadata | ||
xmp_metadata_content = XmpMetadata.new(opts[:info] || {}) | ||
xmp_metadata_content.enable_pdfa_1b = true | ||
xmp_metadata.stream = Stream.new | ||
xmp_metadata.stream << xmp_metadata_content.render | ||
|
||
# PDF/A-1b requirement: OutputIntent with ICC profile stream | ||
initialize_output_intent | ||
end | ||
|
||
if opts[:print_scaling] == :none | ||
root.data[:ViewerPreferences] = { PrintScaling: :None } | ||
end | ||
|
@@ -39,6 +53,10 @@ def root | |
@objects[@root] | ||
end | ||
|
||
def xmp_metadata | ||
@objects[@xmp_metadata] | ||
end | ||
|
||
def pages | ||
root.data[:Pages] | ||
end | ||
|
@@ -96,6 +114,24 @@ def object_id_for_page(k) | |
flat_page_ids = get_page_objects(pages).flatten | ||
flat_page_ids[k] | ||
end | ||
|
||
private | ||
|
||
def initialize_output_intent | ||
icc_profile_name = 'sRGB2014.icc'.freeze | ||
|
||
icc_profile_stream = ref(N: 3) | ||
icc_profile_stream.stream = Stream.new | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is not needed. |
||
icc_profile_stream << File.binread(File.join(File.dirname(__FILE__), '..', '..', '..', 'data', icc_profile_name)) | ||
|
||
root.data[:OutputIntents] = [{ | ||
Type: :OutputIntent, | ||
S: :GTS_PDFA1, | ||
OutputConditionIdentifier: LiteralString.new('Custom'), | ||
Info: LiteralString.new(File.basename(icc_profile_name, '.*')), | ||
DestOutputProfile: icc_profile_stream | ||
}] | ||
end | ||
end | ||
end | ||
end |
This file contains 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 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,95 @@ | ||
module PDF | ||
module Core | ||
class XmpMetadata | ||
attr_accessor :enable_pdfa_1b | ||
|
||
# These attributes must all be synchronized with their counterparts in the | ||
# document information dictionary to be PDF/A-1b compliant. | ||
attr_accessor :dc_title, :dc_creator, :dc_description, | ||
:pdf_keywords, :xmp_creator_tool, :pdf_producer, | ||
:xmp_create_date, :xmp_modify_date | ||
|
||
def initialize(options = {}) | ||
# Convert options for the document information dictionary to their | ||
# counterparts in XMP. | ||
@dc_title = options[:Title] if options[:Title] | ||
@dc_creator = options[:Author] if options[:Author] | ||
@dc_description = options[:Subject] if options[:Subject] | ||
@pdf_keywords = options[:Keywords] if options[:Keywords] | ||
@xmp_creator_tool = options[:Creator] if options[:Creator] | ||
@pdf_producer = options[:Producer] if options[:Producer] | ||
@xmp_create_date = options[:CreationDate] if options[:CreationDate] | ||
@xmp_modify_date = options[:ModDate] if options[:ModDate] | ||
end | ||
|
||
def render | ||
result = "<?xpacket begin=\"\" id=\"W5M0MpCehiHzreSzNTczkc9d\"?>\n" | ||
result << "<rdf:RDF xmlns:rdf=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\">\n" | ||
result << render_pdfaid if @enable_pdfa_1b | ||
result << render_xmp if @xmp_creator_tool || @xmp_create_date || @xmp_modify_date | ||
result << render_pdf if @pdf_keywords || @pdf_producer | ||
result << render_dc if @dc_title || @dc_creator || @dc_description | ||
result << "</rdf:RDF>\n" | ||
result << '<?xpacket end="r"?>' | ||
end | ||
|
||
private | ||
|
||
def render_pdfaid | ||
" <rdf:Description xmlns:pdfaid=\"http://www.aiim.org/pdfa/ns/id/\" rdf:about=\"\">\n" \ | ||
" <pdfaid:part>1</pdfaid:part>\n" \ | ||
" <pdfaid:conformance>B</pdfaid:conformance>\n" \ | ||
" </rdf:Description>\n" | ||
end | ||
|
||
def render_xmp | ||
result = " <rdf:Description xmlns:xmp=\"http://ns.adobe.com/xap/1.0/\" rdf:about=\"\">\n" | ||
result << " <xmp:CreatorTool>#{xml_char_data(@xmp_creator_tool)}</xmp:CreatorTool>\n" if @xmp_creator_tool | ||
result << " <xmp:CreateDate>#{xml_char_data(to_xmp_timestamp(@xmp_create_date))}</xmp:CreateDate>\n" if @xmp_create_date | ||
result << " <xmp:ModifyDate>#{xml_char_data(to_xmp_timestamp(@xmp_modify_date))}</xmp:ModifyDate>\n" if @xmp_modify_date | ||
result << " </rdf:Description>\n" | ||
end | ||
|
||
def render_pdf | ||
result = " <rdf:Description xmlns:pdf=\"http://ns.adobe.com/pdf/1.3/\" rdf:about=\"\">\n" | ||
result << " <pdf:Keywords>#{xml_char_data(@pdf_keywords)}</pdf:Keywords>\n" if @pdf_keywords | ||
result << " <pdf:Producer>#{xml_char_data(@pdf_producer)}</pdf:Producer>\n" if @pdf_producer | ||
result << " </rdf:Description>\n" | ||
end | ||
|
||
def render_dc | ||
result = " <rdf:Description xmlns:dc=\"http://purl.org/dc/elements/1.1/\" rdf:about=\"\">\n" | ||
if @dc_title | ||
result << " <dc:title>\n" | ||
result << " <rdf:Alt>\n" | ||
result << " <rdf:li xml:lang=\"x-default\">#{xml_char_data(@dc_title)}</rdf:li>\n" | ||
result << " </rdf:Alt>\n" | ||
result << " </dc:title>\n" | ||
end | ||
if @dc_creator | ||
result << " <dc:creator>\n" | ||
result << " <rdf:Seq>\n" | ||
result << " <rdf:li>#{xml_char_data(@dc_creator)}</rdf:li>\n" | ||
result << " </rdf:Seq>\n" | ||
result << " </dc:creator>\n" | ||
end | ||
if @dc_description | ||
result << " <dc:description>\n" | ||
result << " <rdf:Alt>\n" | ||
result << " <rdf:li xml:lang=\"x-default\">#{xml_char_data(@dc_description)}</rdf:li>\n" | ||
result << " </rdf:Alt>\n" | ||
result << " </dc:description>\n" | ||
end | ||
result << " </rdf:Description>\n" \ | ||
end | ||
|
||
def to_xmp_timestamp(time) | ||
time.strftime('%Y-%m-%dT%H:%M:%S%:z') | ||
end | ||
|
||
def xml_char_data(string) | ||
string.gsub('&', '&').gsub('<', '<') | ||
end | ||
end | ||
end | ||
end |
This file contains 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
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
xmp_metadata
is a reference. It's smart enough to create a stream when needed so this can be a bit simpler: