Repository: paperless-ngx/paperless-ngx

Issue:
[BUG] Paperless-ngx 2.0.0 upload error: ocrmypdf.exceptions.ColorConversionNeededError: The input PDF has an unusual color space
### Description

After the update from paperless-ngx 1.17.4 to 2.0.0 the PDF upload failed. The following error message was shown:
![image](https://github.com/paperless-ngx/paperless-ngx/assets/118683900/5e045bc1-ee57-435f-b2bb-036d073b741e)
I never had this issue before with version 1.17.4 and in the past documents created from the same company have been uploaded without any issues.

### Steps to reproduce

1. PDF selected for the upload
2. Paperless start with processing the PDF document
3. Then after a few second the process stopped and the error message below was shown
4. 
![image](https://github.com/paperless-ngx/paperless-ngx/assets/118683900/4f696a27-ae51-434e-b60d-02b474bdc080)


### Webserver logs

```bash
File "/usr/local/lib/python3.11/site-packages/ocrmypdf/builtin_plugins/ghostscript.py", line 119, in generate_pdfa

    ghostscript.generate_pdfa(

  File "/usr/local/lib/python3.11/site-packages/ocrmypdf/_exec/ghostscript.py", line 296, in generate_pdfa

    raise ColorConversionNeededError()

ocrmypdf.exceptions.ColorConversionNeededError: The input PDF has an unusual color space. Use

--color-conversion-strategy to convert to a common color space

such as RGB, or use --output-type pdf to skip PDF/A conversion

and retain the original color space.

The above exception was the direct cause of the following exception:

Traceback (most recent call last):

  File "/usr/local/lib/python3.11/site-packages/asgiref/sync.py", line 349, in main_wrap

    raise exc_info[1]

  File "/usr/src/paperless/src/documents/consumer.py", line 441, in try_consume_file

    document_parser.parse(self.path, mime_type, self.filename)

  File "/usr/src/paperless/src/paperless_tesseract/parsers.py", line 388, in parse

    raise ParseError(f"{e.__class__.__name__}: {e!s}") from e

documents.parsers.ParseError: ColorConversionNeededError: The input PDF has an unusual color space. Use

--color-conversion-strategy to convert to a common color space

such as RGB, or use --output-type pdf to skip PDF/A conversion

and retain the original color space
```


### Browser logs

_No response_

### Paperless-ngx version

2.0.0

### Host OS

Synology DMS-Version: DSM 7.2.1-69057 Update 3

### Installation method

Docker - official image

### Browser

Chrome

### Configuration changes

Non

### Other

_No response_


Discussion:
As you can tell, that is being raised by the tool we use to process PDFs, we cant really control the changes they make. On the paperless side you'd be able to affect this with https://docs.paperless-ngx.com/configuration/#PAPERLESS_OCR_USER_ARGS using the setting the output references
Thanks for your swift reply. Don't understand your answer. And I've no idea how to change this settings. Can you help me on this? Is it possible to get back the previous version?
If many users are running into this we may want to look into further
Looks like it was added [here](https://github.com/ocrmypdf/OCRmyPDF/blob/main/docs/release_notes.rst#v1542).  Pretty slim.  And the command doesn't actually say what the options even are to provide for `--color-conversion-strategy`.
Even something like

```py
        if settings.OCR_OUTPUT_TYPE == "pdfa":
            ocrmypdf_args["color-conversion-strategy"] = "LeaveColorUnchanged"
```
They're defined here: https://github.com/ocrmypdf/OCRmyPDF/blob/9898904be70bf957154cc66cb5d6f57bac7c26b4/src/ocrmypdf/_exec/ghostscript.py#L24-L32

I don't know that we can choose the best option for the output, given that possible set of options
Im not totally clear but think its complaining we're not explicitly setting anything, so 'LeaveColorUnchanged' would be enough, presumably?
Using the sample from #4707, setting that as a user arg isn't enough, we'd need to directly set something like "RGB" to actually convert to something.  But then we're making a decision which might not be the best.  Ugh, PDFs are annoying.
The output when choosing RGB does look alright though
Ah, bummer. So two options:

1. We leave it to the user to set just like other args.
2. We use RGB and let users override that.

I think 2 would be fine, we've always had users running into color conversion quirks, in some ways that would still be more control than currently. Cant say that I really know how significant the ramifications of it would be

Resolving patch:
diff --git a/src/paperless/settings.py b/src/paperless/settings.py
--- a/src/paperless/settings.py
+++ b/src/paperless/settings.py
@@ -864,6 +864,11 @@ def default_threads_per_worker(task_workers) -> int:
 if os.environ.get("PAPERLESS_OCR_MAX_IMAGE_PIXELS") is not None:
     OCR_MAX_IMAGE_PIXELS: int = int(os.environ.get("PAPERLESS_OCR_MAX_IMAGE_PIXELS"))
 
+OCR_COLOR_CONVERSION_STRATEGY = os.getenv(
+    "PAPERLESS_OCR_COLOR_CONVERSION_STRATEGY",
+    "RGB",
+)
+
 OCR_USER_ARGS = os.getenv("PAPERLESS_OCR_USER_ARGS", "{}")
 
 # GNUPG needs a home directory for some reason
diff --git a/src/paperless_tesseract/parsers.py b/src/paperless_tesseract/parsers.py
--- a/src/paperless_tesseract/parsers.py
+++ b/src/paperless_tesseract/parsers.py
@@ -186,6 +186,11 @@ def construct_ocrmypdf_parameters(
             "progress_bar": False,
         }
 
+        if "pdfa" in ocrmypdf_args["output_type"]:
+            ocrmypdf_args[
+                "color_conversion_strategy"
+            ] = settings.OCR_COLOR_CONVERSION_STRATEGY
+
         if settings.OCR_MODE == "force" or safe_fallback:
             ocrmypdf_args["force_ocr"] = True
         elif settings.OCR_MODE in ["skip", "skip_noarchive"]:


Test patch: