mirror of
https://github.com/GSA/notifications-admin.git
synced 2026-05-26 08:09:51 -04:00
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:
18
app/utils.py
18
app/utils.py
@@ -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',
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user