def draw_background(self, which, canv):
        ''' Draws a background and/or foreground image
            on each page which uses the template.

            Calculates the image one time, and caches
            it for reuse on every page in the template.

            How the background is drawn depends on the
            --fit-background-mode option.

            If desired, we could add code to push it around
            on the page, using stylesheets to align and/or
            set the offset.
        '''
        uri=self.template[which]
        info = self.image_cache.get(uri)
        if info is None:
            fname, _, _ = MyImage.split_uri(uri)
            if not os.path.exists(fname):
                del self.template[which]
                log.error("Missing %s image file: %s", which, uri)
                return
            try:
                w, h, kind = MyImage.size_for_node(dict(uri=uri, ), self.client)
            except __HOLE__:
                # Broken image, return arbitrary stuff
                uri=missing
                w, h, kind = 100, 100, 'direct'

            pw, ph = self.styles.pw, self.styles.ph
            if self.client.background_fit_mode == 'center':
                scale = min(1.0, 1.0 * pw / w, 1.0 * ph / h)
                sw, sh = w * scale, h * scale
                x, y = (pw - sw) / 2.0, (ph - sh) / 2.0
            elif self.client.background_fit_mode == 'scale':
                x, y = 0, 0
                sw, sh = pw, ph
            else:
                log.error('Unknown background fit mode: %s'% self.client.background_fit_mode)
                # Do scale anyway
                x, y = 0, 0
                sw, sh = pw, ph

            bg = MyImage(uri, sw, sh, client=self.client)
            self.image_cache[uri] = info = bg, x, y
        bg, x, y = info
        bg.drawOn(canv, x, y)
dataset/ETHPy150Open rst2pdf/rst2pdf/rst2pdf/createpdf.py/FancyPage.draw_background