2025-04-17 15:03:21 -07:00
|
|
|
% SPDX-FileCopyrightText: 2022 James R. Barlow
|
|
|
|
|
% SPDX-License-Identifier: CC-BY-SA-4.0
|
2022-07-28 01:06:46 -07:00
|
|
|
|
2025-04-17 15:03:21 -07:00
|
|
|
# Plugins
|
2019-06-20 02:44:29 -07:00
|
|
|
|
2025-04-17 15:03:21 -07:00
|
|
|
> The key words "MUST", "MUST NOT", "REQUIRED", "SHALL", "SHALL
|
|
|
|
|
> NOT", "SHOULD", "SHOULD NOT", "RECOMMENDED", "MAY", and
|
|
|
|
|
> "OPTIONAL" in this document are to be interpreted as described in
|
|
|
|
|
> RFC 2119.
|
2020-07-21 23:53:30 -07:00
|
|
|
|
2020-05-07 03:53:37 -07:00
|
|
|
You can use plugins to customize the behavior of OCRmyPDF at certain points of
|
|
|
|
|
interest.
|
2019-06-20 02:44:29 -07:00
|
|
|
|
2020-05-07 03:53:37 -07:00
|
|
|
Currently, it is possible to:
|
|
|
|
|
|
|
|
|
|
- add new command line arguments
|
|
|
|
|
- override the decision for whether or not to perform OCR on a particular file
|
|
|
|
|
- modify the image is about to be sent for OCR
|
|
|
|
|
- modify the page image before it is converted to PDF
|
2020-07-21 23:53:30 -07:00
|
|
|
- replace the Tesseract OCR with another OCR engine that has similar behavior
|
|
|
|
|
- replace Ghostscript with another PDF to image converter (rasterizer) or
|
|
|
|
|
PDF/A generator
|
2020-05-07 03:53:37 -07:00
|
|
|
|
2025-04-17 15:03:21 -07:00
|
|
|
OCRmyPDF plugins are based on the Python `pluggy` package and conform to its
|
2020-05-07 03:53:37 -07:00
|
|
|
conventions. Note that: plugins installed with as setuptools entrypoints are
|
|
|
|
|
not checked currently, because OCRmyPDF assumes you may not want to enable
|
2020-07-03 16:16:01 -07:00
|
|
|
plugins for all files.
|
2019-06-20 02:44:29 -07:00
|
|
|
|
2025-04-17 15:03:21 -07:00
|
|
|
See \[OCRmyPDF-EasyOCR\](<https://github.com/ocrmypdf/OCRmyPDF-EasyOCR>) for an
|
2024-08-31 01:14:51 -07:00
|
|
|
example of a straightforward, fully working plugin.
|
|
|
|
|
|
2025-04-17 15:03:21 -07:00
|
|
|
## Script plugins
|
2020-05-07 03:53:37 -07:00
|
|
|
|
|
|
|
|
Script plugins may be called from the command line, by specifying the name of a file.
|
2020-07-03 16:16:01 -07:00
|
|
|
Script plugins may be convenient for informal or "one-off" plugins, when a certain
|
|
|
|
|
batch of files needs a special processing step for example.
|
2020-05-07 03:53:37 -07:00
|
|
|
|
2025-04-17 15:03:21 -07:00
|
|
|
```bash
|
|
|
|
|
ocrmypdf --plugin ocrmypdf_example_plugin.py input.pdf output.pdf
|
|
|
|
|
```
|
2020-05-07 03:53:37 -07:00
|
|
|
|
2025-04-17 15:03:21 -07:00
|
|
|
Multiple plugins may be installed by issuing the `--plugin` argument multiple times.
|
2020-05-07 03:53:37 -07:00
|
|
|
|
2025-04-17 15:03:21 -07:00
|
|
|
## Packaged plugins
|
2020-05-07 03:53:37 -07:00
|
|
|
|
|
|
|
|
Installed plugins may be installed into the same virtual environment as OCRmyPDF
|
|
|
|
|
is installed into. They may be invoked using Python standard module naming.
|
2020-07-03 16:16:01 -07:00
|
|
|
If you are intending to distribute a plugin, please package it.
|
2020-05-07 03:53:37 -07:00
|
|
|
|
2025-04-17 15:03:21 -07:00
|
|
|
```bash
|
|
|
|
|
ocrmypdf --plugin ocrmypdf_fancypants.pockets.contents input.pdf output.pdf
|
|
|
|
|
```
|
2020-05-07 03:53:37 -07:00
|
|
|
|
|
|
|
|
OCRmyPDF does not automatically import plugins, because the assumption is that
|
|
|
|
|
plugins affect different files differently and you may not want them activated
|
2025-04-17 15:03:21 -07:00
|
|
|
all the time. The command line or `ocrmypdf.ocr(plugin='...')` must call
|
2020-05-07 03:53:37 -07:00
|
|
|
for them.
|
|
|
|
|
|
|
|
|
|
Third parties that wish to distribute packages for ocrmypdf should package them
|
2025-04-17 15:03:21 -07:00
|
|
|
as packaged plugins, and these modules should begin with the name `ocrmypdf_`
|
|
|
|
|
similar to `pytest` packages such as `pytest-cov` (the package) and
|
|
|
|
|
`pytest_cov` (the module).
|
2020-05-07 03:53:37 -07:00
|
|
|
|
2025-04-17 15:03:21 -07:00
|
|
|
:::{note}
|
|
|
|
|
We recommend plugin authors name their plugins with the prefix
|
|
|
|
|
`ocrmypdf-` (for the package name on PyPI) and `ocrmypdf_` (for the
|
|
|
|
|
module), just like pytest plugins. At the same time, please make it clear
|
|
|
|
|
that your package is not official.
|
|
|
|
|
:::
|
2020-07-03 16:16:01 -07:00
|
|
|
|
2025-04-17 15:03:21 -07:00
|
|
|
## Plugins
|
2021-01-26 01:40:40 -08:00
|
|
|
|
|
|
|
|
You can also create a plugin that OCRmyPDF will always automatically load if both are
|
2024-08-31 01:14:51 -07:00
|
|
|
installed in the same virtual environment, using a project entrypoint.
|
|
|
|
|
OCRmyPDF uses the entrypoint namespace "ocrmypdf".
|
2021-01-26 01:40:40 -08:00
|
|
|
|
2025-04-17 15:03:21 -07:00
|
|
|
For example, `pyproject.toml` would need to contain the following, for a plugin named
|
|
|
|
|
`ocrmypdf-exampleplugin`:
|
2021-01-26 01:40:40 -08:00
|
|
|
|
2025-04-17 15:03:21 -07:00
|
|
|
```toml
|
|
|
|
|
[project]
|
|
|
|
|
name = "ocrmypdf-exampleplugin"
|
2021-01-26 01:40:40 -08:00
|
|
|
|
2025-04-17 15:03:21 -07:00
|
|
|
[project.entry-points."ocrmypdf"]
|
|
|
|
|
exampleplugin = "exampleplugin.pluginmodule"
|
|
|
|
|
```
|
2021-01-26 01:40:40 -08:00
|
|
|
|
2025-04-17 15:03:21 -07:00
|
|
|
## Plugin requirements
|
2020-07-21 23:53:30 -07:00
|
|
|
|
|
|
|
|
OCRmyPDF generally uses multiple worker processes. When a new worker is started,
|
|
|
|
|
Python will import all plugins again, including all plugins that were imported earlier.
|
|
|
|
|
This means that the global state of a plugin in one worker will not be shared with
|
|
|
|
|
other workers. As such, plugin hook implementations should be stateless, relying
|
|
|
|
|
only on their inputs. Hook implementations may use their input parameters to
|
|
|
|
|
to obtain a reference to shared state prepared by another hook implementation.
|
|
|
|
|
Plugins must expect that other instances of the plugin will be running
|
|
|
|
|
simultaneously.
|
|
|
|
|
|
2025-04-17 15:03:21 -07:00
|
|
|
The `context` object that is passed to many hooks can be used to share information
|
2020-07-21 23:53:30 -07:00
|
|
|
about a file being worked on. Plugins must write private, plugin-specific data to
|
2025-04-17 15:03:21 -07:00
|
|
|
a subfolder named `{options.work_folder}/ocrmypdf-plugin-name`. Plugins MAY
|
|
|
|
|
read and write files in `options.work_folder`, but should be aware that their
|
2020-07-21 23:53:30 -07:00
|
|
|
semantics are subject to change.
|
|
|
|
|
|
2025-04-17 15:03:21 -07:00
|
|
|
OCRmyPDF will delete `options.work_folder` when it has finished OCRing
|
|
|
|
|
a file, unless invoked with `--keep-temporary-files`.
|
2020-07-21 23:53:30 -07:00
|
|
|
|
|
|
|
|
The documentation for some plugin hooks contain a detailed description of the
|
|
|
|
|
execution context in which they will be called.
|
|
|
|
|
|
|
|
|
|
Plugins should be prepared to work whether executed in worker threads or worker
|
|
|
|
|
processes. Generally, OCRmyPDF uses processes, but has a semi-hidden threaded
|
|
|
|
|
argument that simplifies debugging.
|
|
|
|
|
|
2025-04-17 15:03:21 -07:00
|
|
|
## Plugin hooks
|
2020-05-07 03:53:37 -07:00
|
|
|
|
2020-07-21 23:53:30 -07:00
|
|
|
A plugin may provide the following hooks. Hooks must be decorated with
|
2025-04-17 15:03:21 -07:00
|
|
|
`ocrmypdf.hookimpl`, for example:
|
2020-05-07 03:53:37 -07:00
|
|
|
|
2025-04-17 15:03:21 -07:00
|
|
|
```python
|
|
|
|
|
from ocrmpydf import hookimpl
|
2019-06-20 02:44:29 -07:00
|
|
|
|
2025-04-17 15:03:21 -07:00
|
|
|
@hookimpl
|
|
|
|
|
def add_options(parser):
|
|
|
|
|
pass
|
|
|
|
|
```
|
2019-06-20 02:44:29 -07:00
|
|
|
|
2020-07-21 23:53:30 -07:00
|
|
|
The following is a complete list of hooks that are available, and when
|
2020-05-07 03:53:37 -07:00
|
|
|
they are called.
|
2019-06-20 02:44:29 -07:00
|
|
|
|
2025-04-17 15:03:21 -07:00
|
|
|
(firstresult)=
|
2020-07-16 00:01:59 -07:00
|
|
|
|
2020-07-21 23:53:30 -07:00
|
|
|
**Note on firstresult hooks**
|
2020-07-16 00:01:59 -07:00
|
|
|
|
|
|
|
|
If multiple plugins install implementations for this hook, they will be called in
|
|
|
|
|
the reverse of the order in which they are installed (i.e., last plugin wins).
|
|
|
|
|
When each hook implementation is called in order, the first implementation that
|
2025-04-17 15:03:21 -07:00
|
|
|
returns a value other than `None` will "win" and prevent execution of all other
|
2020-07-16 00:01:59 -07:00
|
|
|
hooks. As such, you cannot "chain" a series of plugin filters together in this
|
|
|
|
|
way. Instead, a single hook implementation should be responsible for any such
|
|
|
|
|
chaining operations.
|
|
|
|
|
|
2025-04-17 15:03:21 -07:00
|
|
|
## Examples
|
2022-02-20 00:56:43 -08:00
|
|
|
|
2025-04-17 15:03:21 -07:00
|
|
|
- OCRmyPDF's test suite contains several plugins that are used to simulate certain
|
2022-02-20 00:56:43 -08:00
|
|
|
test conditions.
|
2025-04-17 15:03:21 -07:00
|
|
|
- [ocrmypdf-papermerge](https://github.com/papermerge/OCRmyPDF_papermerge) is
|
2022-02-20 00:56:43 -08:00
|
|
|
a production plugin that integrates OCRmyPDF and the Papermerge document
|
|
|
|
|
management system.
|
|
|
|
|
|
2025-04-17 15:03:21 -07:00
|
|
|
### Suppressing or overriding other plugins
|
2022-02-20 00:56:43 -08:00
|
|
|
|
2025-04-17 15:03:21 -07:00
|
|
|
```{eval-rst}
|
2022-07-04 02:16:20 -07:00
|
|
|
.. autofunction:: ocrmypdf.pluginspec.initialize
|
2025-04-17 15:03:21 -07:00
|
|
|
```
|
2022-07-04 02:16:20 -07:00
|
|
|
|
2025-04-17 15:03:21 -07:00
|
|
|
### Custom command line arguments
|
2020-06-30 04:20:14 -07:00
|
|
|
|
2025-04-17 15:03:21 -07:00
|
|
|
```{eval-rst}
|
2020-06-30 04:20:14 -07:00
|
|
|
.. autofunction:: ocrmypdf.pluginspec.add_options
|
2025-04-17 15:03:21 -07:00
|
|
|
```
|
2020-06-30 04:20:14 -07:00
|
|
|
|
2025-04-17 15:03:21 -07:00
|
|
|
```{eval-rst}
|
2020-06-30 04:20:14 -07:00
|
|
|
.. autofunction:: ocrmypdf.pluginspec.check_options
|
2025-04-17 15:03:21 -07:00
|
|
|
```
|
2020-06-30 04:20:14 -07:00
|
|
|
|
2025-04-17 15:03:21 -07:00
|
|
|
### Execution and progress reporting
|
2021-01-30 20:42:00 -08:00
|
|
|
|
2025-04-17 15:03:21 -07:00
|
|
|
```{eval-rst}
|
2023-10-19 00:42:10 -07:00
|
|
|
.. autoclass:: ocrmypdf.pluginspec.ProgressBar
|
|
|
|
|
:members:
|
2023-11-05 00:10:51 -07:00
|
|
|
:special-members: __init__, __enter__, __exit__
|
2025-04-17 15:03:21 -07:00
|
|
|
```
|
2023-10-19 00:42:10 -07:00
|
|
|
|
2025-04-17 15:03:21 -07:00
|
|
|
```{eval-rst}
|
2022-07-04 02:16:20 -07:00
|
|
|
.. autoclass:: ocrmypdf.pluginspec.Executor
|
2021-01-30 20:42:00 -08:00
|
|
|
:members:
|
2023-11-05 00:10:51 -07:00
|
|
|
:special-members: __call__
|
2025-04-17 15:03:21 -07:00
|
|
|
```
|
2021-01-30 20:42:00 -08:00
|
|
|
|
2025-04-17 15:03:21 -07:00
|
|
|
```{eval-rst}
|
2021-01-31 02:21:03 -08:00
|
|
|
.. autofunction:: ocrmypdf.pluginspec.get_logging_console
|
2025-04-17 15:03:21 -07:00
|
|
|
```
|
2021-01-31 02:21:03 -08:00
|
|
|
|
2025-04-17 15:03:21 -07:00
|
|
|
```{eval-rst}
|
2021-01-30 20:42:00 -08:00
|
|
|
.. autofunction:: ocrmypdf.pluginspec.get_executor
|
2025-04-17 15:03:21 -07:00
|
|
|
```
|
2021-01-30 20:42:00 -08:00
|
|
|
|
2025-04-17 15:03:21 -07:00
|
|
|
```{eval-rst}
|
2021-01-31 02:46:44 -08:00
|
|
|
.. autofunction:: ocrmypdf.pluginspec.get_progressbar_class
|
2025-04-17 15:03:21 -07:00
|
|
|
```
|
2021-01-30 20:42:00 -08:00
|
|
|
|
2025-04-17 15:03:21 -07:00
|
|
|
### Applying special behavior before processing
|
2020-06-30 04:20:14 -07:00
|
|
|
|
2025-04-17 15:03:21 -07:00
|
|
|
```{eval-rst}
|
2020-06-30 04:20:14 -07:00
|
|
|
.. autofunction:: ocrmypdf.pluginspec.validate
|
2025-04-17 15:03:21 -07:00
|
|
|
```
|
2020-06-30 04:20:14 -07:00
|
|
|
|
2025-04-17 15:03:21 -07:00
|
|
|
### PDF page to image
|
2020-06-30 04:20:14 -07:00
|
|
|
|
2025-04-17 15:03:21 -07:00
|
|
|
```{eval-rst}
|
2020-06-30 04:20:14 -07:00
|
|
|
.. autofunction:: ocrmypdf.pluginspec.rasterize_pdf_page
|
2025-04-17 15:03:21 -07:00
|
|
|
```
|
2020-06-30 04:20:14 -07:00
|
|
|
|
2025-04-17 15:03:21 -07:00
|
|
|
### Modifying intermediate images
|
2020-06-30 04:20:14 -07:00
|
|
|
|
2025-04-17 15:03:21 -07:00
|
|
|
```{eval-rst}
|
2020-06-30 04:20:14 -07:00
|
|
|
.. autofunction:: ocrmypdf.pluginspec.filter_ocr_image
|
2025-04-17 15:03:21 -07:00
|
|
|
```
|
2020-06-30 04:20:14 -07:00
|
|
|
|
2025-04-17 15:03:21 -07:00
|
|
|
```{eval-rst}
|
2020-06-30 04:20:14 -07:00
|
|
|
.. autofunction:: ocrmypdf.pluginspec.filter_page_image
|
2025-04-17 15:03:21 -07:00
|
|
|
```
|
2020-06-30 04:20:14 -07:00
|
|
|
|
2025-04-17 15:03:21 -07:00
|
|
|
```{eval-rst}
|
2021-04-08 01:51:44 -07:00
|
|
|
.. autofunction:: ocrmypdf.pluginspec.filter_pdf_page
|
2025-04-17 15:03:21 -07:00
|
|
|
```
|
2021-04-08 01:51:44 -07:00
|
|
|
|
2025-04-17 15:03:21 -07:00
|
|
|
### OCR engine
|
2020-06-30 04:20:14 -07:00
|
|
|
|
2025-04-17 15:03:21 -07:00
|
|
|
```{eval-rst}
|
2020-06-30 04:20:14 -07:00
|
|
|
.. autofunction:: ocrmypdf.pluginspec.get_ocr_engine
|
2025-04-17 15:03:21 -07:00
|
|
|
```
|
2020-06-30 04:20:14 -07:00
|
|
|
|
2025-04-17 15:03:21 -07:00
|
|
|
```{eval-rst}
|
2020-06-30 04:20:14 -07:00
|
|
|
.. autoclass:: ocrmypdf.pluginspec.OcrEngine
|
2020-05-07 03:53:37 -07:00
|
|
|
:members:
|
2020-06-30 04:20:14 -07:00
|
|
|
|
|
|
|
|
.. automethod:: __str__
|
2025-04-17 15:03:21 -07:00
|
|
|
```
|
2020-06-30 04:20:14 -07:00
|
|
|
|
2025-04-17 15:03:21 -07:00
|
|
|
```{eval-rst}
|
2020-06-30 04:20:14 -07:00
|
|
|
.. autoclass:: ocrmypdf.pluginspec.OrientationConfidence
|
2025-04-17 15:03:21 -07:00
|
|
|
```
|
2020-06-30 04:20:14 -07:00
|
|
|
|
2025-04-17 15:03:21 -07:00
|
|
|
### PDF/A production
|
2020-06-30 04:20:14 -07:00
|
|
|
|
2025-04-17 15:03:21 -07:00
|
|
|
```{eval-rst}
|
2020-06-30 04:20:14 -07:00
|
|
|
.. autofunction:: ocrmypdf.pluginspec.generate_pdfa
|
2025-04-17 15:03:21 -07:00
|
|
|
```
|
2022-07-04 02:16:20 -07:00
|
|
|
|
2025-04-17 15:03:21 -07:00
|
|
|
### PDF optimization
|
2022-07-04 02:16:20 -07:00
|
|
|
|
2025-04-17 15:03:21 -07:00
|
|
|
```{eval-rst}
|
2022-07-04 02:16:20 -07:00
|
|
|
.. autofunction:: ocrmypdf.pluginspec.optimize_pdf
|
2025-04-17 15:03:21 -07:00
|
|
|
```
|
2022-07-04 02:16:20 -07:00
|
|
|
|
2025-04-17 15:03:21 -07:00
|
|
|
```{eval-rst}
|
|
|
|
|
.. autofunction:: ocrmypdf.pluginspec.is_optimization_enabled
|
|
|
|
|
```
|