def create_latex_pdf(character, basename, template):
    tex = template.render(character=character)
    # Create tex document
    tex_file = f'{basename}.tex'
    with open(tex_file, mode='w') as f:
        f.write(tex)
    # Convenience function for removing temporary files
    def remove_temp_files(basename_):
        filenames = [
            f'{basename_}.tex', f'{basename_}.aux', f'{basename_}.log'
        ]
        for filename in filenames:
            if os.path.exists(filename):
                os.remove(filename)

    # Compile the PDF
    pdf_file = f'{basename}.pdf'
    output_dir = os.path.abspath(os.path.dirname(pdf_file))
    try:
        result = subprocess.run([
            'pdflatex', '--output-directory', output_dir, tex_file,
            '-halt-on-error'
        ],
                                stdout=subprocess.DEVNULL,
                                timeout=10)
    except FileNotFoundError:
        # Remove temporary files
        remove_temp_files(basename)
        raise exceptions.LatexNotFoundError()
    else:
        if result.returncode == 0:
            remove_temp_files(basename)
        else:
            raise exceptions.LatexError(
                f'Processing of {basename}.tex failed.')
Exemplo n.º 2
0
def create_latex_pdf(character, basename, template, keep_temp_files=False, use_dnd_decorations=False):
    tex = template.render(character=character, use_dnd_decorations=use_dnd_decorations)
    # Create tex document
    tex_file = f'{basename}.tex'
    with open(tex_file, mode='w', encoding="utf-8") as f:
        f.write(tex)
    # Convenience function for removing temporary files
    def remove_temp_files(basename_):
        filenames = [f'{basename_}.tex', f'{basename_}.aux',
                     f'{basename_}.log']
        for filename in filenames:
            if os.path.exists(filename):
                os.remove(filename)
    # Compile the PDF
    pdf_file = f'{basename}.pdf'
    output_dir = os.path.abspath(os.path.dirname(pdf_file))
    tex_command_line = ['pdflatex', '--output-directory', output_dir,
                        '-halt-on-error', '-interaction=nonstopmode',
                        tex_file]
    passes = 2 if use_dnd_decorations else 1
    try:
        for i in range(passes):
            result = subprocess.run(tex_command_line,
                                    stdout=subprocess.DEVNULL, timeout=30)
    except FileNotFoundError:
        # Remove temporary files
        remove_temp_files(basename)
        raise exceptions.LatexNotFoundError()
    else:
        if result.returncode == 0 and not keep_temp_files:
            remove_temp_files(basename)
        if result.returncode != 0:
            raise exceptions.LatexError(f'Processing of {basename}.tex failed.'
                                        f' See {basename}.log for details.')
Exemplo n.º 3
0
def create_latex_pdf(
    tex: str,
    basename: str,
    keep_temp_files: bool = False,
    use_dnd_decorations: bool = False,
):
    # Create tex document
    tex_file = f"{basename}.tex"
    with open(tex_file, mode="w", encoding="utf-8") as f:
        f.write(tex)

    # Compile the PDF
    pdf_file = Path(f"{basename}.pdf")
    output_dir = pdf_file.resolve().parent
    tex_command_line = [
        "pdflatex",
        "--output-directory",
        str(output_dir),
        "-halt-on-error",
        "-interaction=nonstopmode",
        str(tex_file),
    ]

    environment = os.environ
    tex_env = environment.get('TEXINPUTS', '')
    module_root = Path(__file__).parent / "modules/"
    module_dirs = [module_root / mdir for mdir in ["DND-5e-LaTeX-Template"]]
    log.debug(f"Loading additional modules from {module_dirs}.")
    environment['TEXINPUTS'] = f".:{':'.join(str(d) for d in module_dirs)}:" + tex_env
    passes = 2 if use_dnd_decorations else 1
    log.debug(tex_command_line)
    log.debug("LaTeX command: %s" % " ".join(tex_command_line))
    log.debug("LaTeX environ: %s" % environment)
    try:
        for i in range(passes):
            result = subprocess.run(
                tex_command_line, stdout=subprocess.DEVNULL, env=environment, timeout=30
            )
    except FileNotFoundError:
        # Remove temporary files
        _remove_temp_files(basename)
        raise exceptions.LatexNotFoundError()
    else:
        if result.returncode == 0 and not keep_temp_files:
            _remove_temp_files(basename)
        if result.returncode != 0:
            # Prepare to raise an exception
            logfile = Path(f"{basename}.log")
            err_msg = f"Processing of {basename}.tex failed. See {logfile} for details."
            # Load the log file for more details
            tex_error_msg = tex_error(logfile)
            if tex_error_msg:
                for line in tex_error_msg.split("\n"):
                    log.error(line)
            raise exceptions.LatexError(err_msg)
Exemplo n.º 4
0
def create_latex_pdf(tex, basename, keep_temp_files=False, use_dnd_decorations=False):
    # Create tex document
    tex_file = f"{basename}.tex"
    with open(tex_file, mode="w", encoding="utf-8") as f:
        f.write(tex)

    # Compile the PDF
    pdf_file = Path(f"{basename}.pdf")
    output_dir = pdf_file.resolve().parent
    tex_command_line = [
        "pdflatex",
        "--output-directory",
        output_dir,
        "-halt-on-error",
        "-interaction=nonstopmode",
        tex_file,
    ]
    passes = 2 if use_dnd_decorations else 1
    try:
        for i in range(passes):
            result = subprocess.run(
                tex_command_line, stdout=subprocess.DEVNULL, timeout=30
            )
    except FileNotFoundError:
        # Remove temporary files
        _remove_temp_files(basename)
        raise exceptions.LatexNotFoundError()
    else:
        if result.returncode == 0 and not keep_temp_files:
            _remove_temp_files(basename)
        if result.returncode != 0:
            # Prepare to raise an exception
            logfile = Path(f"{basename}.log")
            err_msg = f"Processing of {basename}.tex failed. See {logfile} for details."
            # Load the log file for more details
            tex_error_msg = tex_error(logfile)
            if tex_error_msg:
                for line in tex_error_msg.split("\n"):
                    log.error(line)
            raise exceptions.LatexError(err_msg)