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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3177716/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:56:04  来源:igfitidea点击:

FPDF error: Missing or incorrect image file

phpfpdf

提问by Siva

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.

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

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.

这段代码在我的本地主机上运行良好,即使有图像也生成相应的 PDF 文件,但是在移动到服务器后这不起作用。

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.

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

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

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

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

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

这些案例都不适合我,被这个问题困住了,谁能帮我解决这个问题。

Thanks!

谢谢!

Siva ...

湿婆...

回答by Siva

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

这个问题主要是由于我的服务器没有启用“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.

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.

我认为这是解决这个问题的一种方法,如果有人知道其他一些方法,那么您可以将它们张贴在这里,以便这个问题对某人有用。

回答by user1638334

just add:

只需添加:

allow_url_fopen = ON

allow_url_fopen = ON

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

在 php.ini (如果不存在则创建新的)解决了这个问题。

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

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