title: How to further process a buggy / malformed PDF that cannot be parsed by Tika / PDFBox but can by Evince / Libre Office Draw?
body: <p>My program is reading documents with Tika 2.24 to extract their contents.</p>
  <p>Yet some PDFs (maybe buggy or malformed) cannot be processed by PDFBox although Evince, Libre Office Draw or even Gimp can open them.</p>
  <p>I cannot share these PDFs but what I can tell is that they used to trigger <a href="https://issues.apache.org/jira/browse/PDFBOX-4623" rel="nofollow noreferrer">a StackOverFlow Error as described on Jira</a> with PDFBox 2.0.25 and now trigger an IOException with PDFBox 2.0.26 :</p>
  <blockquote>
  <p>Caused by: java.io.IOException: Possible recursion detected when dereferencing object 29 0</p>
  </blockquote>
  <p>Consequently now that an IOException can be caught it is tempting to try and process a malformed PDF differently from the first parsing that triggered the IOException.</p>
  <p>I read <a href="https://stackoverflow.com/a/65196613/6351897">that PDFBox offers a way to handle malformed PDFs</a> by setting <code>setLenient(true)</code> on a parser but could not find a way to set such leniency in Tika.</p>
  <p>By the way I followed <a href="https://stackoverflow.com/a/65196613/6351897">the solution</a> with both  <code>setLenient(true and false)</code> but the IOException still appears.</p>
  <p>Edit : following KJ's suggestion I ran pdftotext which output the following warnings :</p>
  <blockquote>
  <p>Syntax Error (5602): Object '29 0 obj' is being already parsed
  Syntax Error (5603): Bad 'Length' attribute in stream
  Syntax Error (8596): Missing 'endstream' or incorrect stream length
  Syntax Error (16945): Object '35 0 obj' is being already parsed
  Syntax Error (16946): Bad 'Length' attribute in stream
  Syntax Error (23267): Missing 'endstream' or incorrect stream length
  Syntax Error (23332): Object '37 0 obj' is being already parsed
  Syntax Error (23333): Bad 'Length' attribute in stream
  Syntax Error (28645): Missing 'endstream' or incorrect stream length</p>
  </blockquote>
  <p>(Please note : there are 4 pages which seem to be malformed as PDFSam cannot export them separately).</p>
  <p>Opening the pdf file in Text Editor as suggested by KJ did only reveal a single hit for &quot;29 0 obj&quot;. Using <code>mutool show -be mypdf.pdf 29</code> outputs a <code>warning: PDF stream Length incorrect</code> and then the compressed content.</p>
  <p>[QPDF check]
  Still following KJ advices, running QPDF with <code>check</code> flag yields:</p>
  <blockquote>
  <p>checking myPDFWithIssues.pdf<br />
  PDF Version: 1.5<br />
  File is not encrypted<br />
  File is not linearized<br />
  WARNING: myPDFWithIssues.pdf (offset 5602): loop detected resolving object 29 0<br />
  WARNING: myPDFWithIssues.pdf (object 29 0, offset 5552): /Length key in stream dictionary is not an integer<br />
  WARNING: myPDFWithIssues.pdf (object 29 0, offset 5603): attempting to recover stream length<br />
  WARNING: myPDFWithIssues.pdf (object 29 0, offset 5603): recovered stream length: 2983<br />
  WARNING: myPDFWithIssues.pdf (offset 16945): loop detected resolving object 35 0<br />
  WARNING: myPDFWithIssues.pdf (object 35 0, offset 16895): /Length key in stream dictionary is not an integer<br />
  WARNING: myPDFWithIssues.pdf (object 35 0, offset 16946): attempting to recover stream length<br />
  WARNING: myPDFWithIssues.pdf (object 35 0, offset 16946): recovered stream length: 6311<br />
  WARNING: myPDFWithIssues.pdf (offset 23332): loop detected resolving object 37 0<br />
  WARNING: myPDFWithIssues.pdf (object 37 0, offset 23282): /Length key in stream dictionary is not an integer<br />
  WARNING: myPDFWithIssues.pdf (object 37 0, offset 23333): attempting to recover stream length<br />
  WARNING: myPDFWithIssues.pdf (object 37 0, offset 23333): recovered stream length: 5302</p>
  </blockquote>
  <p>Yet the faulty PDF has been regenerated by another user (from the same sources) and the newer PDF does not show any warnings. So issue will be hard to track!</p>
  <p>So my question is : how can I process with Tika / PDFBox malformed PDFs that trigger the aforementioned IOException related to possible recursion ?</p>
  <p>Any hint appreciated</p>