[reportlab-users] set canvas dimensions to match text object
gordon
wgordonw1 at gmail.com
Fri Jun 23 16:28:45 EDT 2017
Hello Robin,
This is very much appreciated!
Thanks,
Gordon
On Fri, Jun 23, 2017 at 10:11 AM, Robin Becker <robin at reportlab.com> wrote:
>
> 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.
>
>
> My version with slight adjustments
> ############################################################
> 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)
> text.setLeading(line_height)
> 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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://pairlist2.pair.net/pipermail/reportlab-users/attachments/20170623/4d83bc5b/attachment.html>
More information about the reportlab-users
mailing list