<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:

def save_image_as_pdf(self, filename, data=None, max_levels=None):
        "Write the heap as PDF file."
        assert (max_levels is None) or (max_levels >= 0)
        import os
        if not filename.endswith('.pdf'):
            filename = filename+'.pdf'
        tmpfd, tmpname = tempfile.mkstemp(suffix='dot')
        with open(tmpname, 'w') as f:
            self.write_as_dot(f, data=data, max_levels=max_levels)
        os.close(tmpfd)
        try:
            subprocess.call(['dot',
                             tmpname,
                             '-Tpdf',
                             '-o',
                             ('%s'%filename)])
        except OSError:
            sys.stderr.write(
                "DOT -> PDF conversion failed. See DOT file: %s\n"
                % (tmpname))
            return False
        os.remove(tmpname)
        return True