Skip to content

Commit

Permalink
Conform to spec. when writing floats
Browse files Browse the repository at this point in the history
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 <kjo@a4nancy.net.eu.org>
  • Loading branch information
mfenniak committed Oct 15, 2009
1 parent 073d985 commit 6ce0636
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions pyPdf/generic.py
Expand Up @@ -206,9 +206,13 @@ class FloatObject(decimal.Decimal, PdfObject):
def __new__(cls, value="0", context=None): def __new__(cls, value="0", context=None):
return decimal.Decimal.__new__(cls, str(value), context) return decimal.Decimal.__new__(cls, str(value), context)
def __repr__(self): 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): def writeToStream(self, stream, encryption_key):
stream.write(str(self)) stream.write(repr(self))




class NumberObject(int, PdfObject): class NumberObject(int, PdfObject):
Expand Down

0 comments on commit 6ce0636

Please sign in to comment.