Forum Bugs

Prince generates invalid PDF when file is open in PDF viewer

mikkelricky
We're experiencing a strange problem on a single MacBook Pro where Prince generates invalid PDFs, i.e. PDFs that cannot be opened due to errors in the PDF.

The problem has been tracked down to (an earlier version of) the PDF being opened in a PDF viewer when running Prince. That is, the error can be reproduced like this
  • Run Prince to generate PDF
  • Open PDF in PDF viewer, e.g. Preview
  • Run Prince again to overwrite the PDF
  • When refreshing in PDF viewer, the viewer crashes or complains about the PDF being invalid

Has anybody else experienced something similar?
mikeday
Can you make a copy of the original PDF file and compare it to see what is going on? For example, is it being truncated to zero bytes in length, or corrupted in some other way?
mikkelricky
The original (working) PDF file and the corrupt one have exactly the same size (50193595 bytes). However, when diff'ing the files, i.e. comparing them line by line, it turns out that they start to differ at line 231471 (out of 232150 lines in the original PDF file), and from this point on the corrupt file contains something looking like binary stuff whereas the original contains some plain text.

I have attached the diff between the files, but I'm not sure it makes any sense to anyone.
  1. invalid-pdf.diff.zip5.8 kB
    diff of invalid and valid pdf. Invalid pdf comes first.
mikeday
Seems that most of the object index at the end of the PDF file is being corrupted, which will make it difficult to load the file. Perhaps MacOS X Preview is locking the file in some way that makes it impossible for Prince to fully overwrite it? Or does the file only get corrupted when you explicitly hit refresh in the viewer? Adobe Reader locks the PDF file on Windows, but makes it entirely unwritable, so that Prince immediately fails with an error message.
mikkelricky
mikeday wrote:
[...] does the file only get corrupted when you explicitly hit refresh in the viewer?

That's a really good question. I will test this out tomorrow when I can get my hands on the affected MacBook.
Prince finishes successfully and doesn't complain about the file being locked (as would be the case on Windows), but it will be interesting to know if it's Prince or the PDF viewer that corrupts the PDF. Thanks for asking that question.