Repository: paperless-ngx/paperless-ngx

Issue:
[BUG] PDF import fails with "OverflowError: date value out of range"
### Description

I have one PDF file, that I can not import because the import step where the date is searched is failing.

The result is, that the file does **not get imported at all**.

_Suggestion:_
Catch errors in the date searching step and set a default date (now or so), if an error occurs (I would even go for any error, not only OverflowErrors).

### Steps to reproduce

1. Import wrong PDF file 
2. Error is shown in the file tasks section under 'Failed'

I have a PDF file that triggers this error reproducible. But it has a lot of personal information in it, so I rather do not want to share it in a GitHub issue.

### Webserver logs

```bash
date value out of range : Traceback (most recent call last):
  File "/usr/src/paperless/src/src/django-q/django_q/cluster.py", line 454, in worker
    res = f(*task["args"], **task["kwargs"])
  File "/usr/src/paperless/src/documents/tasks.py", line 154, in consume_file
    document = Consumer().try_consume_file(
  File "/usr/src/paperless/src/documents/consumer.py", line 334, in try_consume_file
    date = parse_date(self.filename, text)
  File "/usr/src/paperless/src/documents/parsers.py", line 221, in parse_date
    return next(parse_date_generator(filename, text), None)
  File "/usr/src/paperless/src/documents/parsers.py", line 280, in parse_date_generator
    yield from __process_content(text, settings.DATE_ORDER)
  File "/usr/src/paperless/src/documents/parsers.py", line 271, in __process_content
    date = __process_match(m, date_order)
  File "/usr/src/paperless/src/documents/parsers.py", line 262, in __process_match
    date = __parser(date_string, date_order)
  File "/usr/src/paperless/src/documents/parsers.py", line 235, in __parser
    return dateparser.parse(
  File "/usr/local/lib/python3.9/site-packages/dateparser/conf.py", line 92, in wrapper
    return f(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/dateparser/__init__.py", line 61, in parse
    data = parser.get_date_data(date_string, date_formats)
  File "/usr/local/lib/python3.9/site-packages/dateparser/date.py", line 428, in get_date_data
    parsed_date = _DateLocaleParser.parse(
  File "/usr/local/lib/python3.9/site-packages/dateparser/date.py", line 178, in parse
    return instance._parse()
  File "/usr/local/lib/python3.9/site-packages/dateparser/date.py", line 182, in _parse
    date_data = self._parsers[parser_name]()
  File "/usr/local/lib/python3.9/site-packages/dateparser/date.py", line 201, in _try_absolute_parser
    return self._try_parser(parse_method=_parse_absolute)
  File "/usr/local/lib/python3.9/site-packages/dateparser/date.py", line 212, in _try_parser
    date_obj, period = date_parser.parse(
  File "/usr/local/lib/python3.9/site-packages/dateparser/conf.py", line 92, in wrapper
    return f(*args, **kwargs)
  File "/usr/local/lib/python3.9/site-packages/dateparser/date_parser.py", line 39, in parse
    date_obj = localize_timezone(date_obj, settings.TIMEZONE)
  File "/usr/local/lib/python3.9/site-packages/dateparser/utils/__init__.py", line 85, in localize_timezone
    date_time = tz.localize(date_time)
  File "/usr/local/lib/python3.9/site-packages/pytz/tzinfo.py", line 323, in localize
    loc_dt = dt + delta
OverflowError: date value out of range
```


### Browser logs

_No response_

### Paperless-ngx version

Paperless-ngx 1.9.2

### Host OS

raspberry pi with Raspian (armv7l)

### Installation method

Docker - official image

### Browser

_No response_

### Configuration changes

nothing significant to trigger a date OverflowError 😉

### Other

_No response_


Discussion:
As a reminder for myself:
`Ihre Simulationsrechnung.pdf`
Are you able to provide a sample PDF?  Or, barring that, the content where it appears to have found a date, but fails?
Yeah, I'm having a hard time getting the right date-like string to produce this (and add a test for it).  If you can get me a snippet of the content to test with, that would really help.

Resolving patch:
diff --git a/src/documents/consumer.py b/src/documents/consumer.py
--- a/src/documents/consumer.py
+++ b/src/documents/consumer.py
@@ -444,7 +444,12 @@ def progress_callback(current_progress, max_progress):
 
         return document
 
-    def _store(self, text, date, mime_type) -> Document:
+    def _store(
+        self,
+        text: str,
+        date: Optional[datetime.datetime],
+        mime_type: str,
+    ) -> Document:
 
         # If someone gave us the original filename, use it instead of doc.
 
diff --git a/src/documents/parsers.py b/src/documents/parsers.py
--- a/src/documents/parsers.py
+++ b/src/documents/parsers.py
@@ -260,7 +260,7 @@ def __process_match(
 
         try:
             date = __parser(date_string, date_order)
-        except (TypeError, ValueError):
+        except Exception:
             # Skip all matches that do not parse to a proper date
             date = None
 


Test patch: