Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

php - FPDF error: Missing or incorrect image file

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 ...

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...