~tomasgroth/openlp/mupdf

« back to all changes in this revision

Viewing changes to openlp/plugins/presentations/lib/pdfcontroller.py

  • Committer: Tomas Groth
  • Date: 2013-12-31 12:15:18 UTC
  • Revision ID: tomasgroth@yahoo.dk-20131231121518-fwqf5lzui6f60oyf
Changed the way a serviceitem from the servicemanager is copied and converted. Also fixed for PEP8.

Show diffs side-by-side

added added

removed removed

Lines of Context:
38
38
from openlp.core.lib import ScreenList
39
39
from .presentationcontroller import PresentationController, PresentationDocument
40
40
 
41
 
 
42
41
log = logging.getLogger(__name__)
43
42
 
 
43
 
44
44
class PdfController(PresentationController):
45
45
    """
46
46
    Class to control PDF presentations
139
139
            return False
140
140
        else:
141
141
            return True
142
 
        
 
142
 
143
143
    def kill(self):
144
144
        """
145
145
        Called at system exit to clean up any running presentations
153
153
    """
154
154
    Class which holds information of a single presentation.
155
155
    This class is not actually used to present the PDF, instead we convert to
156
 
    image-serviceitem on the fly and present as such. Therefore some of the 'playback' 
 
156
    image-serviceitem on the fly and present as such. Therefore some of the 'playback'
157
157
    functions is not implemented.
158
158
    """
159
159
    def __init__(self, controller, presentation):
169
169
        self.num_pages = -1
170
170
 
171
171
    def gs_get_resolution(self,  size):
172
 
        """ 
 
172
        """
173
173
        Only used when using ghostscript
174
174
        Ghostscript can't scale automaticly while keeping aspect like mupdf, so we need
175
175
        to get the ratio bewteen the screen size and the PDF to scale
187
187
quit \n\
188
188
'
189
189
        # Put postscript into tempfile
190
 
        tmpfile = NamedTemporaryFile(delete=False)
191
 
        tmpfile.write(postscript)
192
 
        tmpfile.close()
 
190
        tmp_file = NamedTemporaryFile(delete=False)
 
191
        tmp_file.write(postscript)
 
192
        tmp_file.close()
193
193
        # Run the script on the pdf to get the size
194
194
        runlog = []
195
195
        try:
196
 
            runlog = check_output([self.controller.gsbin, '-dNOPAUSE', '-dNODISPLAY', '-dBATCH', '-sFile=' + self.filepath, tmpfile.name])
 
196
            runlog = check_output([self.controller.gsbin, '-dNOPAUSE', '-dNODISPLAY', '-dBATCH',
 
197
                                   '-sFile=' + self.filepath, tmp_file.name])
197
198
        except CalledProcessError as e:
198
199
            log.debug(' '.join(e.cmd))
199
200
            log.debug(e.output)
200
 
        os.unlink(tmpfile.name)
 
201
        os.unlink(tmp_file.name)
201
202
        # Extract the pdf resolution from output, the format is " Size: x: <width>, y: <height>"
202
203
        width = 0
203
204
        height = 0
205
206
            try:
206
207
                width = re.search('.*Size: x: (\d+\.?\d*), y: \d+.*', line).group(1)
207
208
                height = re.search('.*Size: x: \d+\.?\d*, y: (\d+\.?\d*).*', line).group(1)
208
 
                break;
 
209
                break
209
210
            except AttributeError:
210
211
                pass
211
212
        # Calculate the ratio from pdf to screen
240
241
            if not os.path.isdir(self.get_temp_folder()):
241
242
                os.makedirs(self.get_temp_folder())
242
243
            if self.controller.mudrawbin:
243
 
                runlog = check_output([self.controller.mudrawbin, '-w', str(size.right()), '-h', str(size.bottom()), '-o', os.path.join(self.get_temp_folder(), 'mainslide%03d.png'), self.filepath])
 
244
                runlog = check_output([self.controller.mudrawbin, '-w', str(size.right()), '-h', str(size.bottom()),
 
245
                                       '-o', os.path.join(self.get_temp_folder(), 'mainslide%03d.png'), self.filepath])
244
246
            elif self.controller.gsbin:
245
247
                resolution = self.gs_get_resolution(size)
246
 
                runlog = check_output([self.controller.gsbin, '-dSAFER', '-dNOPAUSE', '-dBATCH', '-sDEVICE=png16m', '-r' + str(resolution), '-dTextAlphaBits=4', '-dGraphicsAlphaBits=4', '-sOutputFile=' + os.path.join(self.get_temp_folder(), 'mainslide%03d.png'), self.filepath])
 
248
                runlog = check_output([self.controller.gsbin, '-dSAFER', '-dNOPAUSE', '-dBATCH', '-sDEVICE=png16m',
 
249
                                       '-r' + str(resolution), '-dTextAlphaBits=4', '-dGraphicsAlphaBits=4',
 
250
                                       '-sOutputFile=' + os.path.join(self.get_temp_folder(), 'mainslide%03d.png'),
 
251
                                       self.filepath])
247
252
            created_files = sorted(os.listdir(self.get_temp_folder()))
248
253
            for fn in created_files:
249
254
                if os.path.isfile(os.path.join(self.get_temp_folder(), fn)):
250
255
                    self.image_files.append(os.path.join(self.get_temp_folder(), fn))
251
 
        except Exception as e: 
 
256
        except Exception as e:
252
257
            log.debug(e)
253
258
            log.debug(runlog)
254
 
            return False 
 
259
            return False
255
260
        self.num_pages = len(self.image_files)
256
261
        # Create thumbnails
257
262
        self.create_thumbnails()
277
282
        """
278
283
        log.debug('close_presentation pdf')
279
284
        self.controller.remove_doc(self)
280
 
        
 
285
 
281
286
    def is_loaded(self):
282
287
        """
283
288
        Returns true if a presentation is loaded.
298
303
        """
299
304
        Returns total number of slides
300
305
        """
301
 
        return self.num_pages
 
 
b'\\ No newline at end of file'
 
306
        return self.num_pages