<?php
namespace App\Console\Commands;
use Illuminate\Console\Command;
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;
class ConvertDocument extends Command
{
protected $signature = 'docling:convert {inputFile} {outputFile}';
protected $description = 'Converts a document using docling';
public function handle()
{
$inputFile = $this->argument('inputFile');
$outputFile = $this->argument('outputFile');
$doclingPath = '/Users/awidarto/docling-venv/bin/docling';
// IMPORTANT: Always use absolute paths for input and output files
// when calling from a non-interactive environment.
$absoluteInputPath = storage_path('app/' . $inputFile);
$absoluteOutputPath = storage_path('app/' . $outputFile);
if (!file_exists($absoluteInputPath)) {
$this->error("Input file not found: {$absoluteInputPath}");
return 1;
}
$this->info("Starting conversion of {$inputFile} to {$outputFile}...");
$process = new Process([
$doclingPath,
'convert',
$absoluteInputPath,
$absoluteOutputPath
]);
// Set a longer timeout if you are converting large files
$process->setTimeout(300);
try {
$process->mustRun();
$this->info("Conversion successful!");
$this->info("Output file: {$absoluteOutputPath}");
$this->line($process->getOutput());
} catch (ProcessFailedException $exception) {
$this->error('The document conversion failed.');
$this->error($exception->getMessage());
return 1;
}
return 0;
}
}