简体   繁体   English

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

[英]FPDF error: Missing or incorrect image file

I am using the FPDF library for generating PDF files by PHP.我正在使用 FPDF 库通过 PHP 生成 PDF 文件。 This library is working fine with plain text(ie 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.这个库可以很好地处理纯文本(即 PDF 文件正在为任何文本生成),但这给我一个FPDF error: Missing or incorrect image file:{MY_FILE_PATH}同时尝试将图像添加到我的 PDF 页面。 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.这段代码在我的本地主机上运行良好,即使有图像也生成相应的 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.所有图像格式,如 jpg、png 和 gif 也是。

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

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.这个问题主要是由于我的服务器没有启用“allow_url_fopen”设置,我已经通过使用 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.我认为这是解决这个问题的一种方法,如果有人知道其他一些方法,那么您可以将它们张贴在这里,以便这个问题对某人有用。

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/

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).尝试为$pdf->Image()函数的 6 个参数提供空字符串,或者您可以提供文件扩展名(PNG 或 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.所以这样做之后我的问题就解决了,我用不同的图像进行了测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM