I am generating a latex file with many \includegraphics commands from a program. The image files change from run to run. The latex file might, for example, look like the one in this question. Occasionally an image file that I receive is corrupted. I can eliminate nearly all such situations by checking the size of the image file and emitting some vertical space instead of the \includegraphics command should the the image file be too small to reasonably be the sort of image I am expecting; however, a very small number of corrupted image files are larger. In that case pdflatex stops with an error message. All my image files are .jpg files. I believe that jpegtran on UNIX will fix most of these and I could pass every jpeg through that but I am concerned about the impact on runtime if I do that and also there could still be a very few that that does not address. Is there a way to handle this? Basically I need something like: "include a jpg file using includegraphics without generating an error and if that fails then emit some vertical space otherwise the includegraphics can stand".

  • Are your images all in the same folder? I would run some external script that can pick up the corrupted images and replace them with some standard faulty image (like mwe's example-image-a).
    – Werner
    Commented Sep 21, 2012 at 21:56
  • I could run jpegtran on them all in advance but its not really that convenient and also I am not sure that that would necessarily catch them all although it probably would catch most. I would prefer if all this were self contained in the report so I don't have to start messing with external processes. Commented Sep 21, 2012 at 22:00
  • TeX can't even adequately handle bounding box issues, so I don't think it would be able to detect corrupted JPGs internally/at compile time (let alone efficiently).
    – Werner
    Commented Sep 21, 2012 at 22:07
  • 1
    So there is no try...catch processing possible in tex? Seems like a serious omission. Commented Sep 21, 2012 at 22:55
  • Perhaps related: Trapping LaTeX error/warning
    – Werner
    Commented Sep 21, 2012 at 22:58

1 Answer 1


The error you get:

Error: pdflatex (file corrupt.jpg): reading JPEG image failed 
==> Fatal error occurred, the output PDF file is not finished!

Even with nonstopmode active indicates to me that this is a pdfTeX specific issue which occurs in the special PDF-related code of the binary which is responsible for reading JPEG files and not a real TeX error.

Therefore there seems to be nothing you can do in (La)TeX itself. If you can't manually sort out the corrupted files then I would try to write a script which compiles a small test document for each image and checks if the compilation was successful.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .