# [reportlab-users] set canvas dimensions to match text object

Robin Becker robin at reportlab.com
Fri Jun 23 10:11:30 EDT 2017

```I assume you are worrying about the extra space at the bottom, I tried your code
and just removed the line_spacing in the calculation of the total height.

However, I am guessing that your code is using the total line height to position
the text.

Logically, the text should be positioned using the base line as the reference ie
the initial y is then text_height - ascent, with this model we fix the leading
at line_height as well. If you want to add some slack just adjust ascent and/or
descent.

############################################################
from reportlab.pdfbase import pdfmetrics
from reportlab.pdfgen.canvas import Canvas

lines = [
"Lorem ipsum dolor sit amet",
"laboramus cu eos, sit intellegat sadipscing",
"Ut summo reprimique eum",
"propriae concludaturque",
]

ascent_slack = 1
descent_slack = 1
font_size = 16
font_name = 'Helvetica'
face = pdfmetrics.getFont(font_name).face
ascent = (face.ascent * font_size) / 1000.0 + ascent_slack
descent = (face.descent * font_size) / 1000.0 - descent_slack
line_height = ascent - descent

line_widths = []
for line in lines:
width = pdfmetrics.stringWidth(line, font_name, font_size)
line_widths.append(width)

text_width = max(line_widths)
text_height = line_height * len(lines)
canvas = Canvas("output.pdf", pagesize=(text_width, text_height))
print '|lines|=%d line_height=%d ascent=%d descent=%d text_height=%d' %
(len(lines), line_height, ascent, descent, text_height)

x = 0
y = text_height - ascent
text = canvas.beginText(x, y)
text.setFont(font_name, font_size)
for line in lines:
width_offset = (text_width - pdfmetrics.stringWidth(line, font_name,
font_size)) * 0.5
text.setTextOrigin(width_offset, text.getY())
text.textLine(line)

canvas.drawText(text)
canvas.showPage()
canvas.save()
############################################################

On 23/06/2017 14:46, gordon wrote:
> Good Afternoon,
>
> I am trying to create a pdf canvas that is the minimum dimensions to fully
> contain a text object containing arbitrary text of arbitrary lines.
>
> I have gotten close but I think I am missing something in my calculation
> for the height because my calculated heights are larger than the actual
> display result.
>
> Any tips on how to make my height calculation more accurate?
>
> Here is my render code:

--
Robin Becker
```
{"ft_lang_label":"__label__en","ft_lang_prob":0.6324013,"math_prob":0.938176,"size":2543,"snap":"2022-27-2022-33","text_gpt3_token_len":622,"char_repetition_ratio":0.174872,"word_repetition_ratio":0.0,"special_character_ratio":0.29020843,"punctuation_ratio":0.14797136,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.96287346,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2022-06-29T06:47:10Z\",\"WARC-Record-ID\":\"<urn:uuid:81983378-1261-46c7-b3ab-9801bc748bcd>\",\"Content-Length\":\"5564\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:4da58922-b6ec-41ac-a52e-2b56a068a2f7>\",\"WARC-Concurrent-To\":\"<urn:uuid:2fe7283a-2f36-4e43-92f5-9afefc004dad>\",\"WARC-IP-Address\":\"216.92.1.93\",\"WARC-Target-URI\":\"http://two.pairlist.net/pipermail/reportlab-users/2017-June/011666.html\",\"WARC-Payload-Digest\":\"sha1:G2LUJUWC2HXIBQTZRW4QXFH356L6ZZW2\",\"WARC-Block-Digest\":\"sha1:IFVRFKLU6RCDJEN62RO5PIBIRB42ZIRE\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2022/CC-MAIN-2022-27/CC-MAIN-2022-27_segments_1656103624904.34_warc_CC-MAIN-20220629054527-20220629084527-00491.warc.gz\"}"}