ilovepdf / ilovepdf-php

iLovePDF Rest Api - PHP Library (https://developer.ilovepdf.com)
147 stars 39 forks source link

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

Closed TheyCallMeSticky closed 4 months ago

TheyCallMeSticky commented 7 months ago

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

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.

maztch commented 4 months ago

Added check on some cases where lowercase may be sent

mdawart commented 2 months ago

I still experience this problem. Did you change anything in the code or just on server-side?


[
  "exception" => ErrorException {
    #message: "Notice: Undefined index: Content-Disposition"
    #code: 0
    #file: "./vendor/ilovepdf/ilovepdf-php/src/Task.php"
    #line: 295
    #severity: E_NOTICE
    trace: {
      ./vendor/ilovepdf/ilovepdf-php/src/Task.php:295 { …}
      ./vendor/ilovepdf/ilovepdf-php/src/Task.php:235 { …}
      ./src/Service/ILovePdfHandler.php:39 {
        App\Service\ILovePdfHandler::compress(Email $email): array^
        › mkdir($compressedPath);
        › $myTaskCompress->download($path . 'compressed/');
        › EmailHandler::unpackFile('output.zip', $compressedPath);
      }```

(we are using an older version of the library)