Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from reportlab.lib.pagesizes import A4,landscape, portrait
- from reportlab.lib.units import mm
- from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
- from reportlab.lib.enums import TA_LEFT, TA_RIGHT, TA_CENTER, TA_JUSTIFY
- from reportlab.lib import colors
- from reportlab.pdfgen import *
- from reportlab.pdfgen.canvas import Canvas
- from reportlab.platypus import *
- from reportlab.platypus import Paragraph, Frame, Table, TableStyle, KeepInFrame, BaseDocTemplate, PageTemplate
- from reportlab.platypus import SimpleDocTemplate
- from reportlab.platypus import flowables
- from reportlab.platypus.paragraph import ParagraphStyle
- # from tkinter.font import *
- # font = Font(family= , size= , weight= , slant= , underline= , overstrike= )
- from reportlab.pdfbase.ttfonts import TTFont
- from reportlab.pdfbase import pdfmetrics
- from lib_data import data_table_a, list_style_a, data_table_p
- # Vertical elements size
- # -------------------------------------------
- styles = getSampleStyleSheet() #
- styleN = styles['BodyText']
- styleN.alignment = TA_LEFT
- styleBH = styles["Normal"]
- styleBH.alignment = TA_CENTER
- pdfmetrics.registerFont(TTFont('Times','Times.ttf', 'UTF-8'))
- pdfmetrics.registerFont(TTFont('Timesbd','Timesbd.ttf', 'UTF-8'))
- pdfmetrics.registerFont(TTFont('Timesi','Timesi.ttf', 'UTF-8'))
- pdfmetrics.registerFont(TTFont('Timesbi','Timesbi.ttf', 'UTF-8'))
- #pdfmetrics.registerFont(TTFont('Verdana', 'Verdana.ttf', 'UTF-8'))
- st = styles['Normal']
- styleTB = styles['BodyText']
- # -------------------------------------------
- def margins(): # Line coordinates
- balance_2.setLineWidth(0.01) # Дебелина на линията
- balance_2.setStrokeColor('greenyellow') # Цвят на линията
- balance_2.line(left_m, bottom_m, left_m, A4[1] - top_m) # Left margin
- balance_2.line(left_m, A4[1] - top_m, A4[0] - right_m, A4[1] - top_m) # Top margin
- balance_2.line(A4[0] - right_m, A4[1] - top_m, A4[0] - right_m, bottom_m) # Right margin
- balance_2.line(A4[0] - right_m, bottom_m, left_m, bottom_m) # Bottom margin
- balance_2.setStrokeColor('red') # Цвят на линията
- def frames(x0, y0, width, height, bound, id, story=None):
- if story is None:
- story = []
- frame = Frame(x0, y0, width, height, showBoundary=bound, bottomPadding=0, topPadding=0, leftPadding=0, rightPadding=0)
- frame.addFromList(story, balance_2) # Показва frame
- return frame
- top_m = 25*mm # Дава 5 мм повече
- top_f = A4[1] * 0.1
- bottom_f_h = A4[1] * 0.1
- end_f_h = A4[1] * 0.122
- bottom_m = 25*mm # Дава 5 мм повече
- balance_f = A4[1] - top_m - top_f - bottom_f_h - end_f_h - bottom_m
- # Horizontal elements size
- left_m = 20*mm # 25mm / 210mm = 0.119
- right_m = 20*mm
- balance_f_l = (A4[0] - left_m - right_m) / 2 #A4[0] * 0.38
- balance_f_r = (A4[0] - left_m - right_m) / 2 #A4[0] * 0.38
- print(left_m)
- print(A4[0])
- print(left_m / A4[0])
- # Top_frame_left + coordinates
- top_f_l_width = (balance_f_l + balance_f_r) / 3
- # x1, y1 = left_m, A4[1] - top_m - top_f
- # x2, y2 = left_m + top_f_l_width, A4[1] - top_m
- # Top_frame_right + coordinates
- top_f_r_width = top_f_l_width
- # Bottom_frame_left
- bottom_f_l = top_f_r_width
- # Bottom_frame_right
- bottom_f_r = top_f_r_width
- # End_frame
- end_f_w = (balance_f_l + balance_f_r) * 0.6
- balance_2 = Canvas('Balance_2.pdf', pagesize=A4) # Balance_2.pdf
- # Margins
- margins()
- # Top frame left
- story = []
- story.append(Paragraph('"<font face=Timesbd>ОФИС ГЕОРГИЕВ ЕООД</font>"'))
- story.append(Paragraph('<font face=Times>4230 Асеновград, ул. "Княз Александър Батенберг", № 24</font>'))
- story.append(Paragraph('<br/><br/><font face=Times>Дата на съставяне:</font> <font face=Timesbd>19.06.2022 г.</font>'))
- frames(left_m, A4[1] - top_m - top_f, top_f_l_width, top_f, 0, 'top_frame_left', story)
- # Top frame right
- story = []
- story.append(Paragraph('<font face=Timesbd>ПРИЛОЖЕНИЕ</font><font face=Times> 1 към НСС 1</font>'))
- story.append(Paragraph('<font face=Timesbd>ЕИК:</font><font face=Times> 825 613 327</font>'))
- story.append(Paragraph('<font face=Timesbd>КИД:</font><font face=Times> 4120</font>'))
- story.append(Paragraph('<font face=Timesbd>Наименование:</font><font face=Times> Строителство на жилищни и нежилищни сгради</font>'))
- 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)
- # # End frame
- # 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
- # frames(x1_end, y1_end, end_f_w, end_f_h, 1, 'end_frame', None)
- title = 'СЧЕТОВОДЕН БАЛАНС'
- pdfmetrics.registerFont((TTFont('Timesbd', 'timesbd.ttf')))
- text_len = balance_2.stringWidth(title , "Timesbd", 9)
- balance_2.setFont('Timesbd', 9, leading=100)
- balance_2.drawString(left_m + balance_f_l - text_len/2, A4[1] - top_m - top_f + 20, title)
- # print(text_len)
- data_balance = 'към 31.12.2021г.'
- pdfmetrics.registerFont((TTFont('Times', 'times.ttf')))
- text_len = balance_2.stringWidth(data_balance, "Times", 9) # Дава широчината на стринга
- balance_2.setFont('Times', 9, leading=100)
- balance_2.drawString(left_m + balance_f_l - text_len/2, A4[1] - top_m - top_f + 8, data_balance)
- # print(text_len)
- # ДЪЛЖИНА НА БАЛАНСА
- max_num_data_rows = 45
- # Активи
- num_dat_rows = len(data_table_a)
- data_table_a1 = data_table_a
- data_ant = data_table_a[0: 5]
- data_table_a2 = [] # Таблица за страница 2
- if num_dat_rows > max_num_data_rows:
- data_table_a1 = data_table_a[0: max_num_data_rows - 1] # от 0 до 44 = общо 45
- data_table_a2 = data_ant + data_table_a[max_num_data_rows:] # от 45 до края
- # Пасиви
- num_dat_rows_p = len(data_table_p)
- data_table_p1 = data_table_p
- data_ant_p = data_table_p[0: 5]
- data_table_p2 = [] # Таблица за страница 2
- if num_dat_rows_p > max_num_data_rows:
- data_table_p1 = data_table_p[0: max_num_data_rows - 1] # от 0 до 44 = общо 45
- data_table_p2 = data_ant_p + data_table_p[max_num_data_rows:] # от 45 до края
- # БАЛАНС АКТИВИ
- ta = Table(
- data_table_a1,
- colWidths=[(balance_f_l - 58), 29, 29], # 234.13779527559058
- rowHeights=4*mm,
- )
- w, h = ta.wrap(0, 0) # Дава размерите на таблицата
- print(w, ' ', h) # 240.94488188976382 419.52755905511816
- story_a = []
- ta.setStyle(list_style_a)
- story_a.append(ta)
- # ----------------------------------------
- # Брой редове в таблицата на Balance_frame_left
- count_data_rows = len(data_table_a)
- print(count_data_rows) # 37
- # Balance frame left
- x1 = left_m # x начало на Balance_frame_left
- y1 = A4[1] - top_m - top_f - h # y начало на Balance_frame_left
- balance_frame_left_height = h
- frames(x1, y1, balance_f_l, balance_frame_left_height, 1, 'balance_frame_left', story_a)
- # БАЛАНС ПАСИВИ
- tp = Table(
- data_table_p1,
- colWidths=[(balance_f_r - 58), 29, 29], # 234.13779527559058
- rowHeights=4*mm,
- )
- w_p, h_p = ta.wrap(0, 0) # Дава размерите на таблицата
- print(w_p, ' ', h_p) # 240.94488188976382 419.52755905511816
- story_p = []
- tp.setStyle(list_style_a)
- story_p.append(tp)
- # ----------------------------------------
- # Брой редове в таблицата на Balance_frame_right
- count_data_rows_p = len(data_table_p)
- print(count_data_rows) # 37
- # Balance frame right
- y1_p = A4[1] - top_m - top_f - h_p # y начало на Balance_frame_left
- x1_r_f = left_m + balance_f_r
- balance_frame_right_height = h_p
- frames(x1_r_f, y1_p, balance_f_r, balance_frame_right_height, 1, 'balance_frame_right', story_p)
- if count_data_rows > 45:
- page_num = balance_2.getPageNumber()
- end_page = 'Страница 1 от 2'
- pdfmetrics.registerFont((TTFont('Timesbs', 'timesbi.ttf')))
- text_len = balance_2.stringWidth(end_page, "Timesbi", 10)
- balance_2.setFont('Timesbi', 9, leading=100)
- balance_2.drawString(left_m + balance_f_l - text_len / 2, y1 - 40, f'Страница {page_num} от 2')
- balance_2.showPage()
- margins()
- # ------------------------------------------------
- pdfmetrics.registerFont(TTFont('Times', 'Times.ttf', 'UTF-8'))
- pdfmetrics.registerFont(TTFont('Timesbd', 'Timesbd.ttf', 'UTF-8'))
- pdfmetrics.registerFont(TTFont('Timesi', 'Timesi.ttf', 'UTF-8'))
- pdfmetrics.registerFont(TTFont('Timesbi', 'Timesbi.ttf', 'UTF-8'))
- # pdfmetrics.registerFont(TTFont('Verdana', 'Verdana.ttf', 'UTF-8'))
- st = styles['Normal']
- # ------------------------------------------------
- # Top frame left
- story = []
- story.append(Paragraph('"<font face=Timesbd>ОФИС ГЕОРГИЕВ ЕООД</font>"'))
- story.append(Paragraph('<font face=Times>4230 Асеновград, ул. "Княз Александър Батенберг", № 24</font>'))
- story.append(
- Paragraph('<br/><br/><font face=Times>Дата на съставяне:</font> <font face=Timesbd>19.06.2022 г.</font>'))
- frames(left_m, A4[1] - top_m - top_f, top_f_l_width, top_f, 0, 'top_frame_left', story)
- # Top frame right
- story = []
- story.append(Paragraph('<font face=Timesbd>ПРИЛОЖЕНИЕ</font><font face=Times> 1 към НСС 1</font>'))
- story.append(Paragraph('<font face=Timesbd>ЕИК:</font><font face=Times> 825 613 327</font>'))
- story.append(Paragraph('<font face=Timesbd>КИД:</font><font face=Times> 4120</font>'))
- story.append(Paragraph(
- '<font face=Timesbd>Наименование:</font><font face=Times> Строителство на жилищни и нежилищни сгради</font>'))
- 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)
- title = 'СЧЕТОВОДЕН БАЛАНС'
- pdfmetrics.registerFont((TTFont('Timesbd', 'timesbd.ttf')))
- text_len = balance_2.stringWidth(title, "Timesbd", 9)
- balance_2.setFont('Timesbd', 9, leading=100)
- balance_2.drawString(left_m + balance_f_l - text_len / 2, A4[1] - top_m - top_f + 20, title)
- # print(text_len)
- data_balance = 'към 31.12.2021г.'
- pdfmetrics.registerFont((TTFont('Times', 'times.ttf')))
- text_len = balance_2.stringWidth(data_balance, "Times", 9) # Дава широчината на стринга
- balance_2.setFont('Times', 9, leading=100)
- balance_2.drawString(left_m + balance_f_l - text_len / 2, A4[1] - top_m - top_f + 8, data_balance)
- # Таблица Актив стр. 2
- ta2 = Table(
- data_table_a2,
- colWidths=[(balance_f_l - 58), 29, 29], # 234.13779527559058
- rowHeights=4 * mm,
- )
- w, h = ta2.wrap(0, 0) # Дава размерите на таблицата
- print(w, ' ', h) # 240.94488188976382 419.52755905511816
- story_a_2 = []
- ta2.setStyle(list_style_a)
- story_a_2.append(ta2)
- # Balance frame left
- x1 = left_m # x начало на Balance_frame_left
- y1 = A4[1] - top_m - top_f - h # y начало на Balance_frame_left
- balance_frame_left_height = h
- frames(x1, y1, balance_f_l, balance_frame_left_height, 1, 'balance_frame_left_2', story_a_2)
- # Таблица Пасив стр. 2
- tp2 = Table(
- data_table_p2,
- colWidths=[(balance_f_r - 58), 29, 29], # 234.13779527559058
- rowHeights= 4 * mm
- )
- w_p, h_p = tp2.wrap(0, 0) # Дава размерите на таблицата
- print(w_p, ' ', h_p) # 240.94488188976382 419.52755905511816
- story_p_2 = []
- tp2.setStyle(list_style_a)
- story_p_2.append(tp2)
- # Balance frame right
- # x1 = left_m # x начало на Balance_frame_left
- y1_p = A4[1] - top_m - top_f - h_p # y начало на Balance_frame_right
- balance_frame_right_height = h_p
- frames(x1_r_f, y1_p, balance_f_r, balance_frame_right_height, 1, 'balance_frame_right_2', story_p_2)
- # Bottom frame left
- story = []
- story.append(Paragraph('<font face=Timesbd>Ръководител: .......................</font>'))
- story.append(Paragraph('<font face=Times>АЛЕКСАНДЪР ГЕОРГИЕВ ГЕОРГИЕВ</font>'))
- y1 = A4[1] - top_m - top_f - balance_frame_left_height - bottom_f_h - 10 * mm
- frames(x1, y1, balance_f_l, bottom_f_h, 0, 'bottom_frame_left', story)
- # # Bottom frame right
- story = []
- story.append(Paragraph('<font face=Timesbd>Съставител: .......................</font>'))
- story.append(Paragraph('<font face=Times>СП "ОФИС ГЕОРГИЕВ ЕООД"</font>'))
- story.append(Paragraph('<font face=Times>4230 Асеновград, ул."Цар Иван Асен II, № 47"</font>'))
- story.append(Paragraph('<font face=Times>ЕИК: 115 882 107</font>'))
- story.append(Paragraph('<font face=Timesbd>Представляващ: .......................</font>'))
- story.append(Paragraph('<font face=Times>АЛЕКСАНДЪР ГЕОРГИЕВ ГЕОРГИЕВ</font>'))
- x1_b_f_r, y1_b_f_r = left_m + balance_f_l, A4[1] - top_m - top_f - balance_f - bottom_f_h
- frames(x1_b_f_r, y1, balance_f_r, bottom_f_h, 0, 'bottom_frame_right', story)
- # Страница 2 от 2
- page_num = balance_2.getPageNumber()
- end_page = 'Страница 1 от 2'
- pdfmetrics.registerFont((TTFont('Timesbs', 'timesbi.ttf')))
- text_len = balance_2.stringWidth(end_page, "Timesbi", 10)
- balance_2.setFont('Timesbi', 9, leading=100)
- balance_2.drawString(left_m + balance_f_l - text_len / 2, bottom_m + 50, f'Страница {page_num} от 2')
- else:
- #Bottom frame left
- story = []
- story.append(Paragraph('<font face=Timesbd>Ръководител: .......................</font>'))
- story.append(Paragraph('<font face=Times>АЛЕКСАНДЪР ГЕОРГИЕВ ГЕОРГИЕВ</font>'))
- y1 = A4[1] - top_m - top_f - balance_frame_left_height - bottom_f_h - 10 * mm
- frames(x1, y1, balance_f_l, bottom_f_h, 0, 'bottom_frame_left', story)
- # # Bottom frame right
- story = []
- story.append(Paragraph('<font face=Timesbd>Съставител: .......................</font>'))
- story.append(Paragraph('<font face=Times>СП "ОФИС ГЕОРГИЕВ ЕООД"</font>'))
- story.append(Paragraph('<font face=Times>4230 Асеновград, ул."Цар Иван Асен II, № 47"</font>'))
- story.append(Paragraph('<font face=Times>ЕИК: 115 882 107</font>'))
- story.append(Paragraph('<font face=Timesbd>Представляващ: .......................</font>'))
- story.append(Paragraph('<font face=Times>АЛЕКСАНДЪР ГЕОРГИЕВ ГЕОРГИЕВ</font>'))
- x1_b_f_r, y1_b_f_r = left_m + balance_f_l, A4[1] - top_m - top_f - balance_f - bottom_f_h
- frames(x1_b_f_r, y1, balance_f_r, bottom_f_h, 0, 'bottom_frame_right', story)
- balance_2.showPage() # saves current page
- balance_2.save() # stores the file and close the canvas
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement