Example #1
0
def convert(request, chart_id):
    chart = get_object_or_404(Chart, id=chart_id)

    # Output svg data.

    f, svg_path = tempfile.mkstemp()
    png_path = utils.chart_image_path(chart_id)

    with os.fdopen(f, "wb") as fd:
        fd.write(request.POST["svg"])

    # Convert to png.

    status = subprocess.check_call(
        ["java", "-Xmx10m", "-Djava.awt.headless=true", "-jar", BATIK_JAR_PATH, "-d", png_path, svg_path]
    )

    # For debug purposes, if we were asked for svg, save the svg data as well.  Otherwise delete it.

    if request.POST["type"] == "image/svg+xml":
        shutil.move(svg_path, utils.chart_image_path(chart_id, ext="svg"))
    else:
        os.remove(svg_path)

    return image(request, chart_id)
Example #2
0
def image(request, chart_id=None):
    chart = get_object_or_404(Chart, id=chart_id)
    try:
        with open(utils.chart_image_path(chart.id), "rb") as f:
            image_data = f.read()
    except IOError:
        raise Http404

    return HttpResponse(image_data, mimetype="image/png")