exploring & testing reportlab to make PDFs with Python
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

exploring.py 2.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. # https://www.youtube.com/watch?v=ZDR7-iSuwkQ
  2. from reportlab.pdfgen import canvas
  3. from reportlab.lib.units import cm, mm
  4. from reportlab.lib import colors
  5. filename = 'exploring.pdf'
  6. documenttitle = 'Testing'
  7. title = 'Hello Reportlab'
  8. subtitle = 'How are you doing?'
  9. authors = 'You and Me and Many Others too'
  10. textlines = [
  11. 'I am testing you...',
  12. 'I am testing you...',
  13. 'I am testing you...',
  14. 'What a day, no?',
  15. 'What a day, no?',
  16. 'What a day, no?',
  17. 'What a day, no?',
  18. 'What a day, no?',
  19. 'What a day, no?',
  20. 'Now we are together.'
  21. ]
  22. image = 'tutorial-logo.png'
  23. def drawRuler(pdf):
  24. pdf.setFont('Courier', 8)
  25. pdf.setFillColor(colors.blue)
  26. # pdf.drawString(0*mm, 0*mm, 'xy10')
  27. # pdf.drawString(10*mm, 10*mm, 'xy10')
  28. pdf.drawString(20*mm, 160*mm, 'x20')
  29. pdf.drawString(40*mm, 160*mm, 'x40')
  30. pdf.drawString(60*mm, 160*mm, 'x60')
  31. pdf.drawString(80*mm, 160*mm, 'x80')
  32. pdf.drawString(100*mm, 160*mm, 'x100')
  33. pdf.drawString(5*mm, 20*mm, 'y20')
  34. pdf.drawString(5*mm, 40*mm, 'y40')
  35. pdf.drawString(5*mm, 60*mm, 'y60')
  36. pdf.drawString(5*mm, 80*mm, 'y80')
  37. pdf.drawString(5*mm, 100*mm, 'y100')
  38. pdf.drawString(5*mm, 120*mm, 'y120')
  39. pdf.drawString(5*mm, 140*mm, 'y140')
  40. pdf.drawString(5*mm, 160*mm, 'y160')
  41. pdf = canvas.Canvas(filename)
  42. pdf.setPageSize([110*mm, 170*mm])
  43. pdf.setTitle(documenttitle)
  44. pdf.setAuthor(authors)
  45. drawRuler(pdf)
  46. # for font in pdf.getAvailableFonts():
  47. # print(font)
  48. from reportlab.pdfbase.ttfonts import TTFont
  49. from reportlab.pdfbase import pdfmetrics
  50. pdfmetrics.registerFont(TTFont('stan', 'StanleySmith-Broman.ttf'))
  51. pdfmetrics.registerFont(TTFont('concrete-reg', 'cmunorm.ttf'))
  52. pdfmetrics.registerFont(TTFont('concrete-it', 'cmunoti.ttf'))
  53. # Title
  54. pdf.setFillColor(colors.black)
  55. pdf.setFont('stan', 32)
  56. # pdf.drawString(80, 720, title) # x, y (counting from bottom-left)
  57. pdf.drawCentredString(60*mm, 150*mm, title) # x, y (counting from middle point string)
  58. # Subtitle
  59. pdf.setFillColorRGB(255, 0, 255)
  60. pdf.setFont('concrete-it', 16)
  61. pdf.drawCentredString(60*mm, 140*mm, subtitle)
  62. # Line
  63. pdf.setLineWidth(50)
  64. pdf.line(20*mm, 100*mm, 100*mm, 115*mm) # start-x, start-y, end-x, end-y
  65. # Text
  66. pdf.setFont('concrete-reg', 9)
  67. pdf.setFillColor(colors.red)
  68. text = pdf.beginText(20*mm, 70*mm)
  69. for line in textlines:
  70. text.textLine(line)
  71. pdf.drawText(text)
  72. # Image
  73. pdf.drawInlineImage(image, 70*mm, 40*mm, width=25*mm, height=25*mm) # x, y
  74. # Other
  75. pagenumber = pdf.getPageNumber()
  76. pdf.drawCentredString(60*mm, 15*mm, str(pagenumber))
  77. # Save
  78. pdf.save()