Refactor PNG generating endpoint for code reuse

The way we generate PNGs is the same for all the different endpoints,
so the code to do this should be shared.
This commit is contained in:
Chris Hill-Scott
2016-12-28 11:06:11 +00:00
parent 559433c5d2
commit 2854dbe3dd
3 changed files with 35 additions and 30 deletions

View File

@@ -1,6 +1,6 @@
import re
import csv
from io import StringIO
from io import StringIO, BytesIO
from os import path
from functools import wraps
import unicodedata
@@ -8,6 +8,8 @@ import unicodedata
from flask import (abort, current_app, session, request, redirect, url_for)
from flask_login import current_user
from wand.image import Image
from notifications_utils.template import (
SMSPreviewTemplate,
EmailPreviewTemplate,
@@ -257,3 +259,17 @@ def get_template(
return LetterPreviewTemplate(
template
)
def png_from_pdf(pdf_endpoint):
output = BytesIO()
with Image(
blob=pdf_endpoint.get_data()
) as image:
with image.convert('png') as converted:
converted.save(file=output)
output.seek(0)
return dict(
filename_or_fp=output,
mimetype='image/png',
)