def fix_pdf_with_ghostscript_to_tmp_file(input_doc_fname):
    """Attempt to fix a bad PDF file with a Ghostscript command, writing the output
    PDF to a temporary file and returning the filename.  Caller is responsible for
    deleting the file."""

    if not gs_executable: init_and_test_gs_executable(exit_on_fail=True)
    temp_file_name = get_temporary_filename(extension=".pdf")
    gs_run_command = [gs_executable, "-dSAFER", "-o", temp_file_name,
                    "-dPDFSETTINGS=/prepress", "-sDEVICE=pdfwrite", input_doc_fname]
    try:
        gs_output = get_external_subprocess_output(gs_run_command, print_output=True,
                                             indent_string="   ", env=gs_environment)
    except subprocess.CalledProcessError:
        print("\nError in pdfCropMargins:  Ghostscript returned a non-zero exit"
              "\nstatus when attempting to fix the file:\n   ", input_doc_fname,
              file=sys.stderr)
        cleanup_and_exit(1)
    except UnicodeDecodeError:
        print("\nWarning in pdfCropMargins:  In attempting to repair the PDF file"
              "\nGhostscript produced a message containing characters which cannot"
              "\nbe decoded by the 'utf-8' codec.  Ignoring and hoping for the best.",
              file=sys.stderr)
    return temp_file_name
def fix_pdf_with_ghostscript_to_tmp_file ( input_doc_fname ) : if not gs_executable : init_and_test_gs_executable ( exit_on_fail = True ) temp_file_name = get_temporary_filename ( extension = ".pdf" ) gs_run_command = [ gs_executable , "-dSAFER" , "-o" , temp_file_name , "-dPDFSETTINGS=/prepress" , "-sDEVICE=pdfwrite" , input_doc_fname ] try : gs_output = get_external_subprocess_output ( gs_run_command , print_output = True , indent_string = "   " , env = gs_environment ) except subprocess . CalledProcessError : print ( "\nError in pdfCropMargins:  Ghostscript returned a non-zero exit" "\nstatus when attempting to fix the file:\n   " , input_doc_fname , file = sys . stderr ) cleanup_and_exit ( 1 ) except UnicodeDecodeError : print ( "\nWarning in pdfCropMargins:  In attempting to repair the PDF file" "\nGhostscript produced a message containing characters which cannot" "\nbe decoded by the 'utf-8' codec.  Ignoring and hoping for the best." , file = sys . stderr ) return temp_file_name
Attempt to fix a bad PDF file with a Ghostscript command, writing the output
    PDF to a temporary file and returning the filename.  Caller is responsible for
    deleting the file.