From 90c9fcea547c3ec15cec60e8634ec3a6dda736b7 Mon Sep 17 00:00:00 2001 From: Alexander Mankuta Date: Fri, 6 Mar 2026 12:16:40 +0200 Subject: [PATCH] Fix multibyte-encoded string in streams --- CHANGELOG.md | 5 +++++ lib/pdf/core/stream.rb | 6 +++--- spec/pdf/core/stream_spec.rb | 6 ++++++ 3 files changed, 14 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 91e8b53..2389205 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file. ## Unreleased +### Fixed + +- Correctly handle multibyte-encoded string in streams + Alexander Mankuta + ## [0.10.0][] - 2024-03-05 ### Changed diff --git a/lib/pdf/core/stream.rb b/lib/pdf/core/stream.rb index 4985480..83466a9 100644 --- a/lib/pdf/core/stream.rb +++ b/lib/pdf/core/stream.rb @@ -23,10 +23,10 @@ def initialize(io = nil) # Append data to stream. # - # @param io [String] + # @param data [String] # @return [self] - def <<(io) - (@stream ||= +'') << io + def <<(data) + (@stream ||= ''.b) << data.dup.force_encoding(Encoding::BINARY) @filtered_stream = nil self end diff --git a/spec/pdf/core/stream_spec.rb b/spec/pdf/core/stream_spec.rb index 7b17d04..2bc045b 100644 --- a/spec/pdf/core/stream_spec.rb +++ b/spec/pdf/core/stream_spec.rb @@ -50,4 +50,10 @@ expect(stream.data[:DecodeParms]).to eq [{ Predictor: 15 }] end + + it 'handles multibyte encoded strings correctly' do + stream << '♥' + + expect(stream.data[:Length]).to eq 3 + end end