def convert(self, blob, **kw):
        """Convert using soffice converter."""
        timeout = self.run_timeout
        with make_temp_file(blob) as in_fn:

            cmd = [self.soffice, "--headless", "--convert-to", "pdf", in_fn]

            # # TODO: fix this if needed, or remove if not needed
            # if os.path.exists(
            #         "/Applications/LibreOffice.app/Contents/program/python"):
            #     cmd = [
            #         '/Applications/LibreOffice.app/Contents/program/python',
            #         '/usr/local/bin/unoconv', '-f', 'pdf', '-o', out_fn, in_fn
            #     ]

            def run_soffice():
                try:
                    self._process = subprocess.Popen(
                        cmd, close_fds=True, cwd=bytes(self.tmp_dir)
                    )
                    self._process.communicate()
                except Exception as e:
                    logger.error("soffice error: %s", bytes(e), exc_info=True)
                    raise ConversionError("soffice conversion failed") from e

            run_thread = threading.Thread(target=run_soffice)
            run_thread.start()
            run_thread.join(timeout)

            try:
                if run_thread.is_alive():
                    # timeout reached
                    self._process.terminate()
                    if self._process.poll() is not None:
                        try:
                            self._process.kill()
                        except OSError:
                            logger.warning("Failed to kill process %s", self._process)

                    self._process = None
                    raise ConversionError(f"Conversion timeout ({timeout})")

                out_fn = os.path.splitext(in_fn)[0] + ".pdf"
                converted = open(out_fn, "rb").read()
                return converted
            finally:
                self._process = None
def convert ( self , blob , ** kw ) : timeout = self . run_timeout with make_temp_file ( blob ) as in_fn : cmd = [ self . soffice , "--headless" , "--convert-to" , "pdf" , in_fn ] def run_soffice ( ) : try : self . _process = subprocess . Popen ( cmd , close_fds = True , cwd = bytes ( self . tmp_dir ) ) self . _process . communicate ( ) except Exception as e : logger . error ( "soffice error: %s" , bytes ( e ) , exc_info = True ) raise ConversionError ( "soffice conversion failed" ) from e run_thread = threading . Thread ( target = run_soffice ) run_thread . start ( ) run_thread . join ( timeout ) try : if run_thread . is_alive ( ) : self . _process . terminate ( ) if self . _process . poll ( ) is not None : try : self . _process . kill ( ) except OSError : logger . warning ( "Failed to kill process %s" , self . _process ) self . _process = None raise ConversionError ( f"Conversion timeout ({timeout})" ) out_fn = os . path . splitext ( in_fn ) [ 0 ] + ".pdf" converted = open ( out_fn , "rb" ) . read ( ) return converted finally : self . _process = None
Convert using soffice converter.