yii2tech / html2pdf

Yii2 component for HTML to PDF conversion
Other
78 stars 65 forks source link

Mpdf\MpdfException: Temporary files directory is not writable #12

Closed antichris closed 6 years ago

antichris commented 6 years ago

When calling yii2tech\html2pdf\Manager::convert() with mPDF as the converter, the following exception is thrown:

Mpdf\MpdfException: Temporary files directory "/my-project/vendor/mpdf/mpdf/src/Config/../../tmp" is not writable in /my-project/vendor/mpdf/mpdf/src/Cache.php:17
Stack trace:
#0 /my-project/vendor/mpdf/mpdf/src/Mpdf.php(983): Mpdf\Cache->__construct('/my-project/ven...')
#1 /my-project/vendor/yii2tech/html2pdf/src/converters/Mpdf.php(42): Mpdf\Mpdf->__construct(Array)
#2 /my-project/vendor/yii2tech/html2pdf/src/BaseConverter.php(33): yii2tech\html2pdf\converters\Mpdf->convertInternal('<!DOCTYPE html>...', '/my-project/run...', Array)
#3 /my-project/vendor/yii2tech/html2pdf/src/Manager.php(224): yii2tech\html2pdf\BaseConverter->convert('<!DOCTYPE html>...', '/my-project/run...', Array)
#4 /my-project/controllers/SiteController.php(23): yii2tech\html2pdf\Manager->convert('<!DOCTYPE html>...')
#5 [internal function]: app\controllers\backend\SiteController->actionPdf()
#6 /my-project/vendor/yiisoft/yii2/base/InlineAction.php(57): call_user_func_array(Array, Array)
#7 /my-project/vendor/yiisoft/yii2/base/Controller.php(157): yii\base\InlineAction->runWithParams(Array)
#8 /my-project/vendor/yiisoft/yii2/base/Module.php(528): yii\base\Controller->runAction('pdf', Array)
#9 /my-project/vendor/yiisoft/yii2/web/Application.php(103): yii\base\Module->runAction('backend/site/pd...', Array)
#10 /my-project/vendor/yiisoft/yii2/base/Application.php(386): yii\web\Application->handleRequest(Object(yii\web\Request))
#11 /my-project/web/index.php(31): yii\base\Application->run()
#12 {main}

This happens because the constructor of the main mPDF object requires tempDir to be already properly configured when instantiating its Mpdf\Cache in Mpdf\Mpdf::__construct() (line 983 in v7.1.0):

$this->cache = new Cache($config['tempDir']);

the default value for which comes from Mpdf\Config\ConfigVariables::$defaults:

$this->defaults = [
    ...
    'tempDir' => __DIR__ . '/../../tmp',
    ...
];

but your yii2tech\html2pdf\converters\Mpdf::convertInternal() does not set it to a valid value when instantiating mPDF:

$pdf = new \Mpdf\Mpdf([
    'mode' => $charset,
    'format' => $pageSize,
]);

so that even setting tempDir properly in yii2tech\html2pdf\converters\Mpdf::$defaultOptions has no effect.

Get cracking! :wink:

EDIT:

Component Version
html2pdf 1.0.3
mPDF 7.1.0
Yii 2.0.15.1
Composer 1.6.5
PHP 7.0.30
Operating system Ubuntu 16.04.4 LTS
klimov-paul commented 6 years ago

Resolved by commit ea434421b78485335a618778dc58caae48786ee8