A user had the following issue with jPDFWriter: After generating a PDF document with jPDFWriter, he noticed that the PDF would fail when the PDF was added as a figure in a TeX document with the following error message: ** WARNING ** Invalid xref table entry [0]. PDF file is corrupted . The issue is due to an extra line between the xref table and the trailer entry in the PDF created. Tex Software bumps on this line when other PDF viewers have no problem with it.

Update: The line after the xref table is no longer added to PDF documents generated by jPDFWriter v2016R1.01 and higher.

How to “Fix” the PDFs?

The user provided a small program to run on the created PDFs to remove this extra line: “It is a free (and anonymous) gift to the world. And lots of thanks to you for providing jPDFWriter for free.” Here is his code below. We publish it in case this can help other users.

/**
	 * Fix the incompatibility issue with TeX, remove the problematic line.
	 * PDFs from jPDFWriter (versions prior to v2016R1.01) had an empty line 
	 * (some non-character and a line shift) before the word "trailer". Most 
	 * PDF readers accept this, but TeX does not.
	 * WARNING. This code contains no checks to establish whether the fix is 
	 * needed, it will always delete two bytes before the word "trailer", if a 
	 * such is found, this is potentially HARMFUL.
	 */
static void jPDFWriterFix(String fileName) {
byte in[] = readAllBytes(new File(fileName));
int i = 0, t = 0;
// locate "trailer" (we assume that there is only one occurrence)
while (i <= in.length - 7) 
{
 if (in[i] == 't' && in[i+1] == 'r' && in[i+2] == 'a' && in[i+3] == 'i' && in[i+4] == 'l' && in[i+5] == 'e' && in[i+6] == 'r')
 {
  t = i;
  break;
 }
 i++;
}
// if "trailer" exists, compose new PDF omitting the two bytes before
if (t >= 2) 
{ 
 byte out[] = new byte[in.length - 2];
 System.arraycopy(in, 0, out, 0, t - 2);
 System.arraycopy(in, t, out, t - 2, in.length - t);
 writeAllBytes(out, new File(fileName));
 }
}