1

I m trying to make QGIS plugin with plugin builder that compare 2 map scale with Input1 and Input2 as input scale and show recommendation after clicked recommendation button.

I wrote my code like this:

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
        QtCore.QObject.connect(Smpan, QtCore.SIGNAL('clicked()'),self.saveAs)       

    def Compare1(self):
        input1 = self.ui.lineInput1.text()
        input2 = self.ui.lineInput2.text()
        compare = ''
        if input1 == input2:
            compare = 'Matching'
        else:
            compare = 'Not Matching'
        self.ui.textRec1.setPlainText(compare)

    def Compare2(self):
        dat1 = self.ui.lineCond1.text()
        dat2 = self.ui.lineCond2.text()
        hitung = ''
        if dat1 == dat2:
            hitung = 'Scale is Matching'
        elif dat1 >= dat2:
            hitung = 'Still can be used'
        else:
            hitung = 'Cannot be used'
        self.ui.textRec2.setPlainText(hitung)

    def _save(self, simpan):       
        f = open( simpan, 'w' )
        f.write( 'Input 1 :' + "%s" % self.ui.lineInput1.text() )
        f.write( 'Input 2 :' + "%s" % self.ui.lineInput2.text() )
        f.write( 'Recomendation :' + (str(compare)+'\n' )
        f.write( 'Data 1 :' + "%s" % self.ui.lineCond1.text() )
        f.write( 'Data 2 :' + "%s" % self.ui.lineCond2.text() )
        f.close()
    def savefile(self):
        if self.simpan:
              self._save( "%s" % self.simpan )
        else:
              self.saveAs()

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

What must I add to save it to pdf format?

1

You need a Python script to to convert your resulting text file in PDF or a Python module to write pdf files and there are many... (look at PiPY:pdf).

1) first solution with a Python script:

2) second solution with a Python module

The problem is that you need to install the module in the Python used by QGIS (easy in Mac OS X or Linux, difficult in Windows) or in the folder of your plugin.

def _save(self, simpan):  
    import fpdf
    # Portrait, millimeter units, A4 page size     
    pdf=fpdf.FPDF("P", "mm", "A4")
    # 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,5,'Input 1 : ' + self.ui.lineInput1.text(), border=0, align="L" )
    pdf.cell(0,5,'Input 2 : ' + self.ui.lineInput2.text(), border=0, align="L")
    pdf.cell(0,5,'Recomendation : ' + (str(compare), border=0, align="L")
    pdf.cell(0,5,'Data 1 :' +  self.ui.lineCond1.text(), border=0, align="L" )
    pdf.cell(0,5,'Data 2 :' + self.ui.lineCond2.text(), border=0, align="L" )
    pdf.output( simpan+'.pdf','F')
def _save(self, simpan):  
    from PDFWriter import PDFWriter
    pw = PDFWriter(simpan+'.pdf')
    pw.setFont("Courier", 10)
    pw.writeLine('Input 1 : ' + self.ui.lineInput1.text() )
    pw.writeLine('Input 2 : ' +  self.ui.lineInput2.text() )
    pw.writeLine('Recomendation :' + (str(compare))
    pw.writeLine('Data 1 : ' +  self.ui.lineCond1.text() )
    pw.writeLine('Data 2 : ' + self.ui.lineCond2.text() )
    pw.close
  • I forgot pdf.add_page() corrected above – gene Jan 6 '14 at 16:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.