Skip to content

Commit

Permalink
memory usage concerns, credit to @denislavski , as of yet unknown eff…
Browse files Browse the repository at this point in the history
…ects
  • Loading branch information
boazsegev committed Dec 19, 2023
1 parent fc763b6 commit 8eba750
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 27 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Change Log

#### Change log v.1.0.25 (2023-12-19)

**Fix**: possible improve memory usage. Credit to @denislavski (Denislav Naydenov) for opening PR #223 and suggesting this change.

#### Change log v.1.0.24 (2023-10-19)

**Fix**: possible `nil` in loop. Credit to @jkowens for PR #231 and adding a quick fix using a simple guard.
Expand Down
6 changes: 3 additions & 3 deletions lib/combine_pdf/pdf_public.rb
Original file line number Diff line number Diff line change
Expand Up @@ -206,9 +206,9 @@ def to_pdf(options = {})
xref_location = loc
# xref_location = 0
# out.each { |line| xref_location += line.bytesize + 1}
out << "xref\n0 #{indirect_object_count}\n0000000000 65535 f \n"
xref.each { |offset| out << (out.pop + ("%010d 00000 n \n" % offset)) }
out << out.pop + 'trailer'
out << "xref\n0 #{indirect_object_count}\n0000000000 65535 f "
xref.each { |offset| out << ("%010d 00000 n " % offset) }
out << 'trailer'.freeze
out << "<<\n/Root #{false || "#{catalog[:indirect_reference_id]} #{catalog[:indirect_generation_number]} R"}"
out << "/Size #{indirect_object_count}"
out << "/Info #{@info[:indirect_reference_id]} #{@info[:indirect_generation_number]} R"
Expand Down
48 changes: 24 additions & 24 deletions test/automated
Original file line number Diff line number Diff line change
Expand Up @@ -16,51 +16,51 @@ require 'combine_pdf'
# Pry.start

pdf = CombinePDF.load "../../test\ pdfs/filled_form.pdf"
pdf.save '01_check_radio_buttuns.pdf'
pdf.save '../tmp/01_check_radio_buttuns.pdf'
pdf = CombinePDF.load "../../test\ pdfs/filled_form.pdf"
pdf << CombinePDF.load("../../test\ pdfs/empty_form.pdf")
pdf << CombinePDF.load("../../test\ pdfs/filled_form.pdf")
pdf.save '02_check_form_unification_middle_is_empty.pdf'
pdf.save '../tmp/02_check_form_unification_middle_is_empty.pdf'

pdf = CombinePDF.load "../../test\ pdfs/check_form_data__objstreams_w_versions.pdf"
pdf.save '02_01_check_form_data_ordering_issue.pdf'
pdf.save '../tmp/02_01_check_form_data_ordering_issue.pdf'


pdf = CombinePDF.load '../../test pdfs/share-font-background.pdf'
pdf2 = CombinePDF.load '../../test pdfs/share-font-foreground.pdf'
i = 0
pdf.pages.each { |pg| pg << pdf2.pages[i] }
pdf.save '03_check_font_conflict.pdf'
pdf.save '../tmp/03_check_font_conflict.pdf'

pdf = CombinePDF.load '../../test pdfs/nil_1.pdf'
pdf2 = CombinePDF.load '../../test pdfs/nil_2.pdf'
pdf << pdf2
pdf.save '03_01_nil_value_conflict.pdf'
pdf.save '../tmp/03_01_nil_value_conflict.pdf'

pdf = CombinePDF.load '../../test pdfs/space_after_streram_keyword.pdf'
pdf.save '03_02_extra_space_after_stream_keyword.pdf'
pdf.save '../tmp/03_02_extra_space_after_stream_keyword.pdf'

pdf = CombinePDF.load '../../test pdfs/nested_difference.pdf'
pdf.save '03_03_nested_difference.pdf'
pdf.save '../tmp/03_03_nested_difference.pdf'

pdf = CombinePDF.load '../../test pdfs/names_go_haywire_0.pdf'
pdf << CombinePDF.load('../../test pdfs/names_go_haywire_1.pdf')
pdf.save '04_check_view_and_names_reference.pdf'
pdf.save '../tmp/04_check_view_and_names_reference.pdf'

pdf = CombinePDF.load('../../test pdfs/outlines/self_merge_err.pdf')
pdf.save '05_x1_scribus_test.pdf'
pdf.save '../tmp/05_x1_scribus_test.pdf'
pdf = CombinePDF.load('../../test pdfs/outlines/self_merge_err.pdf')
pdf << CombinePDF.load('../../test pdfs/outlines/self_merge_err.pdf')
pdf.save '05_x2_scribus_test.pdf'
pdf.save '../tmp/05_x2_scribus_test.pdf'
pdf = CombinePDF.load "../../test pdfs/outlines/named_dest.pdf";nil
pdf.save '05_check_named_dest_links.pdf' # this will take a while
pdf.save '../tmp/05_check_named_dest_links.pdf' # this will take a while
pdf = CombinePDF.load "../../test pdfs/outlines/named_dest.pdf";nil
pdf << CombinePDF.load('../../test pdfs/outlines/named_dest.pdf'); nil
pdf.save '05_1_timeless_check_named_dest_links.pdf' # never ends... :-(
pdf.save '../tmp/05_1_timeless_check_named_dest_links.pdf' # never ends... :-(

