[Tex/LaTex] how to handle corrupted graphics file

graphics

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".

Best Answer

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.

Related Question