You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Laravel 9中carlos-meneses/laravel-mpdf包运行报错求助

解决Laravel 9中carlos-meneses/laravel-mpdf的Logger兼容报错

报错信息

Declaration of Mpdf\Mpdf::setLogger(Psr\Log\LoggerInterface $logger) must be compatible with Psr\Log\LoggerAwareInterface::setLogger(Psr\Log\LoggerInterface $logger): void

问题原因

该错误源于mpdf/mpdf与项目中psr/log版本不兼容:psr/log 3.0+规范要求setLogger方法必须声明返回void,但旧版mpdf未遵循此规范,导致接口实现不匹配。

解决方案

1. 降级psr/log到兼容版本

不要手动修改composer.lock,直接在composer.json的require段添加兼容版本约束:

"psr/log": "^1.1 || ^2.0"

执行命令更新依赖:

composer update psr/log --with-dependencies

2. 更新mpdf到适配版本

若降级psr/log无效,可指定适配psr/log 3.0的mpdf版本,在composer.json的require段添加:

"mpdf/mpdf": "^8.1"

执行全量更新:

composer update

3. 清理缓存确保生效

php artisan cache:clear
php artisan config:clear

用户提供的composer.json配置

{
    "name": "laravel/laravel",
    "type": "project",
    "description": "The Laravel Framework.",
    "keywords": ["framework", "laravel"],
    "license": "MIT",
    "require": {
        "php": "^8.0.2",
        "anhskohbo/no-captcha": "^3.4",
        "carlos-meneses/laravel-mpdf": "^2.1",
        "guzzlehttp/guzzle": "^7.2",
        "intervention/image": "^2.7",
        "laravel/framework": "^9.19",
        "laravel/sanctum": "^2.14.1",
        "laravel/tinker": "^2.7",
        "mcamara/laravel-localization": "^1.7",
        "spatie/laravel-permission": "^5.5"
    },
    "require-dev": {
        "fakerphp/faker": "^1.9.1",
        "laravel/breeze": "^1.11",
        "laravel/sail": "^1.0.1",
        "mockery/mockery": "^1.4.4",
        "nunomaduro/collision": "^6.1",
        "phpunit/phpunit": "^9.5.10",
        "spatie/laravel-ignition": "^1.0"
    },
    "autoload": {
        "psr-4": {
            "App\\": "app/",
            "Database\\Factories\\": "database/factories/",
            "Database\\Seeders\\": "database/seeders/"
        },
        "files": [
            "app/Helpers/helper.php"
        ]
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },
    "scripts": {
        "post-autoload-dump": [
            "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
            "@php artisan package:discover --ansi"
        ],
        "post-update-cmd": [
            "@php artisan vendor:publish --tag=laravel-assets --ansi --force"
        ],
        "post-root-package-install": [
            "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "@php artisan key:generate --ansi"
        ]
    },
    "extra": {
        "laravel": {
            "dont-discover": []
        }
    }
}

内容的提问来源于stack exchange,提问作者Shady Hesham

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.10 22:20:46