TCPDF - inserting images from an array, duplicate characters causing caching error
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\" />";
开发者_JAVA百科 $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');
?>
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?
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.
精彩评论