<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def convert_to_foldable(input_filename: str, output_filename: str, slice_horiz: int, slice_vert: int, overwrite: bool = False, longedge: bool = False, latex_paper_size: str = LATEX_PAPER_SIZE_A4) -> bool: """ Runs a chain of tasks to convert a PDF to a useful booklet PDF. """

    if not os.path.isfile(input_filename):
        log.warning("Input file does not exist or is not a file")
        return False
    if not overwrite and os.path.isfile(output_filename):
        log.error("Output file exists; not authorized to overwrite (use "
                  "--overwrite if you are sure)")
        return False
    log.info("Processing {!r}", input_filename)
    with tempfile.TemporaryDirectory() as tmpdir:
        log.debug("Using temporary directory {!r}", tmpdir)
        intermediate_num = 0

        def make_intermediate() -> str:
            nonlocal intermediate_num
            intermediate_num += 1
            return os.path.join(tmpdir,
                                "intermediate_{}.pdf".format(intermediate_num))

        # Run this as a chain, rewriting input_filename at each step:
        # Slice, if necessary.
        input_filename = slice_pdf(
            input_filename=input_filename,
            output_filename=make_intermediate(),
            slice_horiz=slice_horiz,
            slice_vert=slice_vert
        )
        # Make the final n-up
        input_filename = booklet_nup_pdf(
            input_filename=input_filename,
            output_filename=make_intermediate(),
            latex_paper_size=latex_paper_size
        )
        # Rotate?
        if longedge:
            input_filename = rotate_even_pages_180(
                input_filename=input_filename,
                output_filename=make_intermediate(),
            )
        # Done.
        log.info("Writing to {!r}", output_filename)
        shutil.move(input_filename, output_filename)
    return True