Example #1
0
    def build_jpeg_preview(
        self,
        file_path: str,
        preview_name: str,
        cache_path: str,
        page_id: int,
        extension: str = ".jpg",
        size: ImgDims = None,
        mimetype: str = "",
    ) -> None:
        if not size:
            size = self.default_size
        # inkscape tesselation-P3.svg  -e
        with tempfile.NamedTemporaryFile("w+b",
                                         prefix="preview-generator-",
                                         suffix=".png") as tmp_png:
            build_png_result_code = self._imagemagick_convert(
                source_path=file_path,
                dest_path=tmp_png.name,
                mimetype=mimetype)

            if build_png_result_code != 0:
                raise IntermediateFileBuildingFailed(
                    "Building PNG intermediate file using convert "
                    "failed with status {}".format(build_png_result_code))

            return ImagePreviewBuilderPillow().build_jpeg_preview(
                tmp_png.name, preview_name, cache_path, page_id, extension,
                size)
Example #2
0
    def build_jpeg_preview(
        self,
        file_path: str,
        preview_name: str,
        cache_path: str,
        page_id: int,
        extension: str = ".jpg",
        size: ImgDims = None,
        mimetype: str = "",
    ) -> None:
        if not size:
            size = self.default_size
        # inkscape tesselation-P3.svg  -e
        with tempfile.NamedTemporaryFile("w+b",
                                         prefix="preview-generator-",
                                         suffix=".png") as tmp_png:
            build_png_result_code = check_call(
                [
                    "inkscape", file_path, "--export-area-drawing", "-e",
                    tmp_png.name
                ],
                stdout=DEVNULL,
                stderr=STDOUT,
            )

            if build_png_result_code != 0:
                raise IntermediateFileBuildingFailed(
                    "Building PNG intermediate file using inkscape "
                    "failed with status {}".format(build_png_result_code))

            return ImagePreviewBuilderPillow().build_jpeg_preview(
                tmp_png.name, preview_name, cache_path, page_id, extension,
                size, mimetype)
Example #3
0
    def build_jpeg_preview(self,
                           file_path: str,
                           preview_name: str,
                           cache_path: str,
                           page_id: int,
                           extension: str = '.jpg',
                           size: ImgDims = None,
                           mimetype: str = '') -> None:
        # inkscape tesselation-P3.svg  -e
        tempfolder = tempfile.tempdir
        tmp_filename = '{}.png'.format(str(uuid.uuid4()))
        tmp_filepath = os.path.join(tempfolder, tmp_filename)
        build_png_result_code = check_call([
            'inkscape', file_path, '--export-area-drawing', '-e', tmp_filepath
        ],
                                           stdout=DEVNULL,
                                           stderr=STDOUT)

        if build_png_result_code != 0:
            raise IntermediateFileBuildingFailed(
                'Building PNG intermediate file using inkscape '
                'failed with status {}'.format(build_png_result_code))

        return ImagePreviewBuilderPillow().build_jpeg_preview(
            tmp_filepath, preview_name, cache_path, page_id, extension, size,
            mimetype)
    def build_jpeg_preview(
        self,
        file_path: str,
        preview_name: str,
        cache_path: str,
        page_id: int,
        extension: str = ".jpg",
        size: ImgDims = None,
        mimetype: str = "",
    ) -> None:
        if not size:
            size = self.default_size
        # inkscape tesselation-P3.svg  -e
        tmp_filename = "{}.png".format(str(uuid.uuid4()))
        if tempfile.tempdir:
            tmp_filepath = os.path.join(tempfile.tempdir, tmp_filename)
        else:
            tmp_filepath = tmp_filename
        build_png_result_code = check_call(
            ["convert", file_path, "-layers", "merge", tmp_filepath], stdout=DEVNULL, stderr=STDOUT
        )

        if build_png_result_code != 0:
            raise IntermediateFileBuildingFailed(
                "Building PNG intermediate file using convert "
                "failed with status {}".format(build_png_result_code)
            )

        return ImagePreviewBuilderPillow().build_jpeg_preview(
            tmp_filepath, preview_name, cache_path, page_id, extension, size
        )
