Exemple #1
0
 def cairo_convert(pdffile, pngfile, dpi):
     cmd = [
         "pdftocairo", "-singlefile", "-png",
         "-r %d" % dpi, pdffile,
         os.path.splitext(pngfile)[0]
     ]
     # for some reason this doesn't work without shell
     check_output(" ".join(cmd), shell=True, stderr=subprocess.STDOUT)
Exemple #2
0
 def gs_convert(pdffile, pngfile, dpi):
     cmd = [
         gs, '-dQUIET', '-dSAFER', '-dBATCH', '-dNOPAUSE', '-dNOPROMPT',
         '-sDEVICE=png16m', '-dUseCIEColor', '-dTextAlphaBits=4',
         '-dGraphicsAlphaBits=4', '-dDOINTERPOLATE',
         '-sOutputFile=%s' % pngfile,
         '-r%d' % dpi, pdffile
     ]
     check_output(cmd, stderr=subprocess.STDOUT)
Exemple #3
0
def make_pdf_to_png_converter():
    """
    Returns a function that converts a pdf file to a png file.
    """

    tools_available = []
    # check for pdftocairo
    try:
        check_output(["pdftocairo", "-v"], stderr=subprocess.STDOUT)
        tools_available.append("pdftocairo")
    except:
        pass
    # check for ghostscript
    try:
        gs = "gs" if sys.platform is not "win32" else "gswin32c"
        check_output([gs, "-v"], stderr=subprocess.STDOUT)
        tools_available.append("gs")
    except:
        pass

    # pick converter
    if "pdftocairo" in tools_available:

        def cairo_convert(pdffile, pngfile, dpi):
            cmd = ["pdftocairo", "-singlefile", "-png", "-r %d" % dpi, pdffile, os.path.splitext(pngfile)[0]]
            # for some reason this doesn't work without shell
            check_output(" ".join(cmd), shell=True, stderr=subprocess.STDOUT)

        return cairo_convert
    elif "gs" in tools_available:

        def gs_convert(pdffile, pngfile, dpi):
            cmd = [
                gs,
                "-dQUIET",
                "-dSAFER",
                "-dBATCH",
                "-dNOPAUSE",
                "-dNOPROMPT",
                "-sDEVICE=png16m",
                "-dUseCIEColor",
                "-dTextAlphaBits=4",
                "-dGraphicsAlphaBits=4",
                "-dDOINTERPOLATE",
                "-sOutputFile=%s" % pngfile,
                "-r%d" % dpi,
                pdffile,
            ]
            check_output(cmd, stderr=subprocess.STDOUT)

        return gs_convert
    else:
        raise RuntimeError("No suitable pdf to png renderer found.")
Exemple #4
0
    def _print_pdf_to_fh(self, fh):
        w, h = self.figure.get_figwidth(), self.figure.get_figheight()

        try:
            # create and switch to temporary directory
            tmpdir = tempfile.mkdtemp()
            cwd = os.getcwd()
            os.chdir(tmpdir)

            # print figure to pgf and compile it with latex
            self.print_pgf("figure.pgf")

            latex_preamble = get_preamble()
            latex_fontspec = get_fontspec()
            latexcode = r"""
\documentclass[12pt]{minimal}
\usepackage[paperwidth=%fin, paperheight=%fin, margin=0in]{geometry}
%s
%s
\usepackage{pgf}

\begin{document}
\centering
\input{figure.pgf}
\end{document}""" % (w, h, latex_preamble, latex_fontspec)
            with codecs.open("figure.tex", "w", "utf-8") as fh_tex:
                fh_tex.write(latexcode)

            texcommand = get_texcommand()
            cmdargs = [
                texcommand, "-interaction=nonstopmode", "-halt-on-error",
                "figure.tex"
            ]
            try:
                check_output(cmdargs, stderr=subprocess.STDOUT)
            except subprocess.CalledProcessError as e:
                raise RuntimeError(
                    "%s was not able to process your file.\n\nFull log:\n%s" %
                    (texcommand, e.output))

            # copy file contents to target
            with open("figure.pdf", "rb") as fh_src:
                shutil.copyfileobj(fh_src, fh)
        finally:
            os.chdir(cwd)
            try:
                shutil.rmtree(tmpdir)
            except:
                sys.stderr.write("could not delete tmp directory %s\n" %
                                 tmpdir)
Exemple #5
0
    def _print_pdf_to_fh(self, fh):
        w, h = self.figure.get_figwidth(), self.figure.get_figheight()

        try:
            # create temporary directory for compiling the figure
            tmpdir = tempfile.mkdtemp(prefix="mpl_pgf_")
            fname_pgf = os.path.join(tmpdir, "figure.pgf")
            fname_tex = os.path.join(tmpdir, "figure.tex")
            fname_pdf = os.path.join(tmpdir, "figure.pdf")

            # print figure to pgf and compile it with latex
            self.print_pgf(fname_pgf)

            latex_preamble = get_preamble()
            latex_fontspec = get_fontspec()
            latexcode = r"""
\documentclass[12pt]{minimal}
\usepackage[paperwidth=%fin, paperheight=%fin, margin=0in]{geometry}
%s
%s
\usepackage{pgf}

\begin{document}
\centering
\input{figure.pgf}
\end{document}""" % (w, h, latex_preamble, latex_fontspec)
            with codecs.open(fname_tex, "w", "utf-8") as fh_tex:
                fh_tex.write(latexcode)

            texcommand = get_texcommand()
            cmdargs = [
                texcommand, "-interaction=nonstopmode", "-halt-on-error",
                "figure.tex"
            ]
            try:
                check_output(cmdargs, stderr=subprocess.STDOUT, cwd=tmpdir)
            except subprocess.CalledProcessError as e:
                raise RuntimeError(
                    "%s was not able to process your file.\n\nFull log:\n%s" %
                    (texcommand, e.output))

            # copy file contents to target
            with open(fname_pdf, "rb") as fh_src:
                shutil.copyfileobj(fh_src, fh)
        finally:
            try:
                shutil.rmtree(tmpdir)
            except:
                TmpDirCleaner.add(tmpdir)
Exemple #6
0
    def _print_pdf_to_fh(self, fh):
        w, h = self.figure.get_figwidth(), self.figure.get_figheight()

        try:
            # create and switch to temporary directory
            tmpdir = tempfile.mkdtemp()
            cwd = os.getcwd()
            os.chdir(tmpdir)

            # print figure to pgf and compile it with latex
            self.print_pgf("figure.pgf")

            latex_preamble = get_preamble()
            latex_fontspec = get_fontspec()
            latexcode = r"""
\documentclass[12pt]{minimal}
\usepackage[paperwidth=%fin, paperheight=%fin, margin=0in]{geometry}
%s
%s
\usepackage{pgf}

\begin{document}
\centering
\input{figure.pgf}
\end{document}""" % (
                w,
                h,
                latex_preamble,
                latex_fontspec,
            )
            with codecs.open("figure.tex", "w", "utf-8") as fh_tex:
                fh_tex.write(latexcode)

            texcommand = get_texcommand()
            cmdargs = [texcommand, "-interaction=nonstopmode", "-halt-on-error", "figure.tex"]
            try:
                check_output(cmdargs, stderr=subprocess.STDOUT)
            except subprocess.CalledProcessError as e:
                raise RuntimeError("%s was not able to process your file.\n\nFull log:\n%s" % (texcommand, e.output))

            # copy file contents to target
            with open("figure.pdf", "rb") as fh_src:
                shutil.copyfileobj(fh_src, fh)
        finally:
            os.chdir(cwd)
            try:
                shutil.rmtree(tmpdir)
            except:
                sys.stderr.write("could not delete tmp directory %s\n" % tmpdir)
