<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def jinja2_output_as_string(impact_report, component_key):
 """Get a given jinja2 component output as string. Useful for composing complex document. :param impact_report: Impact Report that contains the component key. :type impact_report: safe.report.impact_report.ImpactReport :param component_key: The key of the component to get the output from. :type component_key: str :return: output as string. :rtype: str .. versionadded:: 4.0 """

    metadata = impact_report.metadata
    for c in metadata.components:
        if c.key == component_key:
            if c.output_format == 'string':
                return c.output or ''
            elif c.output_format == 'file':
                try:
                    filename = os.path.join(
                        impact_report.output_folder, c.output_path)
                    filename = os.path.abspath(filename)
                    # We need to open the file in UTF-8, the HTML may have
                    # some accents for instance.
                    with codecs.open(filename, 'r', 'utf-8') as f:
                        return f.read()
                except IOError:
                    pass

    raise TemplateError(
        "Can't find component with key '%s' and have an output" %
        component_key)