Advertisement
ALEXANDAR_GEORGIEV

balance_2

Jul 7th, 2022 (edited)
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 15.05 KB | None | 0 0
  1. from reportlab.lib.pagesizes import A4,landscape, portrait
  2. from reportlab.lib.units import mm
  3. from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
  4. from reportlab.lib.enums import TA_LEFT, TA_RIGHT, TA_CENTER, TA_JUSTIFY
  5. from reportlab.lib import colors
  6. from reportlab.pdfgen import *
  7. from reportlab.pdfgen.canvas import Canvas
  8. from reportlab.platypus import *
  9. from reportlab.platypus import Paragraph, Frame, Table, TableStyle, KeepInFrame, BaseDocTemplate, PageTemplate
  10. from reportlab.platypus import SimpleDocTemplate
  11. from reportlab.platypus import flowables
  12. from reportlab.platypus.paragraph import ParagraphStyle
  13. # from tkinter.font import *
  14. # font = Font(family= , size= , weight= , slant= , underline= , overstrike= )
  15. from reportlab.pdfbase.ttfonts import TTFont
  16. from reportlab.pdfbase import pdfmetrics
  17. from lib_data import data_table_a, list_style_a, data_table_p
  18. # Vertical elements size
  19. # -------------------------------------------
  20. styles = getSampleStyleSheet() #
  21. styleN = styles['BodyText']
  22. styleN.alignment = TA_LEFT
  23. styleBH = styles["Normal"]
  24. styleBH.alignment = TA_CENTER
  25. pdfmetrics.registerFont(TTFont('Times','Times.ttf', 'UTF-8'))
  26. pdfmetrics.registerFont(TTFont('Timesbd','Timesbd.ttf', 'UTF-8'))
  27. pdfmetrics.registerFont(TTFont('Timesi','Timesi.ttf', 'UTF-8'))
  28. pdfmetrics.registerFont(TTFont('Timesbi','Timesbi.ttf', 'UTF-8'))
  29. #pdfmetrics.registerFont(TTFont('Verdana', 'Verdana.ttf', 'UTF-8'))
  30. st = styles['Normal']
  31. styleTB = styles['BodyText']
  32. # -------------------------------------------
  33.  
  34.  
  35. def margins():    # Line coordinates
  36.     balance_2.setLineWidth(0.01)  # Дебелина на линията
  37.     balance_2.setStrokeColor('greenyellow')  # Цвят на линията
  38.     balance_2.line(left_m, bottom_m, left_m, A4[1] - top_m)  # Left margin
  39.     balance_2.line(left_m, A4[1] - top_m, A4[0] - right_m, A4[1] - top_m)  # Top margin
  40.     balance_2.line(A4[0] - right_m, A4[1] - top_m, A4[0] - right_m, bottom_m)  # Right margin
  41.     balance_2.line(A4[0] - right_m, bottom_m, left_m, bottom_m)  # Bottom margin
  42.     balance_2.setStrokeColor('red')  # Цвят на линията
  43.  
  44.  
  45. def frames(x0, y0, width, height, bound, id, story=None):
  46.     if story is None:
  47.         story = []
  48.     frame = Frame(x0, y0, width, height, showBoundary=bound, bottomPadding=0, topPadding=0, leftPadding=0, rightPadding=0)
  49.     frame.addFromList(story, balance_2)  # Показва frame
  50.     return frame
  51.  
  52.  
  53. top_m = 25*mm   # Дава 5 мм повече
  54. top_f = A4[1] * 0.1
  55. bottom_f_h = A4[1] * 0.1
  56. end_f_h = A4[1] * 0.122
  57. bottom_m = 25*mm    # Дава 5 мм повече
  58. balance_f = A4[1] - top_m - top_f - bottom_f_h - end_f_h - bottom_m
  59. # Horizontal elements size
  60. left_m = 20*mm   # 25mm / 210mm =  0.119
  61. right_m = 20*mm
  62. balance_f_l = (A4[0] - left_m - right_m) / 2    #A4[0] * 0.38
  63. balance_f_r = (A4[0] - left_m - right_m) / 2    #A4[0] * 0.38
  64. print(left_m)
  65. print(A4[0])
  66. print(left_m / A4[0])
  67. # Top_frame_left + coordinates
  68. top_f_l_width = (balance_f_l + balance_f_r) / 3
  69. # x1, y1 = left_m, A4[1] - top_m - top_f
  70. # x2, y2 = left_m + top_f_l_width, A4[1] - top_m
  71.  
  72. # Top_frame_right + coordinates
  73. top_f_r_width = top_f_l_width
  74.  
  75. # Bottom_frame_left
  76. bottom_f_l = top_f_r_width
  77. # Bottom_frame_right
  78. bottom_f_r = top_f_r_width
  79. # End_frame
  80. end_f_w = (balance_f_l + balance_f_r) * 0.6
  81.  
  82. balance_2 = Canvas('Balance_2.pdf', pagesize=A4)    # Balance_2.pdf
  83. # Margins
  84. margins()
  85. # Top frame left
  86. story = []
  87. story.append(Paragraph('"<font face=Timesbd>ОФИС ГЕОРГИЕВ ЕООД</font>"'))
  88. story.append(Paragraph('<font face=Times>4230 Асеновград, ул. "Княз Александър Батенберг", № 24</font>'))
  89. story.append(Paragraph('<br/><br/><font face=Times>Дата на съставяне:</font> <font face=Timesbd>19.06.2022 г.</font>'))
  90. frames(left_m, A4[1] - top_m - top_f, top_f_l_width, top_f, 0, 'top_frame_left', story)
  91. # Top frame right
  92. story = []
  93. story.append(Paragraph('<font face=Timesbd>ПРИЛОЖЕНИЕ</font><font face=Times> 1 към НСС 1</font>'))
  94. story.append(Paragraph('<font face=Timesbd>ЕИК:</font><font face=Times> 825 613 327</font>'))
  95. story.append(Paragraph('<font face=Timesbd>КИД:</font><font face=Times> 4120</font>'))
  96. story.append(Paragraph('<font face=Timesbd>Наименование:</font><font face=Times> Строителство на жилищни и нежилищни сгради</font>'))
  97. frames(A4[0] - right_m - top_f_r_width, A4[1] - top_m - top_f, top_f_r_width, top_f, 0, 'top_frame_right', story)
  98.  
  99. # # End frame
  100. # x1_end, y1_end = left_m + ((balance_f_l + balance_f_r) - end_f_w) / 2, A4[1] - top_m - top_f - balance_f - bottom_f_h - end_f_h
  101. # frames(x1_end, y1_end, end_f_w, end_f_h, 1, 'end_frame', None)
  102. title = 'СЧЕТОВОДЕН БАЛАНС'
  103. pdfmetrics.registerFont((TTFont('Timesbd', 'timesbd.ttf')))
  104. text_len = balance_2.stringWidth(title , "Timesbd", 9)
  105. balance_2.setFont('Timesbd', 9, leading=100)
  106. balance_2.drawString(left_m + balance_f_l - text_len/2, A4[1] - top_m - top_f + 20, title)
  107. # print(text_len)
  108. data_balance = 'към 31.12.2021г.'
  109. pdfmetrics.registerFont((TTFont('Times', 'times.ttf')))
  110. text_len = balance_2.stringWidth(data_balance, "Times", 9)  # Дава широчината на стринга
  111. balance_2.setFont('Times', 9, leading=100)
  112. balance_2.drawString(left_m + balance_f_l - text_len/2, A4[1] - top_m - top_f + 8, data_balance)
  113. # print(text_len)
  114. # ДЪЛЖИНА НА БАЛАНСА
  115. max_num_data_rows = 45
  116.     # Активи
  117. num_dat_rows = len(data_table_a)
  118. data_table_a1 = data_table_a
  119. data_ant = data_table_a[0: 5]
  120. data_table_a2 = []  # Таблица за страница 2
  121. if num_dat_rows > max_num_data_rows:
  122.     data_table_a1 = data_table_a[0: max_num_data_rows - 1]  # от 0 до 44 = общо 45
  123.     data_table_a2 = data_ant + data_table_a[max_num_data_rows:]  # от 45 до края
  124.     # Пасиви
  125. num_dat_rows_p = len(data_table_p)
  126. data_table_p1 = data_table_p
  127. data_ant_p = data_table_p[0: 5]
  128. data_table_p2 = []  # Таблица за страница 2
  129. if num_dat_rows_p > max_num_data_rows:
  130.     data_table_p1 = data_table_p[0: max_num_data_rows - 1]  # от 0 до 44 = общо 45
  131.     data_table_p2 = data_ant_p + data_table_p[max_num_data_rows:]  # от 45 до края
  132. # БАЛАНС АКТИВИ
  133. ta = Table(
  134.     data_table_a1,
  135.     colWidths=[(balance_f_l - 58), 29, 29],    # 234.13779527559058
  136.     rowHeights=4*mm,
  137.     )
  138. w, h = ta.wrap(0, 0)    # Дава размерите на таблицата
  139. print(w, ' ', h)    # 240.94488188976382   419.52755905511816
  140. story_a = []
  141. ta.setStyle(list_style_a)
  142. story_a.append(ta)
  143. # ----------------------------------------
  144. # Брой редове в таблицата на Balance_frame_left
  145. count_data_rows = len(data_table_a)
  146. print(count_data_rows)  # 37
  147. # Balance frame left
  148. x1 = left_m                         # x начало на Balance_frame_left
  149. y1 = A4[1] - top_m - top_f - h      # y начало на Balance_frame_left
  150. balance_frame_left_height = h
  151. frames(x1, y1, balance_f_l, balance_frame_left_height, 1, 'balance_frame_left', story_a)
  152. # БАЛАНС ПАСИВИ
  153. tp = Table(
  154.     data_table_p1,
  155.     colWidths=[(balance_f_r - 58), 29, 29],    # 234.13779527559058
  156.     rowHeights=4*mm,
  157.     )
  158. w_p, h_p = ta.wrap(0, 0)    # Дава размерите на таблицата
  159. print(w_p, ' ', h_p)    # 240.94488188976382   419.52755905511816
  160. story_p = []
  161. tp.setStyle(list_style_a)
  162. story_p.append(tp)
  163. # ----------------------------------------
  164. # Брой редове в таблицата на Balance_frame_right
  165. count_data_rows_p = len(data_table_p)
  166. print(count_data_rows)  # 37
  167. # Balance frame right
  168. y1_p = A4[1] - top_m - top_f - h_p      # y начало на Balance_frame_left
  169. x1_r_f = left_m + balance_f_r
  170. balance_frame_right_height = h_p
  171. frames(x1_r_f, y1_p, balance_f_r, balance_frame_right_height, 1, 'balance_frame_right', story_p)
  172.  
  173. if count_data_rows > 45:
  174.     page_num = balance_2.getPageNumber()
  175.     end_page = 'Страница 1 от 2'
  176.     pdfmetrics.registerFont((TTFont('Timesbs', 'timesbi.ttf')))
  177.     text_len = balance_2.stringWidth(end_page, "Timesbi", 10)
  178.     balance_2.setFont('Timesbi', 9, leading=100)
  179.     balance_2.drawString(left_m + balance_f_l - text_len / 2, y1 - 40, f'Страница {page_num} от 2')
  180.     balance_2.showPage()
  181.     margins()
  182.     # ------------------------------------------------
  183.     pdfmetrics.registerFont(TTFont('Times', 'Times.ttf', 'UTF-8'))
  184.     pdfmetrics.registerFont(TTFont('Timesbd', 'Timesbd.ttf', 'UTF-8'))
  185.     pdfmetrics.registerFont(TTFont('Timesi', 'Timesi.ttf', 'UTF-8'))
  186.     pdfmetrics.registerFont(TTFont('Timesbi', 'Timesbi.ttf', 'UTF-8'))
  187.     # pdfmetrics.registerFont(TTFont('Verdana', 'Verdana.ttf', 'UTF-8'))
  188.     st = styles['Normal']
  189.     # ------------------------------------------------
  190.     # Top frame left
  191.     story = []
  192.     story.append(Paragraph('"<font face=Timesbd>ОФИС ГЕОРГИЕВ ЕООД</font>"'))
  193.     story.append(Paragraph('<font face=Times>4230 Асеновград, ул. "Княз Александър Батенберг", № 24</font>'))
  194.     story.append(
  195.         Paragraph('<br/><br/><font face=Times>Дата на съставяне:</font> <font face=Timesbd>19.06.2022 г.</font>'))
  196.     frames(left_m, A4[1] - top_m - top_f, top_f_l_width, top_f, 0, 'top_frame_left', story)
  197.     # Top frame right
  198.     story = []
  199.     story.append(Paragraph('<font face=Timesbd>ПРИЛОЖЕНИЕ</font><font face=Times> 1 към НСС 1</font>'))
  200.     story.append(Paragraph('<font face=Timesbd>ЕИК:</font><font face=Times> 825 613 327</font>'))
  201.     story.append(Paragraph('<font face=Timesbd>КИД:</font><font face=Times> 4120</font>'))
  202.     story.append(Paragraph(
  203.         '<font face=Timesbd>Наименование:</font><font face=Times> Строителство на жилищни и нежилищни сгради</font>'))
  204.     frames(A4[0] - right_m - top_f_r_width, A4[1] - top_m - top_f, top_f_r_width, top_f, 0, 'top_frame_right', story)
  205.     title = 'СЧЕТОВОДЕН БАЛАНС'
  206.     pdfmetrics.registerFont((TTFont('Timesbd', 'timesbd.ttf')))
  207.     text_len = balance_2.stringWidth(title, "Timesbd", 9)
  208.     balance_2.setFont('Timesbd', 9, leading=100)
  209.     balance_2.drawString(left_m + balance_f_l - text_len / 2, A4[1] - top_m - top_f + 20, title)
  210.     # print(text_len)
  211.  
  212.     data_balance = 'към 31.12.2021г.'
  213.     pdfmetrics.registerFont((TTFont('Times', 'times.ttf')))
  214.     text_len = balance_2.stringWidth(data_balance, "Times", 9)  # Дава широчината на стринга
  215.     balance_2.setFont('Times', 9, leading=100)
  216.     balance_2.drawString(left_m + balance_f_l - text_len / 2, A4[1] - top_m - top_f + 8, data_balance)
  217.     # Таблица Актив стр. 2
  218.     ta2 = Table(
  219.         data_table_a2,
  220.         colWidths=[(balance_f_l - 58), 29, 29],  # 234.13779527559058
  221.         rowHeights=4 * mm,
  222.     )
  223.     w, h = ta2.wrap(0, 0)  # Дава размерите на таблицата
  224.     print(w, ' ', h)  # 240.94488188976382   419.52755905511816
  225.     story_a_2 = []
  226.     ta2.setStyle(list_style_a)
  227.     story_a_2.append(ta2)
  228.     # Balance frame left
  229.     x1 = left_m  # x начало на Balance_frame_left
  230.     y1 = A4[1] - top_m - top_f - h  # y начало на Balance_frame_left
  231.     balance_frame_left_height = h
  232.     frames(x1, y1, balance_f_l, balance_frame_left_height, 1, 'balance_frame_left_2', story_a_2)
  233.  
  234.     # Таблица Пасив стр. 2
  235.     tp2 = Table(
  236.         data_table_p2,
  237.         colWidths=[(balance_f_r - 58), 29, 29],  # 234.13779527559058
  238.         rowHeights= 4 * mm
  239.     )
  240.     w_p, h_p = tp2.wrap(0, 0)  # Дава размерите на таблицата
  241.     print(w_p, ' ', h_p)  # 240.94488188976382   419.52755905511816
  242.     story_p_2 = []
  243.     tp2.setStyle(list_style_a)
  244.     story_p_2.append(tp2)
  245.     # Balance frame right
  246.    # x1 = left_m  # x начало на Balance_frame_left
  247.     y1_p = A4[1] - top_m - top_f - h_p  # y начало на Balance_frame_right
  248.     balance_frame_right_height = h_p
  249.     frames(x1_r_f, y1_p, balance_f_r, balance_frame_right_height, 1, 'balance_frame_right_2', story_p_2)
  250.  
  251.  
  252.     # Bottom frame left
  253.     story = []
  254.     story.append(Paragraph('<font face=Timesbd>Ръководител: .......................</font>'))
  255.     story.append(Paragraph('<font face=Times>АЛЕКСАНДЪР ГЕОРГИЕВ ГЕОРГИЕВ</font>'))
  256.     y1 = A4[1] - top_m - top_f - balance_frame_left_height - bottom_f_h - 10 * mm
  257.     frames(x1, y1, balance_f_l, bottom_f_h, 0, 'bottom_frame_left', story)
  258.     # # Bottom frame right
  259.     story = []
  260.     story.append(Paragraph('<font face=Timesbd>Съставител: .......................</font>'))
  261.     story.append(Paragraph('<font face=Times>СП "ОФИС ГЕОРГИЕВ ЕООД"</font>'))
  262.     story.append(Paragraph('<font face=Times>4230 Асеновград, ул."Цар Иван Асен II, № 47"</font>'))
  263.     story.append(Paragraph('<font face=Times>ЕИК: 115 882 107</font>'))
  264.     story.append(Paragraph('<font face=Timesbd>Представляващ: .......................</font>'))
  265.     story.append(Paragraph('<font face=Times>АЛЕКСАНДЪР ГЕОРГИЕВ ГЕОРГИЕВ</font>'))
  266.  
  267.     x1_b_f_r, y1_b_f_r = left_m + balance_f_l, A4[1] - top_m - top_f - balance_f - bottom_f_h
  268.     frames(x1_b_f_r, y1, balance_f_r, bottom_f_h, 0, 'bottom_frame_right', story)
  269.     # Страница 2 от 2
  270.     page_num = balance_2.getPageNumber()
  271.     end_page = 'Страница 1 от 2'
  272.     pdfmetrics.registerFont((TTFont('Timesbs', 'timesbi.ttf')))
  273.     text_len = balance_2.stringWidth(end_page, "Timesbi", 10)
  274.     balance_2.setFont('Timesbi', 9, leading=100)
  275.     balance_2.drawString(left_m + balance_f_l - text_len / 2, bottom_m + 50, f'Страница {page_num} от 2')
  276.  
  277. else:
  278.     #Bottom frame left
  279.     story = []
  280.     story.append(Paragraph('<font face=Timesbd>Ръководител: .......................</font>'))
  281.     story.append(Paragraph('<font face=Times>АЛЕКСАНДЪР ГЕОРГИЕВ ГЕОРГИЕВ</font>'))
  282.     y1 = A4[1] - top_m - top_f - balance_frame_left_height - bottom_f_h - 10 * mm
  283.     frames(x1, y1, balance_f_l, bottom_f_h, 0, 'bottom_frame_left', story)
  284.     # # Bottom frame right
  285.     story = []
  286.     story.append(Paragraph('<font face=Timesbd>Съставител: .......................</font>'))
  287.     story.append(Paragraph('<font face=Times>СП "ОФИС ГЕОРГИЕВ ЕООД"</font>'))
  288.     story.append(Paragraph('<font face=Times>4230 Асеновград, ул."Цар Иван Асен II, № 47"</font>'))
  289.     story.append(Paragraph('<font face=Times>ЕИК: 115 882 107</font>'))
  290.     story.append(Paragraph('<font face=Timesbd>Представляващ: .......................</font>'))
  291.     story.append(Paragraph('<font face=Times>АЛЕКСАНДЪР ГЕОРГИЕВ ГЕОРГИЕВ</font>'))
  292.  
  293.     x1_b_f_r, y1_b_f_r = left_m + balance_f_l, A4[1] - top_m - top_f - balance_f - bottom_f_h
  294.     frames(x1_b_f_r, y1, balance_f_r, bottom_f_h, 0, 'bottom_frame_right', story)
  295.  
  296. balance_2.showPage()  # saves current page
  297. balance_2.save()  # stores the file and close the canvas
  298.  
  299.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement