php TCPDF 错误:[图像] 无法获取图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7665540/
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-26 03:07:56  来源:igfitidea点击:

TCPDF ERROR: [Image] Unable to get image

phptcpdf

提问by Shafiul

I'm using TCPDFwith Drupal's printmodule to generate PDF of articles, & ending up with following error message when I click the link to generate PDF:

我将TCPPDF与 Drupal 的打印模块一起使用来生成文章的 PDF,当我单击链接生成 PDF 时,最终会出现以下错误消息:

TCPDF ERROR: [Image] Unable to get image: http://localhost/pathToDrupal/themes/bartik/logo.png

TCPDF ERROR: [Image] Unable to get image: http://localhost/pathToDrupal/themes/bartik/logo.png

The Image exists in the location specified. I tried applying:

图像存在于指定的位置。我尝试申请:

allow_url_fopen = On;
allow_url_include = On;

in php.inibut that could not resolve the problem.

php.ini但这根本就不是解决问题。

Please care to help :(

请关心帮助:(

回答by vstm

Apparently the "fopen"-wrappers are not used by TCPDF.

显然,TCPDF 不使用“fopen”包装器。

If you supply the URL of an image, TCPDF tries to download it with cURL into the "cache"-directory where your TCPDF-installation is stored (you have to check what the K_PATH_CACHE-constant contains to be sure).

如果您提供图像的 URL,TCPDF 会尝试使用 cURL 将其下载到存储 TCPDF 安装的“缓存”目录中(您必须检查K_PATH_CACHE-constant 包含的内容以确保)。

So I guess you have to have write permissions in this directory that the magic works. Also you need cURL enabled.

所以我猜你必须在这个魔法工作的目录中拥有写权限。您还需要启用 cURL。

回答by luissquall

We had problems with the way connections were handled in our linux "example.com" server. So this lead us to try on the server:

我们在 linux “example.com” 服务器中处理连接的方式有问题。所以这导致我们在服务器上尝试:

curl -v http://www.example.com/image.jpg

Whenever TCPDF tried to download an image with curl, the image was not found, nevertheless, we could view the image by just opening it directly in the browser (e.g. http://www.example.com/image.jpg).

每当 TCPDF 尝试使用 curl 下载图像时,都找不到图像,但我们可以通过直接在浏览器中打开它来查看图像(例如http://www.example.com/image.jpg)。

We solved the problem by setting the example.com VirtualHost to accept 127.0.0.1 connections and adding the line "127.0.0.1 example.com" to /etc/hosts.

我们通过将 example.com VirtualHost 设置为接受 127.0.0.1 连接并将“127.0.0.1 example.com”行添加到 /etc/hosts 来解决该问题。

回答by Noman Mustafa Khan

Just use the image path as "images/your_image.png" instead of "http://yourdomain.com/images/your_image.png" in the PDF html.

只需在 PDF html 中使用图像路径作为“images/your_image.png”而不是“ http://yourdomain.com/images/your_image.png”。

回答by Michael

I've found that TCPDF would throw the Unable to get Image: error when the src was an absolute link. Just changing to a relative link would successfully create the PDF.

我发现当 src 是绝对链接时,TCPDF 会抛出 Unable to get Image: 错误。只需更改为相对链接即可成功创建 PDF。

回答by batigolix

I had this problem on a staging server where the docroot of the site was protected by an .htaccess file (to prevent search engine indexing and other confusions)

我在站点的 docroot 受 .htaccess 文件保护的登台服务器上遇到了这个问题(以防止搜索引擎索引和其他混淆)

tcpdf uses curl to fetch the image and it gives this error if it cannot access the file.

tcpdf 使用 curl 来获取图像,如果无法访问该文件,则会出现此错误。

To solve it, I added a rule to .htaccess to allow requests from the webserver

为了解决这个问题,我在 .htaccess 中添加了一条规则以允许来自网络服务器的请求

Allow from 123.45.6.7.8

回答by Ajsti.pl - Maciej Szewczyk

Try to add path by current working dir.

尝试按当前工作目录添加路径。

$img = getcwd().'/web/bundles/_bundlename_/img/logo.png';

回答by shashank

$pdf->Image($base_url.'/'.$node->field_loc_images[0]['filepath'] ,30, 40, 75, 113, 'JPG', '', '', true, 300, '');

回答by jay ralph

In your font unicode problems you need to put this syntax code:

在您的字体 unicode 问题中,您需要放置以下语法代码:

// set font
$fontname = $pdf->addTTFfont('../your path here/sampletruetype.ttf', 'TrueTypeUnicode', '', 32);

$pdf->SetFont($fontname, '', <font size value here>);

put it before you add the page...

把它放在你添加页面之前......

回答by flabel

In drupal be sure to include the tcpdf library in your function and not at the top of your module file or you will get this error.

在 drupal 中,请确保在您的函数中包含 tcpdf 库,而不是在模块文件的顶部,否则您将收到此错误。

回答by Carlo Paglietti

After I upgraded PHP 5.5 to 5.6, I lost hours because of an image error,

我将 PHP 5.5 升级到 5.6 后,由于图像错误而损失了几个小时,

I found the solution here from @caligari(a comment on the accepted answer) and it fixed the problem for me:

我从@caligari找到了解决方案(对已接受答案的评论),它为我解决了这个问题:

install CURL like this:

像这样安装 CURL:

apt-get install php5-curl && /etc/init.d/apache2 restart.