FPDF 错误:图像文件丢失或不正确
我正在使用 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 文件,即使也包含图像,但在移动到服务器后这不起作用。
我尝试过以下可能性:
使用图像的绝对和相对路径。
尝试将本地图像放置在同一文件夹中。
所有图像格式,如 jpg、png 和 gif。
还检查了图像和相应文件夹的权限。
这些案例都不适合我,困扰着这个问题,任何人都可以帮助我解决这个问题。
谢谢!
西瓦...
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:
With absolute and relative paths for the image.
Tried with a local image placed in the same folder.
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.
Thanks!
Siva ...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
经过长时间的斗争,我终于找到了这个问题的原因,我已经讨论过 此处。
主要这个问题是由于我的服务器未启用“allow_url_fopen”设置,我已经通过使用 CURL 解决了这个问题。我给出了解决此问题的分步过程,因为这对于像我这样的人来说可能有用,以避免浪费时间(寻找解决方案)。
我认为这是解决这个问题的一种方法,如果有人知道其他方法,那么你可以将它们发布在这里,这样这个问题可能对某人有用。
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).
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.
只需在 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/
任何人都可以使用此代码来解决。也许它会起作用。因为有时某些情况可能会有所不同。就我而言,我遇到了同样的问题,但在使用它之后。我解决了我的问题..
尝试为 $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..
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.