'Render the figure to hardcopy.  Set the figure patch face and
edge colors.  This is useful because some of the GUIs have a
gray figure face color background and you\'ll probably want to
override this on hardcopy
If outfile is a string, it is interpreted as a file name.
If the extension matches .ep* write encapsulated postscript,
otherwise write a stand-alone PostScript file.
If outfile is a file object, a stand-alone PostScript file is
written into this file object.
metadata must be a dictionary. Currently, only the value for
the key \'Creator\' is used.'

def _print_figure(self, outfile, format, dpi=72, facecolor=u'w', edgecolor=u'w', orientation=u'portrait', isLandscape=False, papertype=None, metadata=None, **kwargs):

    isEPSF = (format == u'eps')
    passed_in_file_object = False
    if isinstance(outfile, six.string_types):
        title = outfile
    elif is_writable_file_like(outfile):
        title = None
        passed_in_file_object = True
    else:
        raise ValueError(u'outfile    must    be    a    path    or    a    file-like    object')
    (width, height) = self.figure.get_size_inches()
    if (papertype == u'auto'):
        if isLandscape:
            papertype = _get_papertype(height, width)
        else:
            papertype = _get_papertype(width, height)
    if isLandscape:
        (paperHeight, paperWidth) = papersize[papertype]
    else:
        (paperWidth, paperHeight) = papersize[papertype]
    if (rcParams[u'ps.usedistiller'] and (not (papertype == u'auto'))):
        if ((width > paperWidth) or (height > paperHeight)):
            if isLandscape:
                papertype = _get_papertype(height, width)
                (paperHeight, paperWidth) = papersize[papertype]
            else:
                papertype = _get_papertype(width, height)
                (paperWidth, paperHeight) = papersize[papertype]
    xo = ((72 * 0.5) * (paperWidth - width))
    yo = ((72 * 0.5) * (paperHeight - height))
    (l, b, w, h) = self.figure.bbox.bounds
    llx = xo
    lly = yo
    urx = (llx + w)
    ury = (lly + h)
    rotation = 0
    if isLandscape:
        (llx, lly, urx, ury) = (lly, llx, ury, urx)
        (xo, yo) = (((72 * paperHeight) - yo), xo)
        rotation = 90
    bbox = (llx, lly, urx, ury)
    origfacecolor = self.figure.get_facecolor()
    origedgecolor = self.figure.get_edgecolor()
    self.figure.set_facecolor(facecolor)
    self.figure.set_edgecolor(edgecolor)
    dryrun = kwargs.get(u'dryrun', False)
    if dryrun:
        class NullWriter(object, ):
            def write(self, *kl, **kwargs):
                pass
        self._pswriter = NullWriter()
    else:
        self._pswriter = io.StringIO()
    _bbox_inches_restore = kwargs.pop(u'bbox_inches_restore', None)
    ps_renderer = self._renderer_class(width, height, self._pswriter, imagedpi=dpi)
    renderer = MixedModeRenderer(self.figure, width, height, dpi, ps_renderer, bbox_inches_restore=_bbox_inches_restore)
    self.figure.draw(renderer)
    if dryrun:
        return
    self.figure.set_facecolor(origfacecolor)
    self.figure.set_edgecolor(origedgecolor)
    if ((metadata is not None) and (u'Creator' in metadata)):
        creator_str = metadata[u'Creator']
    else:
        creator_str = ((u'matplotlib    version    ' + __version__) + u',    http://matplotlib.org/')
    def print_figure_impl(fh):
        if isEPSF:
            print(u'%!PS-Adobe-3.0    EPSF-3.0', file=fh)
        else:
            print(u'%!PS-Adobe-3.0', file=fh)
        if title:
            print((u'%%Title:    ' + title), file=fh)
        print((u'%%Creator:    ' + creator_str), file=fh)
        source_date_epoch = os.getenv(u'SOURCE_DATE_EPOCH')
        if source_date_epoch:
            source_date = datetime.datetime.utcfromtimestamp(int(source_date_epoch)).strftime(u'%a    %b    %d    %H:%M:%S    %Y')
        else:
            source_date = time.ctime()
        print((u'%%CreationDate:    ' + source_date), file=fh)
        print((u'%%Orientation:    ' + orientation), file=fh)
        if (not isEPSF):
            print((u'%%DocumentPaperSizes:    ' + papertype), file=fh)
        print((u'%%%%BoundingBox:    %d    %d    %d    %d' % bbox), file=fh)
        if (not isEPSF):
            print(u'%%Pages:    1', file=fh)
        print(u'%%EndComments', file=fh)
        Ndict = len(psDefs)
        print(u'%%BeginProlog', file=fh)
        if (not rcParams[u'ps.useafm']):
            Ndict += len(ps_renderer.used_characters)
        print((u'/mpldict    %d    dict    def' % Ndict), file=fh)
        print(u'mpldict    begin', file=fh)
        for d in psDefs:
            d = d.strip()
            for l in d.split(u'\n'):
                print(l.strip(), file=fh)
        if (not rcParams[u'ps.useafm']):
            for (font_filename, chars) in six.itervalues(ps_renderer.used_characters):
                if len(chars):
                    font = get_font(font_filename)
                    glyph_ids = []
                    for c in chars:
                        gind = font.get_char_index(c)
                        glyph_ids.append(gind)
                    fonttype = rcParams[u'ps.fonttype']
                    if (len(glyph_ids) > 255):
                        fonttype = 42
                    if is_opentype_cff_font(font_filename):
                        msg = u'OpenType    CFF    fonts    can    not    be    saved    using    the    internal    Postscript    backend    at    this    time.\nConsider    using    the    Cairo    backend.'
                        raise RuntimeError(msg)
                    else:
                        fh.flush()
                        convert_ttf_to_ps(font_filename.encode(sys.getfilesystemencoding()), fh, fonttype, glyph_ids)
        print(u'end', file=fh)
        print(u'%%EndProlog', file=fh)
        if (not isEPSF):
            print(u'%%Page:    1    1', file=fh)
        print(u'mpldict    begin', file=fh)
        print((u'%s    translate' % _nums_to_str(xo, yo)), file=fh)
        if rotation:
            print((u'%d    rotate' % rotation), file=fh)
        print((u'%s    clipbox' % _nums_to_str((width * 72), (height * 72), 0, 0)), file=fh)
        content = self._pswriter.getvalue()
        if (not isinstance(content, six.text_type)):
            content = content.decode(u'ascii')
        print(content, file=fh)
        print(u'end', file=fh)
        print(u'showpage', file=fh)
        if (not isEPSF):
            print(u'%%EOF', file=fh)
        fh.flush()
    if rcParams[u'ps.usedistiller']:
        (fd, tmpfile) = mkstemp()
        try:
            with io.open(fd, u'w', encoding=u'latin-1') as fh:
                print_figure_impl(fh)
            if (rcParams[u'ps.usedistiller'] == u'ghostscript'):
                gs_distill(tmpfile, isEPSF, ptype=papertype, bbox=bbox)
            elif (rcParams[u'ps.usedistiller'] == u'xpdf'):
                xpdf_distill(tmpfile, isEPSF, ptype=papertype, bbox=bbox)
            if passed_in_file_object:
                if file_requires_unicode(outfile):
                    with io.open(tmpfile, u'rb') as fh:
                        outfile.write(fh.read().decode(u'latin-1'))
                else:
                    with io.open(tmpfile, u'rb') as fh:
                        outfile.write(fh.read())
            else:
                with io.open(outfile, u'w') as fh:
                    pass
                mode = os.stat(outfile).st_mode
                shutil.move(tmpfile, outfile)
                os.chmod(outfile, mode)
        finally:
            if os.path.isfile(tmpfile):
                os.unlink(tmpfile)
    elif passed_in_file_object:
        requires_unicode = file_requires_unicode(outfile)
        if ((not requires_unicode) and (six.PY3 or (not isinstance(outfile, StringIO)))):
            fh = io.TextIOWrapper(outfile, encoding=u'latin-1')
            def do_nothing():
                pass
            fh.close = do_nothing
        else:
            fh = outfile
        print_figure_impl(fh)
    else:
        with io.open(outfile, u'w', encoding=u'latin-1') as fh:
            print_figure_impl(fh)
