{"record":{"id":"44bc8b272a0b1487","repo":"Yalantis/uCrop","slug":"load-pdf-external-failed-to-load-file-s-with","errorCode":null,"errorMessage":"load_pdf_external(): Failed to load file '%s' with external command 'gs'.","messagePattern":"load_pdf_external\\(\\): Failed to load file '(.+?)' with external command 'gs'\\.","errorType":"exception","errorClass":"CImgIOException","httpStatus":null,"severity":"error","filePath":"ucrop/src/main/jni/CImg.h","lineNumber":59923,"sourceCode":"    CImg<T>& load_pdf_external(const char *const filename, const unsigned int resolution=400) {\n      if (!filename)\n        throw CImgArgumentException(_cimg_instance\n                                    \"load_pdf_external(): Specified filename is (null).\",\n                                    cimg_instance);\n      CImg<charT> command(1024), filename_tmp(256);\n      const CImg<charT> s_filename = CImg<charT>::string(filename)._system_strescape();\n#if cimg_OS==1\n      std::FILE *file = 0;\n      cimg_snprintf(command,command._width,\"gs -q -dNOPAUSE -sDEVICE=ppmraw -o - -r%u \\\"%s\\\"\",\n                    resolution,s_filename.data());\n      file = popen(command,\"r\");\n      if (file) {\n        const unsigned int omode = cimg::exception_mode();\n        cimg::exception_mode(0);\n        try { load_pnm(file); } catch (...) {\n          pclose(file);\n          cimg::exception_mode(omode);\n          throw CImgIOException(_cimg_instance\n                                \"load_pdf_external(): Failed to load file '%s' with external command 'gs'.\",\n                                cimg_instance,\n                                filename);\n        }\n        pclose(file);\n        return *this;\n      }\n#endif\n      do {\n        cimg_snprintf(filename_tmp,filename_tmp._width,\"%s%c%s.ppm\",\n                      cimg::temporary_path(),cimg_file_separator,cimg::filenamerand());\n      } while (cimg::path_exists(filename_tmp));\n      cimg_snprintf(command,command._width,\"gs -q -dNOPAUSE -sDEVICE=ppmraw -o \\\"%s\\\" -r%u \\\"%s\\\"\",\n                    CImg<charT>::string(filename_tmp)._system_strescape().data(),resolution,s_filename.data());\n      cimg::system(command,\"gs\");\n      if (!cimg::path_exists(filename_tmp)) {\n        cimg::fclose(cimg::fopen(filename,\"r\"));\n        throw CImgIOException(_cimg_instance","sourceCodeStart":59905,"sourceCodeEnd":59941,"githubUrl":"https://github.com/Yalantis/uCrop/blob/f788b534b48c144edf786c8cddbf0e029e637804/ucrop/src/main/jni/CImg.h#L59905-L59941","documentation":"On Unix, load_pdf_external() opens a pipe to 'gs -sDEVICE=ppmraw ...' with popen() and parses the PNM stream from the pipe. If parsing that stream (load_pnm) throws, this CImgIOException is thrown: ghostscript produced a stream that CImg could not decode as an image.","triggerScenarios":"Calling load_pdf_external(filename, resolution) on Unix where gs ran but its stdout was not a valid PNM stream — gs printed an error to stdout, the PDF was encrypted/corrupt, gs failed mid-render (missing fonts), or the stream was empty.","commonSituations":"Ghostscript crashing on a malformed or password-protected PDF; 'gs' being a stub script that emits nothing; memory limits killing gs mid-render; gs version producing headers load_pnm cannot parse.","solutions":["Run 'gs -q -dNOPAUSE -sDEVICE=ppmraw -o out.ppm -r400 file.pdf' by hand to see ghostscript's real error.","Check the PDF opens in a viewer / with 'gs -sDEVICE=nullpage file.pdf'; repair or decrypt it first.","Verify a real ghostscript is installed and on PATH ('gs --version').","Lower the -r resolution argument if rendering fails at very high DPI due to memory.","Render to a temp file yourself with gs, then load the PPM/PNG with CImg::load() — this also surfaces gs errors directly."],"exampleFix":"// before\nimg.load_pdf_external(\"doc.pdf\", 400);\n// after\ntry { img.load_pdf_external(\"doc.pdf\", 400); }\ncatch (CImgIOException&) {\n  if (cimg::system(\"gs -q -dNOPAUSE -sDEVICE=ppmraw -o doc.ppm -r150 doc.pdf\"))\n    throw std::runtime_error(\"ghostscript failed on doc.pdf\");\n  img.load(\"doc.ppm\");\n}","handlingStrategy":"try-catch","validationCode":"if (!cimg::is_file(path)) throw std::invalid_argument(\"PDF missing\");\nif (cimg::system(\"gs --version\",\"gs\")!=0) throw std::runtime_error(\"ghostscript not installed\");","typeGuard":"bool gsAvailable(){ return cimg::system(\"gs --version\",\"gs\")==0; }","tryCatchPattern":"try { img.load_pdf_external(path, res); }\ncatch (CImgIOException& e) {\n  // gs stream undecodable: render to file yourself for a real exit status\n  if (cimg::system(\"gs -q -dNOPAUSE -sDEVICE=ppmraw -o page.ppm -r150 <file>\")==0)\n    img.load(\"page.ppm\");\n  else throw;\n}","preventionTips":["Install ghostscript wherever PDF loading is used","Reject encrypted/corrupt PDFs up front","Lower resolution if renders fail on memory","Prefer rendering to a temp file over pipes to capture gs errors"],"tags":["pdf","ghostscript","pnm","external-tool","cimg"],"backgroundTag":"command-not-found","analyzedSha":"f788b534b48c144edf786c8cddbf0e029e637804","analyzedAt":"2026-09-08T08:36:04.887Z","contentChangedAt":"2026-09-08T08:36:04.887Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}