Example #5
0
    def build_jpeg_preview(
        self,
        file_path: str,
        preview_name: str,
        cache_path: str,
        page_id: int,
        extension: str = ".jpg",
        size: utils.ImgDims = None,
        mimetype: str = "",
    ) -> None:
        """
        generate the pdf small preview
        """
        # INFO - G.M - 2021-10-21 - Page id in pdftocairo begins at 1 instead of 0
        page_id = page_id + 1
        if not size:
            size = self.default_size

        with tempfile.NamedTemporaryFile("w+b",
                                         prefix="preview-generator-",
                                         suffix=".png") as tmp_png:
            build_png_result_code = check_call(
                [
                    PDFTOCAIRO_EXECUTABLE,
                    "-png",
                    "-singlefile",
                    "-scale-to",
                    str(size.max_dim()),
                    "-f",
                    str(page_id),
                    "-l",
                    str(page_id),
                    file_path,
                    # HACK - G.M - 2021-10-21 - For unclear reason, pdftocairo add a second .png
                    # extension to the file created.
                    tmp_png.name.rsplit(".png", 1)[0],
                ],
                stdout=DEVNULL,
                stderr=STDOUT,
            )
            if build_png_result_code != 0:
                raise IntermediateFileBuildingFailed(
                    "Building PNG intermediate file using pdftocairo failed with status {}"
                    .format(build_png_result_code))
            return ImagePreviewBuilderPillow().build_jpeg_preview(
                tmp_png.name, preview_name, cache_path, page_id, extension,
                size, mimetype)
    def build_jpeg_preview(
        self,
        file_path: str,
        preview_name: str,
        cache_path: str,
        page_id: int,
        extension: str = ".jpg",
        size: ImgDims = None,
        mimetype: str = "",
    ) -> None:
        if not size:
            size = self.default_size
        with tempfile.NamedTemporaryFile("w+b",
                                         prefix="preview-generator-",
                                         suffix=".jpg") as tmp_jpg:
            with Xvfb():
                build_jpg_result_code = check_call(
                    [
                        "/usr/bin/drawio",
                        "--no-sandbox",
                        "-x",
                        "-f",
                        "jpg",
                        "-o",
                        tmp_jpg.name,
                        file_path,
                    ],
                    stdout=DEVNULL,
                    stderr=STDOUT,
                    timeout=30,
                )

            if build_jpg_result_code != 0:
                raise IntermediateFileBuildingFailed(
                    "Building JPG intermediate file using drawio "
                    "failed with status {}".format(build_jpg_result_code))

            ImagePreviewBuilderPillow().build_jpeg_preview(
                tmp_jpg.name,
                preview_name,
                cache_path,
                page_id,
                extension,
                size,
                mimetype,
            )
Example #7
0
    def build_jpeg_preview(
        self,
        file_path: str,
        preview_name: str,
        cache_path: str,
        page_id: int,
        extension: str = ".jpg",
        size: ImgDims = None,
        mimetype: str = "",
    ) -> None:
        if not size:
            size = self.default_size
        with tempfile.NamedTemporaryFile("w+b",
                                         prefix="preview-generator-",
                                         suffix=".jpg") as tmp_jpg:
            with Xvfb():
                build_jpg_result_code = check_call(
                    [
                        "drawio",
                        "-x",
                        "-f",
                        "jpg",
                        "-o",
                        tmp_jpg.name,
                        file_path,
                        # INFO - G.M - 12/11/2021 - Add no-sandbox at the end as putting it before
                        # doesn't work, see:
                        # https://github.com/jgraph/drawio-desktop/issues/249#issuecomment-695179747
                        "--no-sandbox",
                    ],
                    stdout=DEVNULL,
                    stderr=STDOUT,
                    timeout=30,
                )

            if build_jpg_result_code != 0:
                raise IntermediateFileBuildingFailed(
                    "Building JPG intermediate file using drawio "
                    "failed with status {}".format(build_jpg_result_code))

            ImagePreviewBuilderWand().build_jpeg_preview(
                tmp_jpg.name, preview_name, cache_path, page_id, extension,
                size, mimetype)