Created
August 8, 2017 19:03
-
-
Save search5/ea7d745a4d3f42fbe51b5ed14816238e to your computer and use it in GitHub Desktop.
PyFPDF에서 테이블을 구현하기 위해서 포팅한 라이브러리
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#-*- coding: utf-8 -*- | |
# original URL: http://fpdf.de/downloads/addons/3/ | |
__author__ = 'jiho' | |
from fpdf import FPDF | |
class PDF_MC_Table(FPDF): | |
def SetWidths(self, w): | |
# Set the array of column widths | |
self.widths = w | |
def SetAligns(self, a): | |
# Set the array of column alignments | |
self.aligns = a | |
def Row(self, data): | |
# Calculate the height of the row | |
nb = 0 | |
for i in range(0, len(data)): | |
nb = max(nb, self.NbLines(self.widths[i], data[i])) | |
h= 5 * nb | |
# Issue a page break first if needed | |
#self.CheckPageBreak(h) | |
# Draw the cells of the row | |
self.set_x(13) | |
for i in range(0, len(data)): | |
w = self.widths[i] | |
a = "L" | |
if getattr(self, "aligns", None): | |
try: | |
a = self.aligns[i] | |
except IndexError: | |
pass | |
# Save the current position | |
x = self.get_x() | |
y = self.get_y() | |
# Draw the border | |
self.rect(x, y, w, h) | |
# Print the text | |
self.multi_cell(w, 5, data[i], 0, a) | |
# Put the position to the right of the cell | |
self.set_xy(x + w, y) | |
# Go to the next line | |
self.ln(h) | |
def CheckPageBreak(self, h): | |
# If the height h would cause an overflow, add a new page immediately | |
if self.get_y() + h > self.page_break_trigger: | |
self.add_page(self.cur_orientation) | |
def NbLines(self, w, txt): | |
# Computes the number of lines a MultiCell of width w will take | |
cw = self.current_font['cw'] | |
if w == 0: | |
w = self.w - self.r_margin - self.x | |
wmax = (w-2 * self.c_margin) * 1000 / self.font_size | |
s = txt.replace("\r", '') | |
nb = len(s) | |
if nb > 0 and s[nb - 1] == "\n": | |
nb -= 1 | |
sep = -1 | |
i = 0 | |
j = 0 | |
l = 0 | |
nl = 1 | |
while i < nb: | |
c=s[i] | |
if c == "\n": | |
i += 1 | |
sep = -1 | |
j = i | |
l = 0 | |
nl += 1 | |
continue | |
if c == ' ': | |
sep = i | |
#print ord(c) | |
l += cw[ord(c)] | |
if l > wmax: | |
if sep == -1: | |
if i == j: | |
i += 1 | |
else: | |
i = sep + 1 | |
sep = -1 | |
j = i | |
l = 0 | |
nl += 1 | |
else: | |
i += 1 | |
return nl |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment