I am able to generate the bar code and save the image file in the root folder using this library python-barcode. Now I am trying to generate the bar code image and download via browser as HttpResponse
Here are my tryouts,
import barcode
from django.http import HttpResponse
def download_bar_code(request):
ean = barcode.get('upc', '123456789102', writer=ImageWriter())
ean.save('filename')
image = ean.render() # Returns PIL image class
# <PIL.Image.Image image mode=RGB size=523x280 at 0x7FAE2B471320>
return HttpResponse(image, content_type="image/png")
Here the image file is saving in the root folder, but not downloading via a browser. I am not able to find the solution for this, I request you to please suggest me some solution to solve this, it would be very grateful for me. Thanks in advance.
You can do something like
response = HttpResponse(mimetype="image/png")
image.save(response, "PNG")
return response
The response is a stream similar to a file, so you can write to that instead of saving it to file.
Also, I think you can skip ean.save('filename')
but I'm not 100% sure as I've never used that library.