FPDF错误:图像文件丢失或不正确 [英] FPDF error: Missing or incorrect image file

查看:105
本文介绍了FPDF错误:图像文件丢失或不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用FPDF库通过PHP生成PDF文件.该库可以正常处理纯文本(即为任何文本生成PDF文件),但是在尝试将图像添加到我的PDF页面时,这给了我FPDF error: Missing or incorrect image file:{MY_FILE_PATH}的错误.通过浏览器访问该文件路径,然后相应的图像看起来很好.

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.

我的代码是:


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();

此代码在我的本地主机中运行正常,即使生成图像也生成了相应的PDF文件,但是在移至服务器后却无法正常工作.

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.

我尝试了以下可能性:

  1. 具有图像的绝对路径和相对路径.

  1. With absolute and relative paths for the image.

尝试将本地图像放置在同一文件夹中.

Tried with a local image placed in the same folder.

所有图像格式,例如jpg,png和gif.

All image formats like jpg, png and gif also.

还检查了图像和相应文件夹的权限.

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.

谢谢!

Siva ...

推荐答案

经过长时间的努力,我终于找到了这个问题的原因,我已经讨论过

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

主要是由于我的服务器未启用'allow_url_fopen'设置引起的,我已经通过使用CURL解决了此问题.我正在逐步解决此问题,因为这对于像我这样的人避免浪费时间(用于寻找解决方案)可能很有用.

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.

这篇关于FPDF错误:图像文件丢失或不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