Exemple #7
0
def make_pdf_to_png_converter():
    """
    Returns a function that converts a pdf file to a png file.
    """

    tools_available = []
    # check for pdftocairo
    try:
        check_output(["pdftocairo", "-v"], stderr=subprocess.STDOUT)
        tools_available.append("pdftocairo")
    except:
        pass
    # check for ghostscript
    try:
        gs = "gs" if sys.platform is not "win32" else "gswin32c"
        check_output([gs, "-v"], stderr=subprocess.STDOUT)
        tools_available.append("gs")
    except:
        pass

    # pick converter
    if "pdftocairo" in tools_available:

        def cairo_convert(pdffile, pngfile, dpi):
            cmd = [
                "pdftocairo", "-singlefile", "-png",
                "-r %d" % dpi, pdffile,
                os.path.splitext(pngfile)[0]
            ]
            # for some reason this doesn't work without shell
            check_output(" ".join(cmd), shell=True, stderr=subprocess.STDOUT)

        return cairo_convert
    elif "gs" in tools_available:

        def gs_convert(pdffile, pngfile, dpi):
            cmd = [
                gs, '-dQUIET', '-dSAFER', '-dBATCH', '-dNOPAUSE', '-dNOPROMPT',
                '-sDEVICE=png16m', '-dUseCIEColor', '-dTextAlphaBits=4',
                '-dGraphicsAlphaBits=4', '-dDOINTERPOLATE',
                '-sOutputFile=%s' % pngfile,
                '-r%d' % dpi, pdffile
            ]
            check_output(cmd, stderr=subprocess.STDOUT)

        return gs_convert
    else:
        raise RuntimeError("No suitable pdf to png renderer found.")
    def _print_pdf_to_fh(self, fh):
        w, h = self.figure.get_figwidth(), self.figure.get_figheight()

        try:
            # create temporary directory for compiling the figure
            tmpdir = tempfile.mkdtemp(prefix="mpl_pgf_")
            fname_pgf = os.path.join(tmpdir, "figure.pgf")
            fname_tex = os.path.join(tmpdir, "figure.tex")
            fname_pdf = os.path.join(tmpdir, "figure.pdf")

            # print figure to pgf and compile it with latex
            self.print_pgf(fname_pgf)

            latex_preamble = get_preamble()
            latex_fontspec = get_fontspec()
            latexcode = r"""
\documentclass[12pt]{minimal}
\usepackage[paperwidth=%fin, paperheight=%fin, margin=0in]{geometry}
%s
%s
\usepackage{pgf}

\begin{document}
\centering
\input{figure.pgf}
\end{document}""" % (w, h, latex_preamble, latex_fontspec)
            with codecs.open(fname_tex, "w", "utf-8") as fh_tex:
                fh_tex.write(latexcode)

            texcommand = get_texcommand()
            cmdargs = [texcommand, "-interaction=nonstopmode",
                       "-halt-on-error", "figure.tex"]
            try:
                check_output(cmdargs, stderr=subprocess.STDOUT, cwd=tmpdir)
            except subprocess.CalledProcessError as e:
                raise RuntimeError("%s was not able to process your file.\n\nFull log:\n%s" % (texcommand, e.output))

            # copy file contents to target
            with open(fname_pdf, "rb") as fh_src:
                shutil.copyfileobj(fh_src, fh)
        finally:
            try:
                shutil.rmtree(tmpdir)
            except:
                TmpDirCleaner.add(tmpdir)
Exemple #9
0
 def gs_convert(pdffile, pngfile, dpi):
     cmd = [
         gs,
         "-dQUIET",
         "-dSAFER",
         "-dBATCH",
         "-dNOPAUSE",
         "-dNOPROMPT",
         "-sDEVICE=png16m",
         "-dUseCIEColor",
         "-dTextAlphaBits=4",
         "-dGraphicsAlphaBits=4",
         "-dDOINTERPOLATE",
         "-sOutputFile=%s" % pngfile,
         "-r%d" % dpi,
         pdffile,
     ]
     check_output(cmd, stderr=subprocess.STDOUT)
 def gs_convert(pdffile, pngfile, dpi):
     cmd = [gs, '-dQUIET', '-dSAFER', '-dBATCH', '-dNOPAUSE', '-dNOPROMPT',
            '-sDEVICE=png16m', '-dUseCIEColor', '-dTextAlphaBits=4',
            '-dGraphicsAlphaBits=4', '-dDOINTERPOLATE', '-sOutputFile=%s' % pngfile,
            '-r%d' % dpi, pdffile]
     check_output(cmd, stderr=subprocess.STDOUT)
 def cairo_convert(pdffile, pngfile, dpi):
     cmd = ["pdftocairo", "-singlefile", "-png",
            "-r %d" % dpi, pdffile, os.path.splitext(pngfile)[0]]
     # for some reason this doesn't work without shell
     check_output(" ".join(cmd), shell=True, stderr=subprocess.STDOUT)