Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

PDF inspector fails on simple pdf with jpg image #339

Closed
e2 opened this Issue · 1 comment

2 participants

@e2
e2 commented
require 'prawn'

filename = 'out.pdf'
File.open(filename,'w') do |f|
  f.write Prawn::Document.new {|pdf| pdf.image open('fractal.jpg')}.render
end

require 'pdf/inspector'
PDF::Inspector::Text.analyze(File.read(filename))

Error:

pdf-reader-1.0.0/lib/pdf/reader/parser.rb:241:in `rescue in stream': ERROR: PDF malformed, expected 'endstream' but found 0 instead

I tried on head with various Ruby versions / etc. Only PNG files seem to work without issues.

Any idea what could be the cause?

Thanks.

@e2
e2 commented

Could you set binmode on the io object for images, like so:

file.binmode if file.respond_to?(:binmode)

so:

image open('foo.jpg')

works?

Details:

    def image(file, options={})
      Prawn.verify_options [:at, :position, :vposition, :height,
                            :width, :scale, :fit], options

      file.binmode if file.respond_to?(:binmode)

      if file.respond_to?(:read)
        image_content = file.read
      else
        raise ArgumentError, "#{file} not found" unless File.file?(file)
        image_content = File.binread(file)
      end

The cause of the problem is that image_content returns the wrong size when file is opened as UTF-8.
That size is saved into the PDF and it makes everything else choke on that.

@yob yob closed this issue from a commit
@yob yob ensure images are read as binary data
* closes #339
0763c76
@yob yob closed this in 0763c76
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.