From 6ce063669e9aa2a57379efdaf2f1877030ff8473 Mon Sep 17 00:00:00 2001 From: Mathieu Fenniak Date: Thu, 15 Oct 2009 10:45:40 -0600 Subject: [PATCH] Conform to spec. when writing floats MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 --- pyPdf/generic.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/pyPdf/generic.py b/pyPdf/generic.py index 835bff3..948c1ee 100644 --- a/pyPdf/generic.py +++ b/pyPdf/generic.py @@ -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):