Exemplo n.º 1
0
def _generate_diagram(app, diagram_type, output_format, source):
    try:
        output_format = output_format.lower()
        if output_format == 'png':
            result = generate_diag(app, diagram_type, output_format, source)
            response = send_file(io.BytesIO(result),
                                 attachment_filename='result.png',
                                 mimetype='image/png')
            return response
        elif output_format == 'pdf':
            result = generate_diag(app, diagram_type, output_format, source)
            response = make_response(result)
            response.headers['Content-Type'] = 'application/pdf'
            response.headers['Content-Disposition'] = 'inline; filename=result.pdf'
            return response
        elif output_format == 'svg':
            result = generate_diag(app, diagram_type, output_format, source)
            response = make_response(result)
            response.headers["Content-Type"] = "image/svg+xml; charset=utf-8"
            return response
        else:
            raise InvalidUsage('Unsupported output format: %s. Must be one of: png, svg or pdf.' % output_format,
                               status_code=400)
    except GenerateError as err:
        raise err
    except Exception as err:
        raise GenerateError('Unexpected error',
                            status_code=500,
                            payload={
                                'source': source,
                                'output_format': output_format,
                                'diagram_type': diagram_type,
                                'error': str(err)
                            })
Exemplo n.º 2
0
def generate_diag(app, diagram_type, output_format, source):
    options = ['-T' + output_format, 'file']
    app.parse_options(options)

    output_format = output_format.lower()
    if output_format == 'pdf':
        app.options.font = [
            '/usr/share/fonts/truetype/dejavu/DejaVuSerif.ttf',
            '/usr/share/fonts/ttf-dejavu/DejaVuSerif.ttf'
        ]

    app.fontmap = create_fontmap(app.options)
    app.setup()

    app.code = source
    try:
        tree = app.module.parser.parse_string(app.code)
        try:
            diagram = app.module.builder.ScreenNodeBuilder.build(
                tree, app.options)
        except TypeError:
            diagram = app.module.builder.ScreenNodeBuilder.build(
                tree)  # old interface

        drawer = app.module.drawer.DiagramDraw(
            app.options.type,
            diagram,
            None,
            fontmap=app.fontmap,
            code=app.code,
            antialias=app.options.antialias,
            nodoctype=app.options.nodoctype,
            transparency=app.options.transparency)
        drawer.draw()
        drawer.drawer._run()
        if output_format == 'png':
            png_image = drawer.drawer.save(None, None, drawer.format)
            return png_image
        elif output_format == 'pdf':
            pdf_drawer = drawer.drawer.target
            pdf_drawer.canvas.showPage()
            binary_pdf = pdf_drawer.canvas.getpdfdata()
            return binary_pdf
        elif output_format == 'svg':
            xml_text = drawer.drawer.save(None, None, drawer.format)
            return xml_text
    except (ParseException, Exception) as err:
        raise GenerateError('Unable to generate the ' + diagram_type +
                            ' diagram from source',
                            status_code=400,
                            payload={
                                'source': source,
                                'output_format': output_format,
                                'diagram_type': diagram_type,
                                'error': str(err)
                            })