Showing posts with label repotlab. Show all posts
Showing posts with label repotlab. Show all posts

Thursday, July 14, 2022

How to draw image from raw bytes using ReportLab?

from reportlab.lib.utils import ImageReader
import io
image = ImageReader(io.BytesIO(raw_image_bytes)) 
page.drawImage(image, ...) 


Example:

def PDF_view(request):

    response = HttpResponse(content_type='application/pdf')

    ...

    page = canvas.Canvas(response, pagesize=A4)
    page.setTitle("Sample PDF")


    image = ImageReader(io.BytesIO(raw_image_bytes))# raw_image_bytes is from external source
image.seek(0) page.drawImage(image, 100, 100 ) filename = 'document.pdf' page.showPage() page.save() return response

https://stackoverflow.com/questions/40966401/how-to-draw-image-from-raw-bytes-using-reportlab