Advertisement
Guest User

Reportlab text centering with charspace

a guest
Feb 11th, 2011
2,735
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.86 KB | None | 0 0
  1. from reportlab.pdfbase.pdfmetrics import stringWidth
  2. from reportlab.pdfgen import canvas
  3. from reportlab.lib.units import mm
  4.  
  5.  
  6. c = canvas.Canvas('test.pdf', (195*mm, 50*mm))
  7. textobj = c.beginText()
  8.  
  9.  
  10. def width(string, font, size, charspace):
  11.     width = stringWidth(string, font, size)
  12.     width += (len(string) - 1) * charspace
  13.     return width
  14.  
  15.  
  16. textobj.setFont('Times-Roman', 12)
  17. textobj.setTextOrigin((100*mm - width("Some string", 'Times-Roman', 12, 3)) / 2, 5*mm)
  18. textobj.setCharSpace(3)
  19. textobj.textLine("Some string")
  20. c.drawText(textobj)
  21. c.rect(0*mm, 5*mm, 100*mm, 10*mm)
  22.  
  23.  
  24. textobj.setFont('Times-Roman', 15)
  25. textobj.setTextOrigin((100*mm - width("Some longer string", 'Times-Roman', 15, 7)) / 2, 25*mm)
  26. textobj.setCharSpace(7)
  27. textobj.textLine("Some longer string")
  28. c.drawText(textobj)
  29. c.rect(0*mm, 25*mm, 100*mm, 10*mm)
  30.  
  31.  
  32. c.showPage()
  33. c.save()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement