API on ReportLab with Django


Well, I have this doubt because I have not found good documentation about it, there is a specific doubt at the moment and it is with the images.

from reportlab.lib import colors
from reportlab.lib.units import cm
from reportlab.lib.enums import TA_JUSTIFY, TA_CENTER
from reportlab.lib.pagesizes import letter, landscape
from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Table, TableStyle, Image
    url_logo = EmpresaImagenes.objects.get(id=1).logo
    if url_logo!='':
        im = Image(url_logo, 3*cm, 3*cm)
        im.hAlign ='RIGHT'

because if I leave only Image(url_logo) the image goes beyond the actual size, if I put Image(url_logo, 3*cm, 3*cm) it adjusts to 3cm x 3cm, but if I just want to adjust the height of the image and that the width is proportional?

I would like to know if anyone knows an API, guide, tutorial or similar (or several) that are (are) quite complete about ReportLab? (in English or Spanish)

I am learning and I need to do several things with ReportLab.

asked by Diana Carolina Hernandez 10.03.2016 в 21:25

1 answer


According to the definition of Image :


Image (filename, width = None, height = None)

Therefore, you may get what you want by doing this:

imagen = Image(url_logo, height=3*cm)

Even if you want to be more curious, you can install ipython and see how an instance of Image is created:

$ ipython
IPython 3.1.0 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object', use 'object??' for extra details.

In [1]: from reportlab.platypus import Image

In [2]: Image??

You will notice the definition of the method __init__ :

 def __init__(self, filename, width=None, height=None, kind='direct',
              mask="auto", lazy=1, hAlign='CENTER'):

You could even pass the hAlign attribute when instantiating the image:

imagen = Image(url_logo, height=3*cm, hAlign='RIGHT')

If you ask me, I always consult the official documentation of ReportLab and User Guide

answered by 10.03.2016 / 22:08