Skip to content

[Bug]: PDF Date Fields Can Be Arrays, Causing TypeError in PdfMeta::make() #64

@international-omelette

Description

@international-omelette

What happened?

The PdfMeta::make() method in src/Models/PdfMeta.php throws a TypeError when PDF documents contain date fields (CreationDate, ModDate) as arrays instead of strings.

Root Cause

Lines 37-43 in PdfMeta.php assume date fields are always strings:

if ($creationDate) {
    $creationDate = new DateTime($creationDate);
}

if ($modDate) {
    $modDate = new DateTime($modDate);
}

However, the underlying smalot/pdfparser library can return arrays when PDF metadata contains array structures (which is valid per PDF specification).

Error Message

TypeError: DateTime::__construct(): Argument #1 ($datetime) must be of type string, array given

Proposed Fix

Add type checking before DateTime construction:

if ($creationDate) {
    if (is_array($creationDate)) {
        // Handle array case - use first valid date
        $validDate = array_filter($creationDate, function($date) {
            return !empty($date) && is_string($date);
        });
        $creationDate = !empty($validDate) ? new DateTime(reset($validDate)) : null;
    } else {
        $creationDate = new DateTime($creationDate);
    }
}

if ($modDate) {
    if (is_array($modDate)) {
        // Handle array case - use last valid date for modification date
        $validDate = array_filter($modDate, function($date) {
            return !empty($date) && is_string($date);
        });
        $modDate = !empty($validDate) ? new DateTime(end($validDate)) : null;
    } else {
        $modDate = new DateTime($modDate);
    }
}

Impact

  • Affects PDF metadata extraction for certain PDF files
  • Causes complete extraction failure rather than graceful degradation

How to reproduce the bug

This occurs with PDF files that have array-structured metadata fields

Package Version

latest

PHP Version

8.x

Which operating systems does with happen with?

No response

Notes

No response

Metadata

Metadata

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions