Exemplo n.º 1
0
    def document_to_pdf(self, file_path, temp_dir):
        """Converts an office document to PDF."""
        if self.is_unoconv_available():
            return self.unoconv_to_pdf(file_path, temp_dir)

        instance_dir = join_path(temp_dir, 'soffice_instance')
        out_dir = join_path(temp_dir, 'soffice_output')
        make_directory(out_dir)
        log.info('Converting [%s] to PDF...', self.result)
        instance_dir = '-env:UserInstallation=file://{}'.format(instance_dir)
        self.exec_command('soffice',
                          instance_dir,
                          '--nofirststartwizard',
                          '--norestore',
                          '--nologo',
                          '--nodefault',
                          '--nolockcheck',
                          '--invisible',
                          '--headless',
                          '--convert-to', 'pdf',
                          '--outdir', out_dir,
                          file_path)

        for out_file in os.listdir(out_dir):
            return join_path(out_dir, out_file)

        msg = "Failed to convert to PDF: {}".format(file_path)
        raise ProcessingException(msg)
Exemplo n.º 2
0
    def ensure_path(self, base_dir, name, encoding='utf-8'):
        if isinstance(name, bytes):
            name = name.decode(encoding, 'ignore')

        out_path = join_path(base_dir, name)
        # out_path = os.path.normpath(out_path)
        if not out_path.startswith(base_dir):
            return
        if os.path.exists(out_path):
            return

        out_dir = os.path.dirname(out_path)
        make_directory(out_dir)
        if os.path.isdir(out_path):
            return

        return out_path
Exemplo n.º 3
0
    def unpack_members(self, pack, temp_dir):
        # Some archives come with non-Unicode file names, this
        # attempts to avoid that issue by naming the destination
        # explicitly.
        names = pack.namelist()
        names = [n for n in names if isinstance(n, six.binary_type)]
        encoding = guess_encoding('\n'.join(names))
        log.debug('Detected filename encoding: %s', encoding)

        for name in pack.namelist():
            file_name = name
            if isinstance(name, six.binary_type):
                file_name = name.decode(encoding, 'ignore')

            out_path = join_path(temp_dir, file_name)
            if os.path.exists(out_path):
                continue
            if not out_path.startswith(temp_dir):
                continue

            out_dir = os.path.dirname(out_path)
            make_directory(out_dir)
            if os.path.isdir(out_path):
                continue

            try:
                in_fh = pack.open(name)
                try:
                    log.debug("Unpack: %s -> %s", self.result, file_name)
                    with open(out_path, 'w') as out_fh:
                        shutil.copyfileobj(in_fh, out_fh)
                finally:
                    in_fh.close()
            except Exception as ex:
                # TODO: should this be a fatal error?
                log.debug("Failed to unpack [%s]: %s", file_name, ex)
Exemplo n.º 4
0
 def make_empty_directory(self):
     return make_directory(self.work_path, uuid4().hex)