-
Notifications
You must be signed in to change notification settings - Fork 138
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How can render images on pdf?? #53
Comments
Are you referencing the correct static path? Images are working for me |
Previously, I have a same problem. import os
import base64
import requests
from django import template
register = template.Library()
@register.filter
def read_image_as_base64(image_url):
"""
:param `image_url` for the complete path of image.
:return string base64 of image
>>> from yourapp.templatetags.image_tags import *
>>>
>>> read_image_as_base64('http://127.0.0.1:8000/static/images/watermark.png')
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgxxxxxxx'
>>>
"""
if not image_url:
return image_url
try:
response = requests.get(image_url)
if response.status_code == 200:
image_data = response.content
image_format = os.path.splitext(image_url)[-1].replace('.', '').lower()
encoded_string = base64.b64encode(image_data).decode('utf-8')
if image_format in ['jpg', 'jpeg', 'png', 'gif']:
return 'data:image/%s;base64,%s' % (image_format, encoded_string)
except Exception as error:
pass
return image_url Usage example:
|
I'm not sure if it should, but at least with the new django easy pdf version (https://github.com/olksndrdevhub/django-easy-pdf3) this isn't working anymore. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I have an issue, I can't render images on pdf.
The text was updated successfully, but these errors were encountered: