TCPDF - 从数组插入图像,重复字符导致缓存错误

发布于 2024-11-07 04:29:30 字数 4264 浏览 7 评论 0原文

我正在使用 TCPDF 即时创建 pdf。在创建 pdf 之前,我有一个循环,它从字符数组中获取值并将它们插入到字符串中以提供图像名称,这是我使用的代码:(我是新人,很好)

$a= str_split("Foo bar");
$str = "";

walk($a, $str);

function walk($pArray, &$str) {
        $i=1;

         foreach($pArray as $key=>$value) {
                    if(is_array($value)) {


                                                 walk($value,$str);

                    } else if($value== " "){    

                                                 $imvar[$i]="<img src=\"images/space.png\" />";
                                                 $str.= $imvar[$i];
                                                 $i++;

                    } else if($value !=" "){

                                                 $imvar[$i]="<img src=\"images/$value.png\"  />";
                                                 $str.= $imvar[$i];
                                                 $i++;

                    }
         }
}

所以这会创建一个新的图像标签对于数组中的每个字符(包括图像名称 a.png、b.png 等),

这工作正常,并且我得到了所需的效果,除非数组中有任何重复的字符,按照我给出的示例。

如果存在任何重复字符,TCPDF 会给出此错误:

TCPDF ERROR: [Image] Unable to get image: E:/wamp/www/PDF%20test/cache/mska_2434d8c67d438ea8956284f8725ce42d

我已查看 tcpdf.php 文件以获取对缓存的引用。我相信它使用 md5 作为临时名称,但显然如果使用重复的字符,它不喜欢它。

任何帮助将不胜感激。

此代码有效(数组中没有重复字符):

 <?php

require_once('tcpdf.php');

$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true,
'UTF-8', false);

$pdf->setPrintHeader(false);

$pdf->setPrintFooter(false);

$pdf->SetFont('helvetica', '', 10);

$pdf->AddPage();

$a= str_split("Fobar");
$str = "";

walk($a, $str);

function walk($pArray, &$str) {
            $i=1;

             foreach($pArray as $key=>$value) {
                        if(is_array($value)) {


                                                                        walk($value,$str);

                        } else if($value== " "){
                                                                        $imvar[$i]="<img src=\"images/space.png\" />";
                                                                        $str.= $imvar[$i];
                                                                        $i++;

                        } else if($value !=" "){

                                                                        $imvar[$i]="<img src=\"images/$value.png\"  />";
                                                                        $str.= $imvar[$i];
                                                                        $i++;

                        }
            }
}

$html = $str;

$pdf->writeHTML($html, true, false, true, false, '');
$pdf->Output('test_images.pdf', 'I');
?>

并且此代码不起作用(数组中重复字符):

<?php

require_once('tcpdf.php');

$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true,
'UTF-8', false);

$pdf->setPrintHeader(false);

$pdf->setPrintFooter(false);

$pdf->SetFont('helvetica', '', 10);

$pdf->AddPage();

$a= str_split("Fooooobar");
$str = "";

walk($a, $str);

function walk($pArray, &$str) {
            $i=1;

             foreach($pArray as $key=>$value) {
                        if(is_array($value)) {


                                                                        walk($value,$str);

                        } else if($value== " "){
                                                                        $imvar[$i]="<img src=\"images/space.png\" />";
                                                                        $str.= $imvar[$i];
                                                                        $i++;

                        } else if($value !=" "){

                                                                        $imvar[$i]="<img src=\"images/$value.png\"  />";
                                                                        $str.= $imvar[$i];
                                                                        $i++;

                        }
            }
}

$html = $str;

$pdf->writeHTML($html, true, false, true, false, '');
$pdf->Output('test_images.pdf', 'I');
?>

I am using TCPDF to create a pdf on the fly. Before the pdf is created i have a loop that takes values from an array of characters and inserts them into a string to provide image names this is the code im using: (i'm new, be nice)

$a= str_split("Foo bar");
$str = "";

walk($a, $str);

function walk($pArray, &$str) {
        $i=1;

         foreach($pArray as $key=>$value) {
                    if(is_array($value)) {


                                                 walk($value,$str);

                    } else if($value== " "){    

                                                 $imvar[$i]="<img src=\"images/space.png\" />";
                                                 $str.= $imvar[$i];
                                                 $i++;

                    } else if($value !=" "){

                                                 $imvar[$i]="<img src=\"images/$value.png\"  />";
                                                 $str.= $imvar[$i];
                                                 $i++;

                    }
         }
}

So this creates a new image tag for each character in the array (including an image name a.png,b.png etc)

This works fine and i am getting the desired effect unless i have any repeated characters in the array, as per the example i have given.

TCPDF gives this error if there are any duplicate characters:

TCPDF ERROR: [Image] Unable to get image: E:/wamp/www/PDF%20test/cache/mska_2434d8c67d438ea8956284f8725ce42d

I have looked through the tcpdf.php file for references to caching. Its using md5 i believe for the temp name, but obviously if a repeated character is used it doesn't like it.

Any help would be much appreciated.

this code works( no repeating characters in the array):

 <?php

require_once('tcpdf.php');

$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true,
'UTF-8', false);

$pdf->setPrintHeader(false);

$pdf->setPrintFooter(false);

$pdf->SetFont('helvetica', '', 10);

$pdf->AddPage();

$a= str_split("Fobar");
$str = "";

walk($a, $str);

function walk($pArray, &$str) {
            $i=1;

             foreach($pArray as $key=>$value) {
                        if(is_array($value)) {


                                                                        walk($value,$str);

                        } else if($value== " "){
                                                                        $imvar[$i]="<img src=\"images/space.png\" />";
                                                                        $str.= $imvar[$i];
                                                                        $i++;

                        } else if($value !=" "){

                                                                        $imvar[$i]="<img src=\"images/$value.png\"  />";
                                                                        $str.= $imvar[$i];
                                                                        $i++;

                        }
            }
}

$html = $str;

$pdf->writeHTML($html, true, false, true, false, '');
$pdf->Output('test_images.pdf', 'I');
?>

and this doesnt work( repeating chars in the array):

<?php

require_once('tcpdf.php');

$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true,
'UTF-8', false);

$pdf->setPrintHeader(false);

$pdf->setPrintFooter(false);

$pdf->SetFont('helvetica', '', 10);

$pdf->AddPage();

$a= str_split("Fooooobar");
$str = "";

walk($a, $str);

function walk($pArray, &$str) {
            $i=1;

             foreach($pArray as $key=>$value) {
                        if(is_array($value)) {


                                                                        walk($value,$str);

                        } else if($value== " "){
                                                                        $imvar[$i]="<img src=\"images/space.png\" />";
                                                                        $str.= $imvar[$i];
                                                                        $i++;

                        } else if($value !=" "){

                                                                        $imvar[$i]="<img src=\"images/$value.png\"  />";
                                                                        $str.= $imvar[$i];
                                                                        $i++;

                        }
            }
}

$html = $str;

$pdf->writeHTML($html, true, false, true, false, '');
$pdf->Output('test_images.pdf', 'I');
?>

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

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

发布评论

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

评论(2

苏大泽ㄣ 2024-11-14 04:29:30

如果您确定问题是它使用 md5,您可以使用 UUID。下面是一个创建有效 UUID 的类:

class UUID {
  public static function v3($namespace, $name) {
    if(!self::is_valid($namespace)) return false;

    // Get hexadecimal components of namespace
    $nhex = str_replace(array('-','{','}'), '', $namespace);

    // Binary Value
    $nstr = '';

    // Convert Namespace UUID to bits
    for($i = 0; $i < strlen($nhex); $i+=2) {
      $nstr .= chr(hexdec($nhex[$i].$nhex[$i+1]));
    }

    // Calculate hash value
    $hash = md5($nstr . $name);

    return sprintf('%08s-%04s-%04x-%04x-%12s',

      // 32 bits for "time_low"
      substr($hash, 0, 8),

      // 16 bits for "time_mid"
      substr($hash, 8, 4),

      // 16 bits for "time_hi_and_version",
      // four most significant bits holds version number 3
      (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x3000,

      // 16 bits, 8 bits for "clk_seq_hi_res",
      // 8 bits for "clk_seq_low",
      // two most significant bits holds zero and one for variant DCE1.1
      (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000,

      // 48 bits for "node"
      substr($hash, 20, 12)
    );
  }

  public static function v4() {
    return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',

      // 32 bits for "time_low"
      mt_rand(0, 0xffff), mt_rand(0, 0xffff),

      // 16 bits for "time_mid"
      mt_rand(0, 0xffff),

      // 16 bits for "time_hi_and_version",
      // four most significant bits holds version number 4
      mt_rand(0, 0x0fff) | 0x4000,

      // 16 bits, 8 bits for "clk_seq_hi_res",
      // 8 bits for "clk_seq_low",
      // two most significant bits holds zero and one for variant DCE1.1
      mt_rand(0, 0x3fff) | 0x8000,

      // 48 bits for "node"
      mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
    );
  }

  public static function v5($namespace, $name) {
    if(!self::is_valid($namespace)) return false;

    // Get hexadecimal components of namespace
    $nhex = str_replace(array('-','{','}'), '', $namespace);

    // Binary Value
    $nstr = '';

    // Convert Namespace UUID to bits
    for($i = 0; $i < strlen($nhex); $i+=2) {
      $nstr .= chr(hexdec($nhex[$i].$nhex[$i+1]));
    }

    // Calculate hash value
    $hash = sha1($nstr . $name);

    return sprintf('%08s-%04s-%04x-%04x-%12s',

      // 32 bits for "time_low"
      substr($hash, 0, 8),

      // 16 bits for "time_mid"
      substr($hash, 8, 4),

      // 16 bits for "time_hi_and_version",
      // four most significant bits holds version number 5
      (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x5000,

      // 16 bits, 8 bits for "clk_seq_hi_res",
      // 8 bits for "clk_seq_low",
      // two most significant bits holds zero and one for variant DCE1.1
      (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000,

      // 48 bits for "node"
      substr($hash, 20, 12)
    );
  }

  public static function is_valid($uuid) {
    return preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?'.
                      '[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/i', $uuid) === 1;
  }
}

要使用它:

$newUUID = UUID::v4();

在这种情况下,两次使用相同的临时名称不会出现问题。事实是,如果 TCPDF 使用 md5 创建“指纹”临时名称(也就是说,它使用 md5,以便当他必须获取文件时,它会对文件名进行另一个 md5 转换),这对您没有用。

您在哪条线路上进行此 md5 调用?

If you are sure that the problem is that it uses md5 you can use UUID. Here is a Class that creates valid UUID:

class UUID {
  public static function v3($namespace, $name) {
    if(!self::is_valid($namespace)) return false;

    // Get hexadecimal components of namespace
    $nhex = str_replace(array('-','{','}'), '', $namespace);

    // Binary Value
    $nstr = '';

    // Convert Namespace UUID to bits
    for($i = 0; $i < strlen($nhex); $i+=2) {
      $nstr .= chr(hexdec($nhex[$i].$nhex[$i+1]));
    }

    // Calculate hash value
    $hash = md5($nstr . $name);

    return sprintf('%08s-%04s-%04x-%04x-%12s',

      // 32 bits for "time_low"
      substr($hash, 0, 8),

      // 16 bits for "time_mid"
      substr($hash, 8, 4),

      // 16 bits for "time_hi_and_version",
      // four most significant bits holds version number 3
      (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x3000,

      // 16 bits, 8 bits for "clk_seq_hi_res",
      // 8 bits for "clk_seq_low",
      // two most significant bits holds zero and one for variant DCE1.1
      (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000,

      // 48 bits for "node"
      substr($hash, 20, 12)
    );
  }

  public static function v4() {
    return sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',

      // 32 bits for "time_low"
      mt_rand(0, 0xffff), mt_rand(0, 0xffff),

      // 16 bits for "time_mid"
      mt_rand(0, 0xffff),

      // 16 bits for "time_hi_and_version",
      // four most significant bits holds version number 4
      mt_rand(0, 0x0fff) | 0x4000,

      // 16 bits, 8 bits for "clk_seq_hi_res",
      // 8 bits for "clk_seq_low",
      // two most significant bits holds zero and one for variant DCE1.1
      mt_rand(0, 0x3fff) | 0x8000,

      // 48 bits for "node"
      mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
    );
  }

  public static function v5($namespace, $name) {
    if(!self::is_valid($namespace)) return false;

    // Get hexadecimal components of namespace
    $nhex = str_replace(array('-','{','}'), '', $namespace);

    // Binary Value
    $nstr = '';

    // Convert Namespace UUID to bits
    for($i = 0; $i < strlen($nhex); $i+=2) {
      $nstr .= chr(hexdec($nhex[$i].$nhex[$i+1]));
    }

    // Calculate hash value
    $hash = sha1($nstr . $name);

    return sprintf('%08s-%04s-%04x-%04x-%12s',

      // 32 bits for "time_low"
      substr($hash, 0, 8),

      // 16 bits for "time_mid"
      substr($hash, 8, 4),

      // 16 bits for "time_hi_and_version",
      // four most significant bits holds version number 5
      (hexdec(substr($hash, 12, 4)) & 0x0fff) | 0x5000,

      // 16 bits, 8 bits for "clk_seq_hi_res",
      // 8 bits for "clk_seq_low",
      // two most significant bits holds zero and one for variant DCE1.1
      (hexdec(substr($hash, 16, 4)) & 0x3fff) | 0x8000,

      // 48 bits for "node"
      substr($hash, 20, 12)
    );
  }

  public static function is_valid($uuid) {
    return preg_match('/^\{?[0-9a-f]{8}\-?[0-9a-f]{4}\-?[0-9a-f]{4}\-?'.
                      '[0-9a-f]{4}\-?[0-9a-f]{12}\}?$/i', $uuid) === 1;
  }
}

To use it:

$newUUID = UUID::v4();

In this case you have no problems of having the same temp-name twice. Fact is, if TCPDF uses md5 to create "fingerprinted" temp names (that is it uses md5 so that when he has to get the file it makes another md5 conversion on the file name) thi is not useful to you.

On which line do you have this md5 call?

晚雾 2024-11-14 04:29:30

我不知道这是否对任何人来说都是解决这个问题的正确方法。我遇到了同样的问题,并且我检测到仅当重复图像(通过多个页面)为 PNG 格式时才会引发此消息错误。如果您对 JPG 图像执行相同的操作,则 TCPDF 会正确处理它,无论您是否始终使用相同的文件名。

I don't know if it will be a right solution for anybody to this problem. I had the same issue and I detected that this message error is thrown only if the repeated image (through several pages) is in PNG format. If you do the same with a JPG image TCPDF processes it correctly, no matter if you use always the same file name.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文