Skip to content
ComPDF
DemoFAQ
New Release

Open-Source PDF SDK & AI Document Processing

Get the full self-hosted SDK and AI document processing on GitHub. One-click deploy to quickly build your document workflows.

Cancel Conversion Task

Assign a callable to $onCancel on the ConvertCallback to abort a long-running task. The SDK polls the callback periodically; returning true causes the conversion to stop and the corresponding Conversion::pdfTo*() call to return ErrorCode::CANCEL (value 1).

php
use ComPDFKit\Conversion\Conversion;
use ComPDFKit\Conversion\ConvertCallback;
use ComPDFKit\Conversion\ConvertOption;
use ComPDFKit\Conversion\ErrorCode;

$cb = new ConvertCallback();
$pagesDone = 0;

$cb->onProgress = static function (int $current, int $total) use (&$pagesDone): void {
    $pagesDone = $current;
};

$cb->onCancel = static function () use (&$pagesDone): bool {
    return $pagesDone >= 1; // stop after the first page
};

$option = new ConvertOption();
$code = Conversion::pdfToWord('input.pdf', '', 'output.docx', $option, $cb);

echo ErrorCode::describe($code) . PHP_EOL; // "Cancel"