TCPDF 无法成像,因为它使用了错误的目录路径

发布于 2025-01-08 15:51:18 字数 302 浏览 1 评论 0原文

我在本地主机上的 pdf 文档中获取图像,但在生产站点上收到错误 TCPDF ERROR: [Image] Unable to get image 我正在使用 html img 标签来获取图像和src 是该图像的目录路径,而不是网址,但我发现 TCPDF 正在添加我给它的路径以及我的 www 文件夹的路径,例如:

我给 tcpdf 的图片路径:首页/inc_dir/img/pic.jpg
tcpdf 在这里查找它:home/www/home/inc_dir/pic.jpg

有人可以帮我找出 tcpdf 正在连接目录吗?

I get my images in my pdf document on my localhost but on the production site i get the error TCPDF ERROR: [Image] Unable to get image i am using an html img tag to get the images and the src is the directory path to this image not a url, but i found out that TCPDF is adding the path i give it with the path to my www folder like:

path to picture i give to tcpdf: home/inc_dir/img/pic.jpg
tcpdf looks for it here: home/www/home/inc_dir/pic.jpg

can someone please help me find out tcpdf is concatenating the directories?

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

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

发布评论

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

评论(5

她如夕阳 2025-01-15 15:51:18

您还可以仅更改图像路径而不是主路径使用:

define('K_PATH_IMAGES', '/path/to/images/');
require_once('tcpdf.php');

这不会破坏 fonts/ 和其他 tcpdf 路径。

You can also change only the image path instead of main path use:

define('K_PATH_IMAGES', '/path/to/images/');
require_once('tcpdf.php');

This won't break fonts/ and other tcpdf paths.

情愿 2025-01-15 15:51:18

TCPDF 使用 $_SERVER['DOCUMENT_ROOT'] 作为所有图像的根目录,并构建与其相关的绝对路径。您可以在 $_SERVER 中或使用以下 PHP 常量来更改它:K_PATH_MAIN

define('K_PATH_MAIN', '/path/to/my-images/');
require_once 'tcpdf.php';

TCPDF is using $_SERVER['DOCUMENT_ROOT'] as a root directory of all your images, and builds their absolute paths in relation to it. You can change it either in $_SERVER or with this PHP constant: K_PATH_MAIN:

define('K_PATH_MAIN', '/path/to/my-images/');
require_once 'tcpdf.php';
烧了回忆取暖 2025-01-15 15:51:18

我使用图像数据而不是路径。可以使用图像的 src 属性中的 @ 将其传递给 TCPDF,如下所示:

<img src="@<?php echo base64_encode('/path/to/image.png')?>" />

HTML 中的 img 标签采用 BASE64 编码的字符串,这与 Image() 函数不同,后者采用未编码的数据。

我不知道这是否有记录,我通过阅读代码发现了这一点(tcpdf.php,第 18824 行页):

if ($imgsrc[0] === '@') {
    // data stream
    $imgsrc = '@'.base64_decode(substr($imgsrc, 1));
    $type = '';
}

I use image data instead of paths. It can be passed to TCPDF using an @ in the image's src-attribute, like so:

<img src="@<?php echo base64_encode('/path/to/image.png')?>" />

An img-tag in HTML takes a BASE64 encoded string, unlike the Image() function, which takes unencoded data.

I don't know if this is even documented, I found this by reading the code (tcpdf.php, line 18824 pp):

if ($imgsrc[0] === '@') {
    // data stream
    $imgsrc = '@'.base64_decode(substr($imgsrc, 1));
    $type = '';
}
月牙弯弯 2025-01-15 15:51:18

我也有同样的问题。但现在已经解决了。
我已将 TCPDF.php 的代码从

旧代码

if ($tag['attribute']['src'][0] == '/') {
   $tag['attribute']['src'] = $_SERVER['DOCUMENT_ROOT'].$tag['attribute']['src'];
}
$tag['attribute']['src'] = urldecode($tag['attribute']['src']);
$tag['attribute']['src'] = str_replace(K_PATH_URL, K_PATH_MAIN, $tag['attribute']['src']);

更改为新代码,

if ($tag['attribute']['src'][0] == '/') {
   $tag['attribute']['src'] = $_SERVER['DOCUMENT_ROOT'].$tag['attribute']['src'];
   $tag['attribute']['src'] = urldecode($tag['attribute']['src']);
   $tag['attribute']['src'] = str_replace(K_PATH_URL, K_PATH_MAIN, $tag['attribute']['src']);
}

请尝试此操作。

I have got the same problem. But it is now resolved.
I have change the code of TCPDF.php from

Old Code

if ($tag['attribute']['src'][0] == '/') {
   $tag['attribute']['src'] = $_SERVER['DOCUMENT_ROOT'].$tag['attribute']['src'];
}
$tag['attribute']['src'] = urldecode($tag['attribute']['src']);
$tag['attribute']['src'] = str_replace(K_PATH_URL, K_PATH_MAIN, $tag['attribute']['src']);

New Code

if ($tag['attribute']['src'][0] == '/') {
   $tag['attribute']['src'] = $_SERVER['DOCUMENT_ROOT'].$tag['attribute']['src'];
   $tag['attribute']['src'] = urldecode($tag['attribute']['src']);
   $tag['attribute']['src'] = str_replace(K_PATH_URL, K_PATH_MAIN, $tag['attribute']['src']);
}

Please try this.

狼亦尘 2025-01-15 15:51:18
define ('K_PATH_IMAGES', dirname(__FILE__).'/assets/imgs/template/');
define ('K_PATH_IMAGES', dirname(__FILE__).'/assets/imgs/template/');
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文