pdf = CombinePDF.load '../../test pdfs/outline_small.pdf'
pdf << CombinePDF.load('../../test pdfs/outline_small.pdf')
pdf.save '06_check_links_to_second_copy.pdf'
pdf.save '../tmp/06_check_links_to_second_copy.pdf'

lists = %w(../../test\ pdfs/outlines/self_merge_err.pdf ../../test\ pdfs/outlines/big_toc.pdf ../../test\ pdfs/outlines/bigger_toc.pdf ../../test\ pdfs/outlines/named_dest_no_toc.pdf ../../test\ pdfs/outlines/named_dest_no_toc2.pdf ../../test\ pdfs/outlines/named_dest.pdf ../../test\ pdfs/outlines/named_dest2.pdf)

Expand All @@ -77,7 +77,7 @@ lists.each do |n|
end
pdf = CombinePDF.new
lists.each { |n| pdf << CombinePDF.load(n) }
pdf.save('07_named destinations.pdf')
pdf.save('../tmp/07_named destinations.pdf')

pdf = CombinePDF.new
lists.each { |n| pdf << CombinePDF.load(n) }
Expand All @@ -91,15 +91,15 @@ pdf.number_pages(start_at: 1,
number_location: [:top, :bottom],
opacity: 0.75)

pdf.save('07_named destinations_numbered.pdf')
pdf.save('../tmp/07_named destinations_numbered.pdf')

CombinePDF.load("../../test\ pdfs/Scribus-unknown_err.pdf").save '08_1-unknown-err-empty-str.pdf'
CombinePDF.load("../../test\ pdfs/Scribus-unknown_err2.pdf").save '08_2-unknown-err-empty-str.pdf'
CombinePDF.load("../../test\ pdfs/Scribus-unknown_err3.pdf").save '08_3-unknown-err-empty-str.pdf'
CombinePDF.load("../../test\ pdfs/xref_in_middle.pdf").save '08_4-xref-in-middle.pdf'
CombinePDF.load("../../test\ pdfs/xref_split.pdf").save '08_5-xref-fragmented.pdf'
CombinePDF.load("../../test\ pdfs/Scribus-unknown_err.pdf").save '../tmp/08_1-unknown-err-empty-str.pdf'
CombinePDF.load("../../test\ pdfs/Scribus-unknown_err2.pdf").save '../tmp/08_2-unknown-err-empty-str.pdf'
CombinePDF.load("../../test\ pdfs/Scribus-unknown_err3.pdf").save '../tmp/08_3-unknown-err-empty-str.pdf'
CombinePDF.load("../../test\ pdfs/xref_in_middle.pdf").save '../tmp/08_4-xref-in-middle.pdf'
CombinePDF.load("../../test\ pdfs/xref_split.pdf").save '../tmp/08_5-xref-fragmented.pdf'

CombinePDF.load("../../test\ pdfs/nil_object.pdf").save('09_nil_in_parsed_array.pdf')
CombinePDF.load("../../test\ pdfs/nil_object.pdf").save('../tmp/09_nil_in_parsed_array.pdf')

encrypted = [ "../../test\ pdfs/pdf-reader/encrypted_version4_revision4_128bit_aes_user_pass_apples_enc_metadata.pdf",
"../../test\ pdfs/AESv2\ encrypted.pdf",
Expand All @@ -110,7 +110,7 @@ encrypted = [ "../../test\ pdfs/pdf-reader/encrypted_version4_revision4_128bit_a
encrypted.length.times do |i|
fname = File.basename encrypted[i]
begin
CombinePDF.load(encrypted[i]).save "10_#{i}_#{fname}"
CombinePDF.load(encrypted[i]).save "../tmp/10_#{i}_#{fname}"
rescue => e
puts e.class.name, e.message
if(i == 0)
Expand All @@ -126,10 +126,10 @@ IO.binwrite '11_prawn.pdf', (Prawn::Document.new { text 'Hello World!' }).render
page = CombinePDF.parse((Prawn::Document.new { text 'Hello World!' }).render)
pdf = CombinePDF.new
pdf << page
pdf.save '11_parsed_from_prawn.pdf'
pdf.save '../tmp/11_parsed_from_prawn.pdf'
pdf = CombinePDF.new
pdf << page << page
pdf.save('11_AcrobatReader_is_unique_page.pdf')
pdf.save('../tmp/11_AcrobatReader_is_unique_page.pdf')

puts GC.stat.inspect
# unify = [
Expand Down

0 comments on commit 8eba750

Please sign in to comment.