2
votes

I am using the FPDF library for generating PDF files by PHP. This library is working fine with plain text(i.e PDF files are generating for any text), but this is giving me an error of FPDF error: Missing or incorrect image file:{MY_FILE_PATH} while trying to add an image to my PDF page. Accessed that file path through the browser, then the corresponding image is appearing fine.

My code is:

require('fpdf.php');

$pdf = new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial', 'B', 16);
$pdf->Write(10, 'Example image');
$image_path = MY_FILE_PATH; // This variable contains my actual file path 
$pdf->Image($image_path, 10, 10, 350, 450);
$pdf->Output();

This code is working perfectly in my localhost and generating the corresponding PDF files even with the images also, but this is not working after moving to the server.

I have tried with these possibilities like:

  1. With absolute and relative paths for the image.

  2. Tried with a local image placed in the same folder.

  3. All image formats like jpg, png and gif also.

  4. Checked the permissions for the image and corresponding folders also.

None of these cases are working for me, stuck with this problem, can anyone help me to slove this issue.

Thanks!

Siva ...

3
Can you show an example path you are using? - Pekka
you can take the path desicomments.com/user/2008/03/7139/good.jpg as an example. Can we use relative paths like '/test.jpg' also with FPDF? - Siva
Pekka, thanks for your response, got the reason for this issue and fixed that one. - Siva

3 Answers

5
votes

After a long struggle finally I found the reason for this issue which I've already discussed here.

Mainly this problem is due to the 'allow_url_fopen' setting which is not enabled for my server, I've solved this issue by using the CURL. I'm giving the step by step procedure to solve this issue because this may useful for somebody like me to avoid wastage of time(for finding the solution).

1. Create a temporary file in the local system with write permission.

Ex: $fp = @fopen($local_file_path, 'x'); 
    fclose($fp);

2. Get the remote location file contents using CURL and write that to local file 
which is created in the previous step.

Ex: $curl = new CurlWrapper($remote_file_path);
    $curl->copyToFile($local_file_path);

3. Now send this local file path to FPDF function(image) then the corresponding 
PDF will get generate for the file without any issues.

I think this is one method to solve this issue, if anybody knows some other ways then you can post them here so that this question may be useful for somebody.

2
votes

just add:

allow_url_fopen = ON

on php.ini (create new if not exists) solves the problem.

http://www.solo-technology.com/blog/2010/04/07/quick-fix-for-url-file-access-is-disabled-issues/

0
votes

anyone can use this code for solve. may be it will work. because sometime some case may be different. in my case i was getting same issue but after use it. i fixed my issue..

$pdf->Image($image_path, 10, 10, 350, 450 ,''); 

try to give empty string to 6 parameter of $pdf->Image() function or you can give file extension like ( PNG or JPG). in my case empty string '' was working. because i was cropping image so that image going some time wrong. so after do that my problem gone fixed and i tested with different images.