ilovepdf/ilovepdf-php

Error: "Content-Disposition" Header Missing in API Response

TheyCallMeSticky opened this issue · 2 comments

Description
We are experiencing an issue where the "Content-Disposition" header is missing from the API response, which is critical for our file download functionality. This problem has recently surfaced and is affecting our web application's ability to process documents through your API. We use the API for splitting files, converting images to PDFs, merging documents, and compressing files.

Steps to Reproduce

  1. Send a request to the API to process a document (e.g., split, merge, compress, or ImageToPdf conversion).
  2. Attempt to download the processed file using the provided endpoint.
  3. The API response lacks the "Content-Disposition" header, causing our file download logic to fail.

Expected Behavior
The API response should include a "Content-Disposition" header with the filename, allowing our application to correctly handle the file download.

Actual Behavior
The "Content-Disposition" header is missing from the API response, leading to a failure in our download logic. The issue is identified in the following code snippet from our src/Task.php at line 323:

private function downloadFile($task)
{
    $data = array('v'=> self::VERSION);
    $body = Body::Form($data);
    $response = parent::sendRequest('get', 'download/' . $task, $body);

    if(preg_match("/filename\*\=utf-8\'\'([\W\w]+)/", $response->headers['Content-Disposition'], $matchesUtf)){
        $filename = urldecode(str_replace('"', '', $matchesUtf[1]));
    }
    else {
        preg_match('/ .*filename=\"([\W\w]+)\"/', $response->headers['Content-Disposition'], $matches);
        $filename = str_replace('"', '', $matches[1]);
    }

    $this->outputFile = $response->raw_body;
    $this->outputFileName = $filename;
    $this->outputFileType = pathinfo($this->outputFileName, PATHINFO_EXTENSION);
}

Code Snippet (For Reference)
Here is a snippet from our implementation attempting to download and process the file:

public function imageToPdf($path, $file): array
{
    $return = ['result' => false, 'msg' => null, 'outputFileName' => null];

    for ($attempt = 1; $attempt <= $this->maxRetry; $attempt++) {
        try {
            $myTask = new ImagepdfTask($this->api_key, $this->secret_key);
            $newFileName = pathinfo($file, PATHINFO_FILENAME) . '.pdf';
            $myTask->addFile($path . $file);
            $myTask->setPagesize('A4');
            $myTask->setOutputFilename($newFileName);
            $myTask->execute();
            $myTask->download($path);

            $newFileName = $myTask->outputFileName;
            if (!file_exists($path . $newFileName)) {
                $return['msg'] = 'Nouveau fichier introuvable';
            } else {
                $return['result'] = true;
                $return['outputFileName'] = $newFileName;
            }
            return $return;

        } catch (Exception$e) {
            $return['msg'] .= 'An error occurred: ' . $e->getMessage();

            if ($attempt == $this->maxRetry) {
                return $return;
            }
        }
    }
    return $return;
}

Attempts to Resolve

  • Implemented an API call queue with delays between calls.
  • Checked for any changes in API documentation regarding headers.

Additional Context
This issue seems to occur randomly and has significantly impacted our ability to serve our users effectively. Any insights or advice on how to address this problem would be greatly appreciated.

Added check on some cases where lowercase may be sent