Skip to main content

Integrating docling with Laravel Artisan

This guide explains how to call the docling command-line tool from a Laravel Artisan command. This is useful for automating document conversion tasks within your web application.

Key Concepts

When running an Artisan command, the PHP process executes in a non-interactive shell. This means it doesn’t have the same environment as your user’s terminal session. Therefore, you cannot simply source the virtual environment’s activation script. Instead, you must use the absolute path to the docling executable within the virtual environment.

Steps for Integration

  1. Locate the docling Executable: The absolute path to the docling executable in the virtual environment we created is:
  2. Create a New Artisan Command: You can create a new Artisan command using the following command:
  3. Implement the Command: Open the newly created app/Console/Commands/ConvertDocument.php file and modify it to use the Process component to execute the docling command. Here is a complete example:
  4. File Permissions: The user that runs your web server (e.g., www-data, nginx, apache) needs to have execute permissions on the docling executable and read/write permissions on the directories where your input and output files are stored. You can ensure the executable has the correct permissions with:
    You will also need to ensure that your storage/app directory (or wherever you are storing your files) is writable by the web server user.

Usage

Once you have created the Artisan command, you can run it from your terminal like any other command:
This command will convert the my_document.pdf file located in storage/app to my_converted_document.docx in the same directory.