Advertisement
ALEXANDAR_GEORGIEV

vertical_pdf

Aug 1st, 2022 (edited)
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 7.03 KB | None | 0 0
  1. # vertical_text_table.py
  2. from reportlab.pdfbase import pdfmetrics
  3. from reportlab.pdfbase.ttfonts import TTFont
  4. from reportlab.lib import colors
  5. from reportlab import *
  6. from reportlab.lib.colors import HexColor
  7. from reportlab.lib.pagesizes import letter
  8. from reportlab.lib.styles import getSampleStyleSheet
  9. from reportlab.lib.units import inch
  10. from reportlab.platypus import (BaseDocTemplate, Frame, Paragraph, NextPageTemplate,
  11.       PageBreak, PageTemplate, Image, Table, TableStyle, Spacer)
  12. #from rotatedtext import verticalText
  13. from reportlab.platypus.flowables import Flowable
  14. from reportlab.pdfgen import canvas
  15.  
  16. import textwrap
  17.  
  18.  
  19. pdfmetrics.registerFont(TTFont('Times','Times.ttf', 'UTF-8'))
  20. pdfmetrics.registerFont(TTFont('Timesbd','Timesbd.ttf', 'UTF-8'))
  21. pdfmetrics.registerFont(TTFont('Timesi','Timesi.ttf', 'UTF-8'))
  22. pdfmetrics.registerFont(TTFont('Timesbi','Timesbi.ttf', 'UTF-8'))
  23.  
  24.  
  25.  
  26. class verticalText(Flowable): # Rotates a text in a table cell.
  27.  
  28.     def __init__(self, text):
  29.         Flowable.__init__(self)
  30.         self.text = text
  31.  
  32.     def draw(self):
  33.         canvas = self.canv
  34.         canvas.rotate(90)
  35.         fs = canvas._fontsize
  36.         # Измества канваса малко надясно
  37.         canvas.translate(1, -fs/1.2)  # canvas._leading?
  38.         canvas.drawString(0, 0, self.text)
  39.  
  40.     def wrap(self, aW, aH):
  41.         canv = self.canv
  42.         fn, fs = canv._fontname, canv._fontsize
  43.         return canv._leading, 1 + canv.stringWidth(self.text, fn, fs)
  44.  
  45.  
  46. class verticalText1(Flowable): # Rotates a text in a table cell.
  47.  
  48.     def __init__(self, text):
  49.         Flowable.__init__(self)
  50.         self.text = text
  51.  
  52.     def draw(self):
  53.         canvas = self.canv
  54.         canvas.rotate(90)
  55.         fs = canvas._fontsize
  56.         # Измества канваса малко надясно
  57.         canvas.translate(1, -fs/1.2)  # canvas._leading?
  58.         #canvas.drawString(0, 0, self.text)
  59.         draw_wrapped_line(canvas, self.text, 16, 26, 3, 7)
  60.  
  61.     def wrap(self, aW, aH):
  62.         canv = self.canv
  63.         fn, fs = canv._fontname, canv._fontsize
  64.         return canv._leading, 1 + canv.stringWidth(self.text, fn, fs)
  65.  
  66. def draw_wrapped_line(canvas, text, length, x_pos, y_pos, y_offset):
  67.     """
  68.    :param canvas: reportlab canvas
  69.    :param text: the raw text to wrap
  70.    :param length: the max number of characters per line
  71.    :param x_pos: starting x position
  72.    :param y_pos: starting y position
  73.    :param y_offset: the amount of space to leave between wrapped lines
  74.    """
  75.     if len(text) > length:
  76.         wraps = textwrap.wrap(text, length)
  77.         for x in range(len(wraps)):
  78.             canvas.drawCentredString(x_pos, y_pos, wraps[x])
  79.             y_pos -= y_offset
  80.         y_pos += y_offset  # add back offset after last wrapped line
  81.     else:
  82.         canvas.drawCentredString(x_pos, y_pos, text)
  83.     return y_pos
  84.  
  85.  
  86. document = BaseDocTemplate('Vertical_1.pdf')
  87.  
  88. Elements = []
  89.  
  90. titleFrame_1 = Frame(
  91.     0.5*inch, 0.75*inch, 7*inch, 9*inch, id='col1', showBoundary=0)
  92. titleTemplate_1 = PageTemplate(
  93.     id='OneCol', frames=titleFrame_1)
  94. document.addPageTemplates([titleTemplate_1])
  95.  
  96. cw = [1.2*inch] + [1*inch]*6
  97. rh = [0.25*inch] + [.6*inch] + [0.25*inch]*7
  98.  
  99. data = [
  100.     ['ПОКАЗАТЕЛИ', 'Отчетна стойност на\n нетекущите активи', '', '', '', '', '', ''],
  101.     ['', verticalText1('В началото на периода'), verticalText1('На постъпилите през периода'),
  102.         verticalText1('На излезлите през периода'), verticalText1('В края на периода (1+2-3)'),
  103.         verticalText1('Последваща оцен-ка в увеличение'), verticalText1('Последваща оцен-ка в намаление'),
  104.      verticalText1('Преоценена стой-ност (4+5-6)')],
  105.     ['a', '1','2','3','4','5','6','7'],
  106.     ['I. Нематериални активи', '', '','', '', '', '', ''],
  107.     ['    1. Продукти от развойна дейност', '0', '0', '', '233', '1', '3', '555'],
  108.     ['    2. Концесии, патенти, търговски\nмарки, програмни продукти и\nдруги подобни права и активи', '0', '0', '0', '0', '694', '38', '4'],
  109.     ['    3. Търговска репутация', '0', '0', '23', '2', '1319', '2', '5'],
  110.     ['    4. Предоставени аванси и немате-\nриални активи в процес\nна изграждане', '0', '0','0', '0', '0', '0', '6'],
  111.     ['        в т.ч.: предоставени аванси', '0', '0', '93', '805', '24093', '11771', '7'],
  112.     ['Общо за група I.', '0', '0', '93', '805', '24093', '11771', '7'],
  113.     ]
  114.  
  115. ts = [
  116.     ('GRID', (0, 0), (-1, -1), 0.5, colors.black),
  117.     #('GRID', (0, 1), (0, 1), 0.5, colors.black),
  118.     #('GRID', (3, 1), (3, 1), 0.5, colors.black),
  119.     ('SPAN', (1, 0), (-1, 0)),  # Отчтна стойност на нетекущите активи
  120.     ('SPAN', (0, 0), (0, 1)),   # Показатели
  121.  
  122.  
  123.     #('GRID', (0, 2), (-1, -1), 0.5, colors.black),
  124.     ('ALIGN', (0, 0), (-1, 0), 'CENTER'),  # Център по Х -> ПОКАЗАТЕЛИ
  125.     ('VALIGN', (0, 0), (0, 0), 'MIDDLE'),  # Цебтър по Y -> ПОКАЗАТЕЛИ
  126.     ('FONT', (0, 0), (0, 0), 'Timesbd', 7), # -> ПОКАЗАТЕЛИ
  127.     ('FONT', (1, 0), (-1, 0), 'Timesbd', 6), # -> ПОКАЗАТЕЛИ
  128.     ('FONT', (1, 1), (-1, 1), 'Times', 7, 6),   # -> Vertical text
  129.     ('BACKGROUND', (0, 0), (-1, 2), colors.lightgreen),     # lightcoral, lightgreen, lightsalmon, lightseagreen, lightsteelblue
  130.     ('ALIGN', (1, 1), (-1, 1), 'LEFT'), # Център по Х -> Vertical text
  131.     ('VALIGN', (1, 1), (-1, 1), 'BOTTOM'),  # Цебтър по Y -> Vertical text
  132.     ('ALIGN', (0, 2), (-1, 2), 'CENTER'),   # a, 1, 2, 3 ....7
  133.     ('VALIGN', (0, 2), (-1, 2), 'MIDDLE'),  # a, 1, 2, 3 ....7
  134.     ('FONT', (0, 2), (-1, 3), 'Timesbd', 6),  # a, 1, 2, 3 ....7
  135.     ('SPAN', (0, 3), (-1, 3)),  # I. Нематериални активи
  136.     ('BACKGROUND', (0, 3), (-1, 3), colors.lightgoldenrodyellow),  # I. Нематериални активи
  137.     ('ALIGN', (0, 3), (0, -1), 'LEFT'),
  138.     ('VALIGN', (0, 3), (0, -1), 'MIDDLE'),
  139.     # DATA NUMBERS
  140.     ('ALIGN', (1, 3), (-1, -1), 'RIGHT'),
  141.     ('VALIGN', (1, 3), (-1, -1), 'MIDDLE'),
  142.     ('FONT', (1, 4), (-1, -1), 'Times', 7),
  143.     #('LEADING', (1, 3), (-1, -1), 1),
  144.     #('HALIGN', (1, 3), (-1, -1), 0),
  145.     #('LEFTPADDING', (1, 3), (-1, -1), 0),
  146.     ('RIGHTPADDING', (1, 3), (-1, -1), 2),
  147.     ('BOTTOMPADDING', (0, 9), (-1, 9), 6),
  148.     ('TOPPADDING', (0, 3), (0, -1), 0),
  149.  
  150.     ('FONT', (0, 4), (0, 8), 'Times', 7, 5),
  151.     ('FONT', (0, 9), (-1, 9), 'Timesbd', 7, 5),
  152.     ('BACKGROUND', (0, 9), (-1, 9), colors.lightgoldenrodyellow),
  153. ]
  154.  
  155. t = Table(
  156.     data, style=ts,
  157.     colWidths=[120, 20, 20, 20, 20, 20, 20, 20], rowHeights=[20, 70, 10, 10, 10, 20, 10, 20, 10, 10])
  158.     #colWidths=cw, rowHeights=rh)
  159.  
  160.  
  161.  
  162. Elements.append(t)
  163. document.build(Elements)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement