def initialize(self): site = getSite() self.settings = Settings(self.context) self.global_settings = GlobalSettings(site) utils = getToolByName(self.context, 'plone_utils') msg = None if self.context.getContentType() in ('application/pdf', 'application/x-pdf', 'image/pdf'): if not self.installed: msg = "Since you do not have swftools installed on this " + \ "system, we can not render the pages of this PDF." elif self.settings.converting is not None and \ self.settings.converting: msg = "The PDF is currently being converted to the " + \ "FlexPaper view..." self.enabled = False elif not self.settings.successfully_converted: msg = "There was an error trying to convert the PDF. Maybe " +\ "the PDF is encrypted, corrupt or malformed?" self.enabled = False else: self.enabled = False msg = "The file is not a PDF. No need for this view." if msg: mtool = getToolByName(self.context, 'portal_membership') if mtool.checkPermission('cmf.ModifyPortalContent', self.context): utils.addPortalMessage(msg)
def convert(context): """ Convert PDF to Flex Paper """ settings = Settings(context) site = getSite() global_settings = GlobalSettings(site) if DateTime(settings.last_updated) < DateTime(context.ModificationDate()): context = aq_inner(context) field = context.getField('file') or context.getPrimaryField() import transaction # commit anything done before the expensive operation transaction.commit() try: s_options = settings.command_line_options and \ settings.command_line_options or \ global_settings.command_line_options if s_options: opts = [ o.strip().replace(' ', '').replace('\t', '') for o in s_options.split(',') ] else: opts = [] result = pdf2swf.convert(str(field.get(context).data), opts, password=settings.encryption_password) transaction.begin() if has_pab: blob = Blob() bfile = blob.open('w') bfile.write(result) bfile.close() settings.data = blob else: file = BaseUnit('_converted_file', result, mimetype='application/x-shockwave-flash', filename=context.getFilename().replace( '.pdf', '.swf'), context=context) converted_field.set(context, file, _initializing_=True) settings.successfully_converted = True except: logger.exception('Error converting PDF') settings.successfully_converted = False settings.last_updated = DateTime().ISO8601() settings.converting = False transaction.commit()