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

Allow user to preserve PDF names #163

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
4 changes: 2 additions & 2 deletions lib/combine_pdf/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ module CombinePDF
def load(file_name = '', options = {})
raise TypeError, "couldn't parse data, expecting type String" unless file_name.is_a?(String) || file_name.is_a?(Pathname)
return PDF.new if file_name == ''
PDF.new(PDFParser.new(IO.read(file_name, mode: 'rb').force_encoding(Encoding::ASCII_8BIT), options))
PDF.new(PDFParser.new(IO.read(file_name, mode: 'rb').force_encoding(Encoding::ASCII_8BIT), options), preserve_names: options[:preserve_names])
end

# creats a new PDF object.
Expand Down Expand Up @@ -37,7 +37,7 @@ def new(string = false)
# data:: is a string that represents the content of a PDF file.
def parse(data, options = {})
raise TypeError, "couldn't parse and data, expecting type String" unless data.is_a? String
PDF.new(PDFParser.new(data, options))
PDF.new(PDFParser.new(data, options), preserve_names: options[:preserve_names])
end

# makes a PDFWriter object
Expand Down
10 changes: 9 additions & 1 deletion lib/combine_pdf/pdf_protected.rb
Original file line number Diff line number Diff line change
Expand Up @@ -198,8 +198,16 @@ def rebuild_names(name_tree = nil, base = 'CombinePDF_0000000')
if pos.is_a? Array
next if resolved.include?(pos.object_id)
if pos[0].is_a? String

# Names should sorted in ascending,
# so re-sort is needed if preserving names.
pos = pos.group_by.with_index{|_, i| i/2 }.values.sort_by(&:first).flatten(1) if @preserve_names

(pos.length / 2).times do |i|
dic << (pos[i * 2].clear << base.next!)
# According preserve_names value decide changing or not
pos[i * 2].clear << base.next! unless @preserve_names
dic << pos[i * 2]

pos[(i * 2) + 1][0] = {is_reference_only: true, referenced_object: pages[pos[(i * 2) + 1][0]]} if(pos[(i * 2) + 1].is_a?(Array) && pos[(i * 2) + 1][0].is_a?(Numeric))
dic << (pos[(i * 2) + 1].is_a?(Array) ? { is_reference_only: true, referenced_object: { indirect_without_dictionary: pos[(i * 2) + 1] } } : pos[(i * 2) + 1])
# dic << pos[(i * 2) + 1]
Expand Down
9 changes: 8 additions & 1 deletion lib/combine_pdf/pdf_public.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,10 @@ class PDF
attr_reader :outlines
# Access the Names PDF object Hash (or reference). Use with care.
attr_reader :names
# For preserve original names
attr_reader :preserve_names

def initialize(parser = nil)
def initialize(parser = nil, options = {})
# default before setting
@objects = []
@version = 0
Expand All @@ -104,6 +106,10 @@ def initialize(parser = nil)
@names = parser.names_object || {}
@forms_data = parser.forms_object || {}
@outlines = parser.outlines_object || {}

# Default is to change all names
@preserve_names = options[:preserve_names] || false

# rebuild the catalog, to fix wkhtmltopdf's use of static page numbers
rebuild_catalog

Expand Down Expand Up @@ -304,6 +310,7 @@ def >>(data)
def insert(location, data)
pages_to_add = nil
if data.is_a? PDF
@preserve_names = data.preserve_names
@version = [@version, data.version].max
pages_to_add = data.pages
actual_value(@names ||= {}.dup).update data.names, &HASH_MERGE_NEW_NO_PAGE
Expand Down