Browse files

Conform to spec. when writing floats

FloatObject.writeToStream could output "1.1 E-1", but the PDF specification
§7.3.3 "Numeric Objects" doesn't allow exponential notation. We now conforms
to the specification by manually outputing:
* fractional numbers with 5 digits precision;
* integers without exponent.

The output stream now tends to include useless extraneous zeros, but a few
extra bytes are still better than writing unparsable PDFs.

Patch from Kjo Hansi Glaz <>
  • Loading branch information...
mfenniak committed Oct 15, 2009
1 parent 073d985 commit 6ce063669e9aa2a57379efdaf2f1877030ff8473
Showing with 6 additions and 2 deletions.
  1. +6 −2 pyPdf/
@@ -206,9 +206,13 @@ class FloatObject(decimal.Decimal, PdfObject):
def __new__(cls, value="0", context=None):
return decimal.Decimal.__new__(cls, str(value), context)
def __repr__(self):
- return str(self)
+ if self == self.to_integral():
+ return str(self.quantize(decimal.Decimal(1)))
+ else:
+ # XXX: this adds useless extraneous zeros.
+ return "%.5f" % self
def writeToStream(self, stream, encryption_key):
- stream.write(str(self))
+ stream.write(repr(self))
class NumberObject(int, PdfObject):

0 comments on commit 6ce0636

Please sign in to comment.