2011-05-13 67 views
0

我正在使用TCPDF在運行中創建PDF。在創建PDF之前,我有一個循環,從字符數組取值,將它們插入到一個字符串,提供圖像名稱這是使用代碼IM:(我是新,是很好)TCPDF - 從數組插入圖像,導致緩存錯誤的重複字符

$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 ERROR: [Image] Unable to get image: E:/wamp/www/PDF%20test/cache/mska_2434d8c67d438ea8956284f8725ce42d 

我已經通過了對緩存引用tcpdf.php文件中查找:如果有任何重複的字符

TCPDF給出了這樣的錯誤。它使用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'); 
?> 
+0

如果你已經確認它使用md5作爲臨時名稱,那麼我想你可以用一個UUID生成器替換md5。你使用TCPDF的什麼版本? – 2011-05-13 13:42:44

+0

最新版本5.9.078。我將如何實現UUID?謝謝 – tcnarss 2011-05-13 13:46:21

+0

我已經發布了它,但第二次認爲這可能無法幫助你...在哪一行你選擇了md5呼叫? – 2011-05-13 14:01:56

回答

1

如果您確定這個問題是它使用md5你可以使用UUID。下面是創建有效的UUID A類:

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調用?

+0

其在線7247或7280的最新版本 – tcnarss 2011-05-13 14:29:16

+0

我試圖以這種方式複製您的錯誤:在PDF中,我有兩個相同的img標籤,如下所示: $ baseurl。=「components/com_spin/libs/tcpdf /圖像/ LOGO_SPIN.jpg「; $ out。=「」; $ out。=「」; 然後處理輸出,我得到正確的兩個圖像,沒有錯誤...這個測試案例是否正確? – 2011-05-13 14:53:23

+0

我也嘗試過使用相同的數據調用Image()函數兩次,並且仍然沒有錯誤 – 2011-05-13 14:54:57

0

我不知道這是否對任何人來說都是正確的解決方案。我遇到了同樣的問題,並且檢測到只有重複圖像(通過多頁)以PNG格式顯示時纔會出現此消息錯誤。如果您對JPG圖像執行相同的操作,則TCPDF會正確處理它,而不管您是否始終使用相同的文件名。

相關問題