Skip to content

Commit a30667c

Browse files
Fix Renderer errors with --enable-frozen-string-literal
There were several instances in which adding .b to string literals was necessary to not throw this error when --enable-frozen-string-literal was enabled. Exposed with tests.
1 parent 44f46fa commit a30667c

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

lib/combine_pdf/renderer.rb

+4-4
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,15 @@ def format_hash_to_pdf(object)
101101
end
102102
object[:indirect_reference_id] ||= 0
103103
object[:indirect_generation_number] ||= 0
104-
return "#{object[:indirect_reference_id]} #{object[:indirect_generation_number]} R".force_encoding(Encoding::ASCII_8BIT)
104+
return "#{object[:indirect_reference_id]} #{object[:indirect_generation_number]} R".b.force_encoding(Encoding::ASCII_8BIT)
105105
end
106106

107107
# if the object is indirect...
108108
out = []
109109
if object[:indirect_reference_id]
110110
object[:indirect_reference_id] ||= 0
111111
object[:indirect_generation_number] ||= 0
112-
out << "#{object[:indirect_reference_id]} #{object[:indirect_generation_number]} obj\n".force_encoding(Encoding::ASCII_8BIT)
112+
out << "#{object[:indirect_reference_id]} #{object[:indirect_generation_number]} obj\n".b.force_encoding(Encoding::ASCII_8BIT)
113113
if object[:indirect_without_dictionary]
114114
out << object_to_pdf(object[:indirect_without_dictionary])
115115
out << "\nendobj\n"
@@ -126,11 +126,11 @@ def format_hash_to_pdf(object)
126126
# (using LESS-THAN SIGNs (3Ch) and GREATER-THAN SIGNs (3Eh)).
127127
out << "<<\n".b
128128
object.each do |key, value|
129-
out << "#{object_to_pdf key} #{object_to_pdf value}\n".force_encoding(Encoding::ASCII_8BIT) unless PDF::PRIVATE_HASH_KEYS.include? key
129+
out << "#{object_to_pdf key} #{object_to_pdf value}\n".b.force_encoding(Encoding::ASCII_8BIT) unless PDF::PRIVATE_HASH_KEYS.include? key
130130
end
131131
object.delete :Length
132132
out << '>>'.b
133-
out << "\nstream\n#{object[:raw_stream_content]}\nendstream".force_encoding(Encoding::ASCII_8BIT) if object[:raw_stream_content]
133+
out << "\nstream\n#{object[:raw_stream_content]}\nendstream".b.force_encoding(Encoding::ASCII_8BIT) if object[:raw_stream_content]
134134
out << "\nendobj\n" if object[:indirect_reference_id]
135135
out.join.force_encoding(Encoding::ASCII_8BIT)
136136
end

test/combine_pdf/renderer_test.rb

+26
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,30 @@ def test_numeric_array_to_pdf
1717

1818
assert_equal(expected, actual)
1919
end
20+
21+
def test_object_to_pdf_indirect_reference_id
22+
actual = TestRenderer.new.test_object(
23+
:indirect_reference_id => 1,
24+
:indirect_generation_number => 2
25+
)
26+
assert_match /^1 2 obj/, actual
27+
assert_match /endobj$/, actual
28+
end
29+
30+
def test_object_to_pdf_is_reference_only
31+
actual = TestRenderer.new.test_object(
32+
:Pages => {
33+
:referenced_object => { :Type => :Pages, :Count => 0, :indirect_reference_id => 3 },
34+
:is_reference_only => true
35+
}
36+
)
37+
assert_match /Pages 3 0 R/, actual
38+
end
39+
40+
def test_object_to_pdf_raw_stream_content
41+
actual = TestRenderer.new.test_object(
42+
:raw_stream_content => 'Testing'
43+
)
44+
assert_match /stream\nTesting\nendstream/, actual
45+
end
2046
end

0 commit comments

Comments
 (0)