mPDF temporary file is not writable using Yii

不羁岁月 提交于 2020-05-13 06:27:57

问题


I tried to print a certificate in PDF but when I push my code to staging, it said

Temporary files directory "/var/www/protected/vendor/mpdf/mpdf/src/Config/../../tmp" is not writable

I'm not sure how to change the permission and how to change the custom directory.

Here's my code of a button to click to get the certificate:

 <a class="btn btn-sd btn-sd-ghost btn-sd-ghost-black margin-right-lg" href="<?php echo $this->createUrl('//idea/frontend/pdf', array('id'=>$model->id))?>" target="_blank">Get Your Certificate<i class="icon-right-small"></i></a> 
            <?php endif; ?>

and this is the controller:

public function actionPdf($id){
        $model = HUB::getOrganization($id);
        $orgtitle = $model->title;

        $mpdf = new \Mpdf\Mpdf(['mode' => 'utf-8', 'format' => 'A4-L']);
        $mpdf->WriteHTML("<html><body style='background-image:url(\"/images/cert-idea.jpg\"); background-image-resize: 5; background-position: top center;'></body></html>");

        $mpdf->WriteHTML("<div style='text-align:center; display:table; height:100%; width:100%; padding-top:28%;'><h1 style='display:table-cell; vertical-align:middle; font-size:40px;'>".$orgtitle."</h1></div>");


        $mpdf->Output('IDEA-CERT-'.$orgtitle.'.pdf', 'I');
    }

Hope someone can help on my issue. Thank you!


回答1:


Try a custom temporary directory as stated in the documentation:

It is recommended to set custom temporary directory via tempDir configuration key. The directory must have write permissions (mode 775 is recommended).

<?php
$mpdf = new \Mpdf\Mpdf(['tempDir' => __DIR__ . '/custom/temp/dir/path']);

You will have far more control over permissions of a directory outside composer vendor-dir.

Mode 775 may not be sufficient if a web-server user, typically www-data has to access the directory. Use 777 if necessary.

Be warned that mPDF auto-cleans its temporary directory, so use one dedicated only to mPDF.




回答2:


It is essential to provide writable temporary directory. Best solution is to use OS provided temporary space.

$mpdf = new \Mpdf\Mpdf(['tempDir' => sys_get_temp_dir().DIRECTORY_SEPARATOR.'mpdf']);



回答3:


You can change the file permission with:
chmod 777 /var/www/protected/vendor/mpdf/mpdf/tmp
but that would allow anyone on that computer any type of access to that file, so probably not a good idea. It would give you a starting point though, if this works you know that the problem is file permissions.

You may need to run it as super-user if you are not the owner of that file

A better solution would be to change the owner to be the process that the server runs on,
chown www-data: /var/www/protected/vendor/mpdf/mpdf/tmp
changing www-data for the process that will save the file.

It is strange that is trying to save the pdf to that directory though, are you using Kartik's mPDF? The default configuration is to send the file inline to the browser, it should only be trying to save the file if you changed the configuration, either globally or when you create the widget, to:

'destination' => ['Pdf::DEST_FILE', '../../tmp']

If that is the case then the best would be to configure that to whichever option you need, probably Pdf::DEST_BROWSER (default) or Pdf::DEST_DOWNLOAD to force a download without prompting the user.

Saving the pdf as a file inside the directory where the application is doesn't really make sense unless you only ever plan to use your developing computer as a client, there is no way to tell what other client's folder structures will look like so it is much better to let their browsers decide what to do with the file.




回答4:


Try this for (XAMPP / LAMPP Ubuntu)

  • open terminal then set as super user (su)
  • copy and paste bellow from sudo-tmp
sudo chmod 777 -R /opt/lampp/htdocs/phpdasar/pertemuan21/vendor/mpdf/mpdf/tmp
sudo chown -R sahrulsidik /opt/lampp/htdocs/phpdasar/pertemuan21/vendor/mpdf/mpdf/tmp


来源:https://stackoverflow.com/questions/48554488/mpdf-temporary-file-is-not-writable-using-yii

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!