1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229 | ######################################################################
# PDF Library
######################################################################
#https://pyfpdf.readthedocs.io/en/latest/ReferenceManual/index.html
from fpdf import FPDF
class PDF(FPDF): #override header and footer
def __init__(self,orientation='P', unit='pt', pagesize='A4'):
super(PDF,self).__init__(orientation=orientation, unit=unit, format=pagesize)
self.set_margins(self.l_margin*2, self.t_margin*2, self.r_margin*2)
#default margin: 28.35, 28.35, 28.35, 28.35*2
def header(self,logo=None, title=None):
if logo is not None:
self.image(logo, 10, 8, 33)
self.set_font('Arial', 'B', 15)
self.cell(80) # Move to the right
if title is not None:
self.cell(30, 10, 'Title', align='R')
self.ln(20) # Line break
self.line( self.l_margin, self.t_margin, self.w - self.r_margin, self.t_margin )
def footer(self):
self.set_y(-56)
self.set_font('Arial', 'I', 8)
self.cell(0, 28, '- ' + str(self.page_no()) + ' -', align='C')
self.line( self.l_margin, self.h - self.b_margin, self.w - self.r_margin, self.h - self.b_margin )
class pypdf():
def __init__(self, orientation='P', unit='pt', pagesize='A4'):
'''
pypdf(orientation='P', unit='pt', format='A4'
- orientation: 'P', 'L'
- unit: 'pt', 'mm', 'cm', 'in',
- format:
- A3 (841.89,1190.55)
- A4 (595.28,841.89)
- A5 (420.94,595.28)
- Letter (612,792)
- Legal (612,1008)
'''
self.pdf = PDF(orientation=orientation,unit=unit,pagesize=pagesize)
def save(self,filename):
self.pdf.output(name=filename,dest='F')
def saveToString(self):
return self.pdf.output(dest='S')
def addPage(self,orientation=''):
self.pdf.add_page(orientation=orientation)
def getPageWidth(self):
return self.pdf.w
def getPageHeight(self):
return self.pdf.h
def setXY(self,x,y):
self.pdf.set_xy(x,y)
def setX(self,x):
self.pdf.set_x(x)
def setY(self,y):
self.pdf.set_y(y)
def dumpPageInfo(self):
print( "w: ", self.pdf.w, self.pdf.w_pt)
print( "h: ", self.pdf.h, self.pdf.h_pt)
print( "ltrb: ", self.pdf.l_margin, self.pdf.t_margin, self.pdf.r_margin, self.pdf.b_margin)
def setFont(self,font="Arial",size=12):
#set_font(self, family,style='',size=0):
''' family
self.core_fonts={'courier': 'Courier',
'courierB': 'Courier-Bold',
'courierBI': 'Courier-BoldOblique',
'courierI': 'Courier-Oblique',
'helvetica': 'Helvetica',
'helveticaB': 'Helvetica-Bold',
'helveticaBI': 'Helvetica-BoldOblique',
'helveticaI': 'Helvetica-Oblique',
'symbol': 'Symbol',
'times': 'Times-Roman',
'timesB': 'Times-Bold',
'timesBI': 'Times-BoldItalic',
'timesI': 'Times-Italic',
'zapfdingbats': 'ZapfDingbats'}
'''
#style: B, I, BI, U
self.pdf.set_font(font,size=size)
def setFontSize(self,size):
return self.pdf.set_font_size(size)
def getFontSize(self):
return self.pdf.font_size
def setTextColor(self,color=[0,0,0]):
self.pdf.set_text_color(color[0], color[1], color[2])
def setFillColor(self,color=[0,0,0]):
self.pdf.set_fill_color(color[0], color[1], color[2])
def getStringWidth(self,text):
return self.pdf.get_string_width(text)
def printText(self,text):
#cell(self, w,h=0,txt='',border=0,ln=0,align='',fill=0,link=''):
#align: 'L','C','R'
text_width = self.pdf.get_string_width(text)
field_width = self.pdf.w - self.pdf.r_margin
if text_width > field_width:
self.pdf.ln()
self.pdf.cell(text_width,
self.pdf.font_size, txt=text, ln=0,
align='L', border=0, fill=False)
def printTextLn(self,text):
self.printText(text)
self.pdf.ln()
def setLineWidth(self,width):
self.pdf.set_line_width(width)
def printLine(self):
#fpdf.line(x1, y1, x2, y2)
#TODO self.pdf.line(self.pdf.l_margin, )
pass
def printImage(self,filename,w_percent=0):
#fpdf.image(name, x = None, y = None, w = 0, h = 0, type = '', link = '')
width = user_width = self.pdf.w - self.pdf.l_margin - self.pdf.r_margin
if w_percent > 0:
width = int(user_width * w_percent / 100)
self.pdf.image(filename,w=width)
def printTable(self,data,spacing=4):
'''
data = [['First Name', 'Last Name', 'email', 'zip'],
['Mike', 'Driscoll', 'mike@somewhere.com', '55555'],
['John', 'Doe', 'jdoe@doe.com', '12345'],
['Nina', 'Ma', 'inane@where.com', '54321']
]
'''
col_width = pdf.w / 4.5
row_height = pdf.font_size
for row in data:
for item in row:
pdf.cell(col_width, row_height*spacing,
txt=item, border=1)
pdf.ln(row_height*spacing)
pdf.output('simple_table.pdf')
######################################################################
# PDF Test Example
######################################################################
def test():
pdf = FPDF()
pdf.add_page()
pdf.set_font("Arial", size=12)
pdf.cell(200, 10, txt="Welcome to Python!", ln=1, align="C")
pdf.output("a.pdf")
def pdfCreationTest():
pdf = pypdf()
pdf.addPage()
pdf.dumpPageInfo()
pdf.setFont()
pdf.setFillColor([255,255,0])
pdf.printText("Welcome1")
pdf.printTextLn("Welcome2")
pdf.printTextLn("Welcome3")
pdf.printTextLn("Welcome Write")
pdf.printTextLn("")
pdf.printImage("D:/line.png", w_percent=80)
pdf.save('a.pdf')
#print(pdf.saveToString())
def create_pdf(pdf_path):
pdf = FPDF()
# Create the special value {nb}
#pdf.alias_nb_pages()
pdf.add_page()
pdf.set_font('Times', '', 12)
line_no = 1
for i in range(50):
pdf.cell(0, 6, txt="Line #{}".format(line_no), border=1, ln=1)
line_no += 1
pdf.output(pdf_path)
def simple_table(spacing=1):
data = [['First Name', 'Last Name', 'email', 'zip'],
['Mike', 'Driscoll', 'mike@somewhere.com', '55555'],
['John', 'Doe', 'jdoe@doe.com', '12345'],
['Nina', 'Ma', 'inane@where.com', '54321']
]
pdf = FPDF()
pdf.set_font("Arial", size=12)
pdf.add_page()
col_width = pdf.w / 4.5
row_height = pdf.font_size
for row in data:
for item in row:
pdf.cell(col_width, row_height*spacing,
txt=item, border=1)
pdf.ln(row_height*spacing)
pdf.output('simple_table.pdf')
######################################################################
# MAIN Example
######################################################################
if __name__ == "__main__":
pdfCreationTest()
#test()
#create_pdf('c.pdf')
#simple_table()
|
댓글 없음:
댓글 쓰기