Example #1
0
def pdf_changed(content, event):
    """
    This event handler is fired when ATFile objects are initialized or edited
    and calls the appropriate functions to convert the pdf to png thumbnails
    and store the list of thumbnails annotated on the file object.
    """
    portal = getSite()
    if 'collective.pdfpeek' in portal.portal_quickinstaller.objectIds():
        registry = getUtility(IRegistry)
        config = registry.forInterface(IPDFPeekConfiguration)
        if config.eventhandler_toggle == True:
            if content.getContentType() == 'application/pdf':
                """Mark the object with the IPDF marker interface."""
                alsoProvides(content, IPDF)
                pdf_file_data_string = content.getFile().data
                image_converter = convertPDFToImage()
                images = image_converter.generate_thumbnails(pdf_file_data_string)
                alsoProvides(content, IAttributeAnnotatable)
                annotations = IAnnotations(content)
                annotations['pdfpeek'] = {}
                annotations['pdfpeek']['image_thumbnails'] = images
                content.reindexObject()
            else:
                noLongerProvides(content, IPDF)
                IAnnotations(content)
                annotations = IAnnotations(content)
                if 'pdfpeek' in annotations:
                    del annotations['pdfpeek']
                content.reindexObject()
        return None
def pdf_changed(content, event):
    """
    This event handler is fired when ATFile objects are initialized or edited
    and calls the appropriate functions to convert the pdf to png thumbnails
    and store the list of thumbnails annotated on the file object.
    """
    portal = getSite()
    if "collective.pdfpeek" in portal.portal_quickinstaller.objectIds():
        registry = getUtility(IRegistry)
        config = registry.forInterface(IPDFPeekConfiguration)
        if config.eventhandler_toggle == True:
            if content.getContentType() == "application/pdf":
                """Mark the object with the IPDF marker interface."""
                alsoProvides(content, IPDF)
                pdf_file_data_string = content.getFile().data
                image_converter = convertPDFToImage()
                images = image_converter.generate_thumbnails(pdf_file_data_string)
                alsoProvides(content, IAttributeAnnotatable)
                annotations = IAnnotations(content)
                annotations["pdfpeek"] = {}
                annotations["pdfpeek"]["image_thumbnails"] = images
                content.reindexObject()
            else:
                noLongerProvides(content, IPDF)
                IAnnotations(content)
                annotations = IAnnotations(content)
                if "pdfpeek" in annotations:
                    del annotations["pdfpeek"]
                content.reindexObject()
        return None
 def test_convert_pdf(self):
     """
     """
     converter = convertPDFToImage()
     for pdf in self.pdf_files:
         image = converter.ghostscript_transform(open(pdf, mode="rb").read(), 1)
         self.assertTrue(image != "")
         self.assertTrue(image != None)
         self.assertTrue(type(image) == type(""))
    def convert(self, orig, data, **kwargs):
        converter = convertPDFToImage()
        options = self.get_options()

        if kwargs.get("pdf_resolution", None) is not None:
            options["resolution"] = kwargs["pdf_resolution"]

        img = converter.ghostscript_transform(orig, 1, options)
        data.setData(img)
        return data
 def test_convert_pdf(self):
     """
     """
     converter = convertPDFToImage()
     for pdf in self.pdf_files:
         image = converter.ghostscript_transform(
             open(pdf, mode='rb').read(), 1)
         self.assertTrue(image != '')
         self.assertTrue(image != None)
         self.assertTrue(type(image) == type(''))
def run_pdfpeek(content, pdf_file_data_string):
    image_converter = convertPDFToImage()
    images = None
    errmsg = "Failed to convert PDF to images with PDFPeek on %s." % content.id
    successmsg = "Converted PDF to images with PDFPeek on %s." % content.id

    try:
        images = image_converter.generate_thumbnails(pdf_file_data_string)
    except:
        logger.error(errmsg)

    if images:
        alsoProvides(content, IPDF)
        alsoProvides(content, IAttributeAnnotatable)
        annotations = IAnnotations(content)
        annotations['pdfpeek'] = {}
        annotations['pdfpeek']['image_thumbnails'] = images
        content.reindexObject()
        logger.info(successmsg)
        return successmsg
    else:
        return errmsg