Details

    • Type: Improvement Improvement
    • Status: Closed
    • Priority: Major Major
    • Resolution: Fixed
    • Affects Version/s: 1.6.0
    • Fix Version/s: 1.7.0
    • Component/s: Parsing
    • Labels:
      None

      Description

      Currently PDF parsing is done in sequential manner resulting in problems with stream parsing and skipping unused content. The solution is a conforming parser which first reads XREF tables and uses this information to only parse required objects and uses length information for stream parsing. A completely new implementation of such a parser is currently worked on in PDFBOX-1000. While this parser will be the long term solution a short term solution based on existing code would be desirable. A first incomplete solution was presented in PDFBOX-1104.

      Starting from PDFBOX-1104 I have implemented an 'as much as possible' conforming parser, called 'non-sequential parser', which handles all PDF documents (even inlined, with object streams etc.). The parser can be used as a drop-in-replacement for PDFParser (subclass of PDFParser). It overwrites method parse and getPage method. The only restriction is currently the need to specify a file instead of an input stream. In order to efficiently read the file and use it with the existing object parsing code I developed a RandomAccessBufferedFileInputStream which allows InputStream operations in combination with seek operations and cached read data.

      In order to use NonSequentialPDFParser small changes and additions on existing classes are needed. This includes changing some methods/fields from private to protected in PDFParser, add parsing of stream object information from XREF streams, store and get this information from XrefTrailerResolver (object ids are stored negated in order to distinguish them from offsets) and allow resetting offset in PushBackInputStream. All these changes do not change behavior of current parser. Another requirement is the long offset patch (PDFBOX-1196) which is excluded from the patch set provided here.

      The provided parser currently works in a forceParsing=false mode resulting in an IOException if a parsing error occurs. In most cases this shouldn't be a problem since in my use cases exceptions typically occur trying to parse unused content or streams which with this new parser are no problems anymore. In my setup I use the new parser first and if a parsing error occurs, fall back to the sequential parser (a bit like Acrobat does it if XREF information is buggy):

      try

      { // ---- try first with (mostly) standard conform parsing doc = PDDocument.loadNonSeq( PDF_FILE, raBuf ); handleDocument(doc); }

      catch ( IOException ioe )

      { // ---- retry with sequential parser and force parsing doc = PDDocument.load( new FileInputStream(PDF_FILE), raBuf, true ); handleDocument(doc); }

      For me this new parser works very well on large document collections and is a large step forward to parse all documents also accepted by common PDF tools. While its behavior is nearly 'conform' there is nevertheless a need for a clean 'real' conforming parser. For instance since the underlying object structure has no access to the parser it is necessary to first parse all objects before they can be used. This includes objects that might not be needed at all. Another normally not needed step is copying the content of a stream. Since we work on a file with random access there would be no need for it. However this parser should fill the hole until a full featured and clean conforming parser is available.

      1. 2012-01-02_NonSequentialParser.patch
        8 kB
        Timo Boehme
      2. 2012-03-12_NonSeqParser_neededChanges.patch
        20 kB
        Timo Boehme
      3. NonSequentialPDFParser.java
        41 kB
        Timo Boehme
      4. NonSequentialPDFParser.java
        33 kB
        Timo Boehme
      5. RandomAccessBufferedFileInputStream.java
        7 kB
        Timo Boehme
      6. RandomAccessBufferedFileInputStream.java
        7 kB
        Timo Boehme

        Issue Links

          Activity

          Hide
          Timo Boehme added a comment -

          the new non-sequential PDF parser

          Show
          Timo Boehme added a comment - the new non-sequential PDF parser
          Hide
          Timo Boehme added a comment -

          file input stream implementation which caches read data and allows seek operations

          Show
          Timo Boehme added a comment - file input stream implementation which caches read data and allows seek operations
          Hide
          Timo Boehme added a comment -

          changes to existing classes in order to use the new parser (please apply PDFBOX-1196 before this patch set)

          Show
          Timo Boehme added a comment - changes to existing classes in order to use the new parser (please apply PDFBOX-1196 before this patch set)
          Hide
          Maruan Sahyoun added a comment -

          I had a quick look at the changes made and I think that this is a very good step forward. The new parsing of the xref should resolve a lot of current issues as do a lot of the other changes. As I'm currently working on PDFBOX-1000 maybe we could have a quick chat about how to combine the efforts.

          Show
          Maruan Sahyoun added a comment - I had a quick look at the changes made and I think that this is a very good step forward. The new parsing of the xref should resolve a lot of current issues as do a lot of the other changes. As I'm currently working on PDFBOX-1000 maybe we could have a quick chat about how to combine the efforts.
          Hide
          Timo Boehme added a comment -

          The missing decryption support is now added to the parser. The parser is tested on multiple thousands of documents and could now be added.
          I have attached the current parser (which will get some cleanup/reformatting before committing) and the required changes/additions; especially small restructuring in the security handler classes in order to use decryption for single objects directly by the parser (move most of the 'decrypt' preparation code out to an extra method).

          If no one objects I would commit the code in small steps: (1) add the RandomAccessBufferedFileInputStream with small change in PushBackInputStream, (2) add changes to security handler classes, (3) add the parser.

          Show
          Timo Boehme added a comment - The missing decryption support is now added to the parser. The parser is tested on multiple thousands of documents and could now be added. I have attached the current parser (which will get some cleanup/reformatting before committing) and the required changes/additions; especially small restructuring in the security handler classes in order to use decryption for single objects directly by the parser (move most of the 'decrypt' preparation code out to an extra method). If no one objects I would commit the code in small steps: (1) add the RandomAccessBufferedFileInputStream with small change in PushBackInputStream, (2) add changes to security handler classes, (3) add the parser.
          Hide
          Timo Boehme added a comment -

          With commit revisions 1311015, 1311016, 1311018 and 1311020 the new parser is added to PDFBOX. Simply call the new PDDocument.loadNonSeq method to use this parser. If parsing fails (e.g. wrong startxref) one can fall back to standard parser with the other load methods.

          Show
          Timo Boehme added a comment - With commit revisions 1311015, 1311016, 1311018 and 1311020 the new parser is added to PDFBOX. Simply call the new PDDocument.loadNonSeq method to use this parser. If parsing fails (e.g. wrong startxref) one can fall back to standard parser with the other load methods.

            People

            • Assignee:
              Timo Boehme
              Reporter:
              Timo Boehme
            • Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved:

                Development