这将是我在 Stackoverflow 上的第一篇文章。我有一个关于 python 的 FPDF 包的问题。创建一个定义了页眉和页脚的类,效果很好。但是,当我尝试通过init初始化一些变量时,页眉和页脚将停止显示。我用谷歌搜索了所有内容,但无法弄清楚。有谁知道如何解决这个问题?
包含初始化的代码
import fpdf as FPDF
import pandas as pd
import lipsum
from datetime import date
import locale
db=pd.DataFrame({'Fontsize':[5,6,7,8,9,10,11,12,14,17,20,25],'Baselineskip':[6,7,8,9.5,11,12,13.6,14.5,18,22,25,30]})
class PDF(FPDF.FPDF):
def __init__(self,fontsize):
FPDF.FPDF.__init__(self,orientation='P',unit='mm',format='A4')
self.set_font('Arial','',fontsize)
self.fontsize=fontsize
self.baseskip=db[db['Fontsize']==self.fontsize].iloc[0,1]
def header(self):
# Logo
# x=110.9, y=16.1
self.image(name='logo.png', x=110.6, y=15.9, w=72.9)
# Arial bold 15
self.set_font('Arial', '', 15)
#textcolor Arcadis orange
self.set_text_color(r=228,g=97,b=15)
# Title
self.text(self.l_margin, 16.1+7.7, txt='bla')
# Page footer
def footer(self):
# Position at 1.5 cm from bottom
self.set_y(-15)
# Arial italic 8
self.set_font('Arial', 'I', 8)
# Page number
self.cell(0, 10, 'Page ' + str(self.page_no()) + '/{nb}', 0, 0, 'C')
# Instantiation of inherited class
pdf = PDF(fontsize=9)
pdf.alias_nb_pages()
pdf.add_page()
pdf.c_margin=0
pdf.set_top_margin(37)
pdf.set_left_margin(25)
pdf.set_right_margin(25)
pdf.set_line_width(2/pdf.k)
pdf.set_draw_color(228,97,15)
pdf.line(pdf.l_margin,pdf.t_margin,pdf.fw-pdf.r_margin,pdf.t_margin)
pdf.set_font('Arial')
pdf.set_font_size(9)
pdf.ln(11/pdf.k)
for i in range(1, 20):
pdf.cell(0,11/pdf.k, 'Printing line number ' + str(i), 0, 1)
pdf.multi_cell(0,11/pdf.k,lipsum.generate_words(100),align='J')
pdf.output('bla.pdf', 'F')
无代码
class PDF(FPDF.FPDF):
def header(self):
# Logo
# x=110.9, y=16.1
self.image(name='logo.png', x=110.6, y=15.9, w=72.9)
# Arial bold 15
self.set_font('Arial', '', 15)
#textcolor Arcadis orange
self.set_text_color(r=228,g=97,b=15)
# Title
self.text(self.l_margin, 16.1+7.7, txt='BemalingsAdvies')
# Page footer
def footer(self):
# Position at 1.5 cm from bottom
self.set_y(-15)
# Arial italic 8
self.set_font('Arial', 'I', 8)
# Page number
self.cell(0, 10, 'Page ' + str(self.page_no()) + '/{nb}', 0, 0, 'C')
def set_fontskip(self,fontsize):
self.set_font_size(fontsize)
self.fontsize=fontsize
self.baseskip=db[db['Fontsize']==self.fontsize].iloc[0,1]
我提前感谢任何人。