FPDF 错误:图像文件丢失或不正确

发布于 2024-09-07 20:15:09 字数 773 浏览 1 评论 0原文

我正在使用 FPDF 库通过 PHP 生成 PDF 文件。该库可以很好地处理纯文本(即为任何文本生成 PDF 文件),但是在尝试添加时出现 FPDF 错误:缺少或不正确的图像文件:{MY_FILE_PATH} 错误将图像添加到我的 PDF 页面。通过浏览器访问该文件路径,则相应的图像显示正常。

我的代码是:

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 文件,即使也包含图像,但在移动到服务器后这不起作用。

我尝试过以下可能性:

  1. 使用图像的绝对和相对路径。

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

  3. 所有图像格式,如 jpg、png 和 gif。

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

这些案例都不适合我,困扰着这个问题,任何人都可以帮助我解决这个问题。

谢谢!

西瓦...

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

她说她爱他 2024-09-14 20:15:09

经过长时间的斗争,我终于找到了这个问题的原因,我已经讨论过 此处

主要这个问题是由于我的服务器未启用“allow_url_fopen”设置,我已经通过使用 CURL 解决了这个问题。我给出了解决此问题的分步过程,因为这对于像我这样的人来说可能有用,以避免浪费时间(寻找解决方案)。

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.

我认为这是解决这个问题的一种方法,如果有人知道其他方法,那么你可以将它们发布在这里,这样这个问题可能对某人有用。

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.

南冥有猫 2024-09-14 20:15:09

只需在 php.ini 上添加:

allow_url_fopen = ON

(如果不存在则创建新的)即可解决问题。

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

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/

蒲公英的约定 2024-09-14 20:15:09

任何人都可以使用此代码来解决。也许它会起作用。因为有时某些情况可能会有所不同。就我而言,我遇到了同样的问题,但在使用它之后。我解决了我的问题..

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

尝试为 $pdf->Image() 函数的 6 个参数提供空字符串,或者您可以提供文件扩展名,例如(PNG 或 JPG)。就我而言,空字符串''正在工作。因为我正在裁剪图像,所以图像有时会出错。所以之后我的问题就解决了,我用不同的图像进行了测试。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文