lib/hexapdf/cli/modify.rb in hexapdf-0.3.0 vs lib/hexapdf/cli/modify.rb in hexapdf-0.4.0
- old
+ new
@@ -75,20 +75,16 @@
define_encryption_options
end
def execute(in_file, out_file) #:nodoc:
maybe_raise_on_existing_file(out_file)
- HexaPDF::Document.open(in_file, decryption_opts: {password: @password}) do |doc|
+ with_document(in_file, password: @password, out_file: out_file) do |doc|
arrange_pages(doc) unless @pages == '1-e'
@embed_files.each {|file| doc.files.add(file, embed: true)}
apply_encryption_options(doc)
apply_optimization_options(doc)
- doc.write(out_file)
end
- rescue HexaPDF::Error => e
- $stderr.puts "Processing error : #{e.message}"
- exit(1)
end
private
# Arranges the pages of the document as specified with the --pages option.
@@ -98,16 +94,17 @@
parse_pages_specification(@pages, all_pages.length).each do |index, rotation|
page = all_pages[index]
page.value.update(page.copy_inherited_values)
if rotation == :none
page.delete(:Rotate)
- else
+ elsif rotation.kind_of?(Integer)
page[:Rotate] = ((page[:Rotate] || 0) + rotation) % 360
end
new_page_tree.add_page(page)
end
- doc.delete(doc.catalog.delete(:Pages))
doc.catalog[:Pages] = new_page_tree
+ remove_unused_pages(doc)
+ doc.pages.add unless doc.pages.count > 0
end
end
end