Example #1
0
    def printToPdf(self, theHtml, theFilename=None):
        """Render an html snippet into the printer, paginating as needed.

        Args:
            * theHtml: str A string containing an html snippet. It will have a
                header and footer appended to it in order to make it a valid
                html document. The header will also apply the bootstrap theme
                to the document.
            * theFilename: str String containing a pdf file path that the
                output will be written to.
        Returns:
            str: The file path of the output pdf (which is the same as the
                theFilename parameter if it was specified.

        Raises:
            None
        """
        LOGGER.info('InaSAFE Map printToPdf called')
        if theFilename is None:
            myHtmlPdfPath = unique_filename(prefix='table',
                                           suffix='.pdf',
                                           dir=temp_dir('work'))
        else:
            # We need to cast to python string in case we receive a QString
            myHtmlPdfPath = str(theFilename)

        self.printer = setupPrinter(myHtmlPdfPath)
        self.loadAndWait(theHtmlSnippet=theHtml)
        self.webView.print_(self.printer)

        return myHtmlPdfPath
Example #2
0
    def printToPdf(self, theHtml, theFilename=None):
        """Render an html snippet into the printer, paginating as needed.

        Args:
            * theHtml: str A string containing an html snippet. It will have a
                header and footer appended to it in order to make it a valid
                html document. The header will also apply the bootstrap theme
                to the document.
            * theFilename: str String containing a pdf file path that the
                output will be written to.
        Returns:
            str: The file path of the output pdf (which is the same as the
                theFilename parameter if it was specified.

        Raises:
            None
        """
        LOGGER.info('InaSAFE Map printToPdf called')
        if theFilename is None:
            myHtmlPdfPath = unique_filename(prefix='table',
                                           suffix='.pdf',
                                           dir=temp_dir('work'))
        else:
            # We need to cast to python string in case we receive a QString
            myHtmlPdfPath = str(theFilename)

        self.printer = setupPrinter(myHtmlPdfPath)
        self.loadAndWait(theHtmlSnippet=theHtml)
        self.webView.print_(self.printer)

        return myHtmlPdfPath
Example #3
0
    def printImpactTable(self, theKeywords, theFilename=None):
        """High level table generator to print layer keywords.

        It gets the summary and impact table from a QgsMapLayer's keywords and
        renders to pdf, returning the resulting PDF file path.

        Args:
            theKeywords: dic containing impact layer keywords (required)

        Returns:
            str: Path to generated pdf file.

        Raises:
            None

        """
        myFilePath = theFilename

        if theFilename is None:
            myFilePath = unique_filename(suffix='.pdf', dir=temp_dir())

        try:
            mySummaryTable = theKeywords['impact_summary']
        except KeyError:
            mySummaryTable = None

        myAttributionTable = impactLayerAttribution(theKeywords)

        try:
            myFullTable = theKeywords['impact_table']
        except KeyError:
            myFullTable = None

        try:
            myAggregationTable = theKeywords['postprocessing_report']
        except KeyError:
            myAggregationTable = None

        myHtml = ''
        if mySummaryTable != myFullTable and mySummaryTable is not None:
            myHtml = '<h2>%s</h2>' % self.tr('Summary Table')
            myHtml += mySummaryTable
            if myAggregationTable is not None:
                myHtml += myAggregationTable
            if myAttributionTable is not None:
                myHtml += myAttributionTable
            myHtml += '<h2>%s</h2>' % self.tr('Detailed Table')
            myHtml += myFullTable
        else:
            if myAggregationTable is not None:
                myHtml = myAggregationTable
            if myFullTable is not None:
                myHtml += myFullTable
            if myAttributionTable is not None:
                myHtml += myAttributionTable

        # myNewFilePath should be the same as myFilePath
        myNewFilePath = self.printToPdf(myHtml, myFilePath)
        return myNewFilePath
Example #4
0
    def printImpactTable(self, theKeywords, theFilename=None):
        """High level table generator to print layer keywords.

        It gets the summary and impact table from a QgsMapLayer's keywords and
        renders to pdf, returning the resulting PDF file path.

        Args:
            theKeywords: dic containing impact layer keywords (required)

        Returns:
            str: Path to generated pdf file.

        Raises:
            None

        """
        myFilePath = theFilename

        if theFilename is None:
            myFilePath = unique_filename(suffix='.pdf', dir=temp_dir())

        try:
            mySummaryTable = theKeywords['impact_summary']
        except KeyError:
            mySummaryTable = None

        myAttributionTable = impactLayerAttribution(theKeywords)

        try:
            myFullTable = theKeywords['impact_table']
        except KeyError:
            myFullTable = None

        try:
            myAggregationTable = theKeywords['postprocessing_report']
        except KeyError:
            myAggregationTable = None

        myHtml = ''
        if mySummaryTable != myFullTable and mySummaryTable is not None:
            myHtml = '<h2>%s</h2>' % self.tr('Summary Table')
            myHtml += mySummaryTable
            if myAggregationTable is not None:
                myHtml += myAggregationTable
            if myAttributionTable is not None:
                myHtml += myAttributionTable
            myHtml += '<h2>%s</h2>' % self.tr('Detailed Table')
            myHtml += myFullTable
        else:
            if myAggregationTable is not None:
                myHtml = myAggregationTable
            if myFullTable is not None:
                myHtml += myFullTable
            if myAttributionTable is not None:
                myHtml += myAttributionTable

        # myNewFilePath should be the same as myFilePath
        myNewFilePath = self.printToPdf(myHtml, myFilePath)
        return myNewFilePath
 def test_Converting(self):
     """Test converting a file
     """
     myDialog = ConverterDialog(PARENT)
     myDialog.test_mode = True
     myDialog.cBDefaultOutputLocation.setEnabled(False)
     my_grid_path = os.path.join(TESTDATA, "grid.xml")
     my_output_raster = unique_filename(prefix="result_grid", suffix=".tif", dir=temp_dir("test"))
     myDialog.cBLoadLayer.setEnabled(True)
     myDialog.leInputPath.setText(my_grid_path)
     myDialog.leOutputPath.setText(my_output_raster)
     myButton = myDialog.buttonBox.button(QDialogButtonBox.Ok)
     QTest.mouseClick(myButton, Qt.LeftButton)
     assert os.path.exists(my_output_raster), "Raster is not created"
Example #6
0
 def Xtest_Converting(self):
     """Test converting a file
     """
     myDialog = ConverterDialog(PARENT)
     myDialog.test_mode = True
     myDialog.cBDefaultOutputLocation.setEnabled(False)
     my_grid_path = os.path.join(TESTDATA, 'grid.xml')
     my_output_raster = unique_filename(prefix='result_grid',
                                        suffix='.tif',
                                        dir=temp_dir('test'))
     myDialog.cBLoadLayer.setEnabled(True)
     myDialog.leInputPath.setText(my_grid_path)
     myDialog.leOutputPath.setText(my_output_raster)
     myButton = myDialog.buttonBox.button(QDialogButtonBox.Ok)
     QTest.mouseClick(myButton, Qt.LeftButton)
     assert os.path.exists(my_output_raster), 'Raster is not created'