Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #1130 from jkseppan/pdf-on-stdout

Fix writing pdf on stdout
  • Loading branch information...
commit 0ee82be7a84beacbd95b6d0c7835fa8ac1c7ec67 2 parents be72e1b + f829bb5
@mdboom mdboom authored
Showing with 15 additions and 4 deletions.
  1. +15 −4 lib/matplotlib/backends/backend_pdf.py
View
19 lib/matplotlib/backends/backend_pdf.py
@@ -394,11 +394,19 @@ def __init__(self, filename):
self.nextObject = 1 # next free object id
self.xrefTable = [ [0, 65535, 'the zero object'] ]
self.passed_in_file_object = False
+ self.original_file_like = None
+ self.tell_base = 0
if is_string_like(filename):
fh = open(filename, 'wb')
elif is_writable_file_like(filename):
- fh = filename
- self.passed_in_file_object = True
+ try:
+ self.tell_base = filename.tell()
+ except IOError:
+ fh = BytesIO()
+ self.original_file_like = filename
+ else:
+ fh = filename
+ self.passed_in_file_object = True
else:
raise ValueError("filename must be a path or a file-like object")
@@ -524,6 +532,9 @@ def close(self):
self.writeTrailer()
if self.passed_in_file_object:
self.fh.flush()
+ elif self.original_file_like is not None:
+ self.original_file_like.write(self.fh.getvalue())
+ self.fh.close()
else:
self.fh.close()
@@ -1351,7 +1362,7 @@ def reserveObject(self, name=''):
return Reference(id)
def recordXref(self, id):
- self.xrefTable[id][0] = self.fh.tell()
+ self.xrefTable[id][0] = self.fh.tell() - self.tell_base
def writeObject(self, object, contents):
self.recordXref(object.id)
@@ -1360,7 +1371,7 @@ def writeObject(self, object, contents):
def writeXref(self):
"""Write out the xref table."""
- self.startxref = self.fh.tell()
+ self.startxref = self.fh.tell() - self.tell_base
self.write(("xref\n0 %d\n" % self.nextObject).encode('ascii'))
i = 0
borken = False
Please sign in to comment.
Something went wrong with that request. Please try again.