Skip to content

Commit

Permalink
Scientific notation for floats is invalid in PDF
Browse files Browse the repository at this point in the history
  • Loading branch information
bradediger committed Nov 2, 2011
1 parent e4d0d0a commit 701aae0
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/prawn/core/pdf_object.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,13 @@ def PdfObject(obj, in_content_stream = false)
when NilClass then "null"
when TrueClass then "true"
when FalseClass then "false"
when Numeric then String(obj)
when Numeric
if (str = String(obj)) =~ /e/i
# scientific notation is not supported in PDF
sprintf("%.16f", obj).gsub(/\.?0+\z/, "")
else
str
end
when Array
"[" << obj.map { |e| PdfObject(e, in_content_stream) }.join(' ') << "]"
when Prawn::Core::LiteralString
Expand Down
2 changes: 2 additions & 0 deletions spec/pdf_object_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
it "should convert a Ruby number to PDF number" do
Prawn::Core::PdfObject(1).should == "1"
Prawn::Core::PdfObject(1.214112421).should == "1.214112421"
# scientific notation is not valid in PDF
Prawn::Core::PdfObject(0.000005).should == "0.000005"
end

it "should convert a Ruby time object to a PDF timestamp" do
Expand Down

0 comments on commit 701aae0

Please sign in to comment.