mPDF Error: Doesn´t work on Server

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
NovicePHP
New php-forum User
New php-forum User
Posts: 4
Joined: Mon Mar 19, 2018 12:12 pm

Mon Mar 19, 2018 12:15 pm

I am using mPDF to create a pdf document and to save it. It works fine on my localhost but when I try it on the server it is not creating (Saving) the pdf document. I came to know that I don´t have enough permissions (777) to the project folder. I tried to change the permissions by right-clicking the folder >Properties> Security. It doesn´t work to change the permissions for my user.

I have full access to only on my c: and so am thinking to save the pdf document on my desktop like C:/users/Novice/pdf

Here´s my code

Code: Select all

include_once("sample_gen_pdf/mpdf.php"); 

        $mpdf=new mPDF( );  

        $mpdf->SetDisplayMode('fullpage'); 

        $mpdf->list_indent_first_level = 0;  

        $mpdf->WriteHTML($html,2); 

        $mpdf->Output(__DIR__ . '/pdf/'.$filename.'.pdf', 'F');  
It works fine on the localhost but not on the server and so I need to save the pdf on my desktop. Any help on my code is much more appreciated.

When I execute the above code on the server it´s returning me an error as

mPDF Error: Unable to create output file: /project/pdf/filename

User avatar
phpRob
New php-forum User
New php-forum User
Posts: 65
Joined: Mon Feb 26, 2018 7:15 am

Mon Mar 19, 2018 12:34 pm

One thing is to be sure to not get tripped up on paths on the server vs on your local machine. The permissions errors could have you distracted from a path issue. Needed paths could be different. Relative paths are best, but you must make them work both on server and on local machine at least this has been an issue I've had to deal with.

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 713
Joined: Mon Feb 22, 2016 5:52 pm

Mon Mar 19, 2018 1:18 pm

Have you checked that /project/pdf is a folder on your server?

Is your server linux?

PHP will have write permissions on your server.

Try this (untested):

Code: Select all


$root = $_SERVER['DOCUMENT_ROOT'];

include_once("sample_gen_pdf/mpdf.php"); 
$mpdf=new mPDF( );  
$mpdf->SetDisplayMode('fullpage'); 
$mpdf->list_indent_first_level = 0;  
$mpdf->WriteHTML($html,2); 
$mpdf->Output($root . $filename. '.pdf', 'F');
This should save the file in the same directory as your web page index; so if your web site is example.com - the pdf file will be in the same place as your web sites index page.

As Rob said, the most likely cause is where you are trying to save the file.

NovicePHP
New php-forum User
New php-forum User
Posts: 4
Joined: Mon Mar 19, 2018 12:12 pm

Tue Mar 20, 2018 12:51 pm

hyper wrote:
Mon Mar 19, 2018 1:18 pm
Have you checked that /project/pdf is a folder on your server?

Is your server linux?

PHP will have write permissions on your server.

Try this (untested):

Code: Select all


$root = $_SERVER['DOCUMENT_ROOT'];

include_once("sample_gen_pdf/mpdf.php"); 
$mpdf=new mPDF( );  
$mpdf->SetDisplayMode('fullpage'); 
$mpdf->list_indent_first_level = 0;  
$mpdf->WriteHTML($html,2); 
$mpdf->Output($root . $filename. '.pdf', 'F');
This should save the file in the same directory as your web page index; so if your web site is example.com - the pdf file will be in the same place as your web sites index page.

As Rob said, the most likely cause is where you are trying to save the file.
Yes, the git server is linux

I tried with your code and it is able to create the pdf document on my localhost but not on the server. It is showing empty files in the folder.

I guess I need to have permissions of the folder to be changed by the server.. Isn´t it?

Is there any other thing that I missed? Please help me out

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 713
Joined: Mon Feb 22, 2016 5:52 pm

Tue Mar 20, 2018 5:10 pm

O.K., create the following code in a new file and show us the result:

Code: Select all

<?php

$root = $_SERVER['DOCUMENT_ROOT'];
echo sprintf('%o', fileperms($root));
What it does is echo out the file permissions in octal which will show us what the file permissions are, you said that the last script created a blank document which would point to the rest of the script and not the file permissions, so I am trying to establish if it is a permission problem or not: do not change any file permissions, they should be 644 if anything, certainly not 777.

NovicePHP
New php-forum User
New php-forum User
Posts: 4
Joined: Mon Mar 19, 2018 12:12 pm

Wed Mar 21, 2018 1:11 am

hyper wrote:
Tue Mar 20, 2018 5:10 pm
O.K., create the following code in a new file and show us the result:

Code: Select all

<?php

$root = $_SERVER['DOCUMENT_ROOT'];
echo sprintf('%o', fileperms($root));
What it does is echo out the file permissions in octal which will show us what the file permissions are, you said that the last script created a blank document which would point to the rest of the script and not the file permissions, so I am trying to establish if it is a permission problem or not: do not change any file permissions, they should be 644 if anything, certainly not 777.
When I execute your code I got the result as

40775

When I execute git ls-files --stage am getting 100644

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 713
Joined: Mon Feb 22, 2016 5:52 pm

Wed Mar 21, 2018 10:00 am

You have write permissions.

Have you checked that $html is a correct page and displays?

Have you tried it without these?

Code: Select all

$mpdf->SetDisplayMode('fullpage'); 

$mpdf->list_indent_first_level = 0; 

NovicePHP
New php-forum User
New php-forum User
Posts: 4
Joined: Mon Mar 19, 2018 12:12 pm

Thu Mar 22, 2018 5:38 am

hyper wrote:
Wed Mar 21, 2018 10:00 am
You have write permissions.

Have you checked that $html is a correct page and displays?

Have you tried it without these?

Code: Select all

$mpdf->SetDisplayMode('fullpage'); 

$mpdf->list_indent_first_level = 0; 
I tried it and the browser is able to create the pdf document.

I modified my code as

Code: Select all

  $mpdf=new mPDF( ); 
    $mpdf->SetDisplayMode('fullpage');

    $mpdf->list_indent_first_level = 0;	

    $mpdf->WriteHTML($html,2);

    $mpdf->Output($filename.'.pdf', 'D'); 
It is trying to download the pdf document in the downloads folder but when I change the above code with 'F' then it is not working..

The pdf is only saving in the downloads but not in the path which I have given. I need it to be saved in my path directly rather than in the downloads folder

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 713
Joined: Mon Feb 22, 2016 5:52 pm

Thu Mar 22, 2018 12:26 pm

I don't know mpdf, however, If you are downloading a file from a browser, the location has nothing to do with your PHP script, it has everything to do with your browser. You can probably change the download location in your browser.

Post Reply