Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Fix writing pdf on stdout #1130

Merged
merged 1 commit into from

3 participants

@jkseppan
Collaborator

Could @KennethNielsen try to see if this fixes #1089?

@jkseppan jkseppan Fix writing pdf on stdout
Writing on file-like objects broke object cross-references if
anything else had been written on the same stream before. On Linux
writing on sys.stdout failed because sys.stdout.tell() raises
an error; this is worked around by using a BytesIO object.
f829bb5
@mdboom
Owner

This looks like a good solution.

@KennethNielsen

Hallo @jkseppan and thank you very much for working on this (must admit that I didn't feel too positive about being able to do it myself after opening the ~2500 lines pdf backend file ;) ). I will try to get around to test it tomorrow or in the weekend.

@KennethNielsen

@jkseppan I have now tested this by saving a simple figure to stdout and piping the output to a file. I have done this on both a Debian and Red Hat based linux distribution (Ubuntu 12.04 and Fedora 16 respective) and it works like a charm. Thanks once again.

@mdboom
Owner

Great! I'm going to merge this.

@mdboom mdboom merged commit 0ee82be into matplotlib:master
@jkseppan jkseppan deleted the jkseppan:pdf-on-stdout branch
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 21, 2012
  1. @jkseppan

    Fix writing pdf on stdout

    jkseppan authored
    Writing on file-like objects broke object cross-references if
    anything else had been written on the same stream before. On Linux
    writing on sys.stdout failed because sys.stdout.tell() raises
    an error; this is worked around by using a BytesIO object.
This page is out of date. Refresh to see the latest.
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
Something went wrong with that request. Please try again.