38
38
from openlp.core.lib import ScreenList
39
39
from .presentationcontroller import PresentationController, PresentationDocument
42
41
log = logging.getLogger(__name__)
44
44
class PdfController(PresentationController):
46
46
Class to control PDF presentations
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.
159
159
def __init__(self, controller, presentation):
169
169
self.num_pages = -1
171
171
def gs_get_resolution(self, size):
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
189
189
# Put postscript into tempfile
190
tmpfile = NamedTemporaryFile(delete=False)
191
tmpfile.write(postscript)
190
tmp_file = NamedTemporaryFile(delete=False)
191
tmp_file.write(postscript)
193
193
# Run the script on the pdf to get the size
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>"
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)
209
210
except AttributeError:
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'),
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:
253
258
log.debug(runlog)
255
260
self.num_pages = len(self.image_files)
256
261
# Create thumbnails
257
262
self.create_thumbnails()