将标题添加到fpdf

时间:2014-01-10 20:23:59

标签: python fpdf

我尝试在每个页面添加一个标题,我添加了一个我从fpdf教程网站获得的代码,但我没有工作

这是完整的代码:

from PyQt4 import QtCore, QtGui
from ui_condition import Ui_condition
import fpdf
# create the dialog for zoom to point


class conditionDialog(QtGui.QDialog):

    def __init__(self):
        QtGui.QDialog.__init__(self)
        # Set up the user interface from Designer.
        self.ui = Ui_condition()
        self.ui.setupUi(self)

        Recomend = self.ui.btnProc
        QtCore.QObject.connect(Recomend, QtCore.SIGNAL('clicked()'),self.Compare1)
        RecSec = self.ui.btnProc2
        QtCore.QObject.connect(RecSec, QtCore.SIGNAL('clicked()'),self.Compare2)
        Smpan = self.ui.btnSave
        Smpan.clicked.connect(self.saveAs)

    def Compare1(self):
        input1 = self.ui.lineInput1.text()
        input2 = self.ui.lineInput2.text()
        compare = ''
        if input1 == input2:
            compare = 'Data dapat digunakam'
        else:
            compare = 'Data tidak cocok'
        self.ui.textRec1.setPlainText(compare)

    def Compare2(self):
        dat1 = self.ui.lineCond1.text()
        dat2 = self.ui.lineCond2.text()
        hitung = ''
        if dat1 == dat2:
            hitung = 'Skala Cocok'
        elif dat1 >= dat2:
            hitung = 'Masih dapat dipakai'
        else:
            hitung = 'Data Tidak dapat dipakai'
        self.ui.textRec2.setPlainText(hitung)

    def header(self):
        #Header on each page
        # set the font for the header, B=Bold
        self.set_font("Times", style="B", size=16)
        # page title
        self.cell(0,25, "INI UJI COBA!!!", border=0, ln=1, align="C")

    def _save(self, simpan):
        # Portrait, millimeter units, A4 page size     
        pdf=fpdf.FPDF("P", "mm", "A4")
        #Mencoba Header
        pdf.alias_nb_pages()
        # Add our one report page
        pdf.add_page()
        # Set font: Times, normal, size 10
        pdf.set_font('Times','', 10)
        # Layout cell: 0 x 5 mm, text, no border, Left
        pdf.cell(0,20,'== LAPORAN ASSESSMENT ==', border=0, ln=1, align="C" )
        pdf.cell(50,10,'==='*31, border=0, ln=1, align="L" )
        pdf.cell(30,7,'Hasil Perbandingan Skala', border=0, ln=1, align="L" )
        pdf.cell(50,3,'---'*21, border=0, ln=1, align="L" )
        pdf.cell(50,5,'Input 1 : ' + self.ui.lineInput1.text(), border=0, ln=1, align="L" ) 
        pdf.cell(50,5,'Input 2 : ' + self.ui.lineInput2.text(), border=0, ln=1, align="L" )
        pdf.cell(50,5,'Recomendation : ' + self.ui.textRec1.toPlainText(), border=0, ln=1, align="L" )
        pdf.cell(50,12,'---'*21, border=0, ln=1, align="L" )
        pdf.cell(30,7,'Hasil Perbandingan Data', border=0, ln=1, align="L" )
        pdf.cell(50,3,'---'*21, border=0, ln=1, align="L" )
        pdf.cell(50,5,'Data 1 :' +  self.ui.lineCond1.text(), border=0, ln=1, align="L" )
        pdf.cell(50,5,'Data 2 :' + self.ui.lineCond2.text(), border=0, ln=1, align="L" )
        pdf.cell(50,5,'Recomendation 2 : ' + self.ui.textRec2.toPlainText(), border=0, ln=1, align="L" )
        pdf.output( simpan+'.pdf','F')

    def saveAs(self):
            tulis = QtGui.QFileDialog(self).getSaveFileName()
            if tulis !="":
                self._save( tulis )

当我尝试保存文件时,文件可以保存并且信息可以显示(_save输入)但标题没有显示

我必须添加到此代码

谢谢

1 个答案:

答案 0 :(得分:0)

尝试让您的课程继承自此http://code.google.com/p/pyfpdf/wiki/Tutorial

所述的fpdf.FPDF

class conditionDialog(QtGui.QDialog,fpdf.FPDF)

class conditionDialog(QtGui.QDialog,FPDF)

再见,

弗朗西斯