tags:

views:

24

answers:

3

I'm creating a PDF file from a txt-template with tcpdf ([Example 8][1]). The txt-template looks like this:

SALUTATION  
FIRSTNAME LASTNAME                            
STREET                     CURRENTDATE
SOMEMOREINFORMATION                           MYWEBSITE

I replace those markers with the correct value. So that it would look like this:

Mr.  
John Doe                   
Downingstreet 10           14th May, 2010
[email protected]                                  www.stackoverflow.com

In this example, when I replace the values, the indention of the date is dependent on the length of the street name (which I don't want). I could solve this issue with str_pad but the problem is, I normally use three columns and there are lines which only have content in col1 and col3 as in the last line. How can I solve that problem? Is there something like the "overwrite" function in Word, that when you write, the text just gets overwritten?

Thanks in advance.

A: 

Count street's string length and then add/remove left padding of date.

hsz
A: 

You can use sprintf, e.g.

function something($street, $currentDate, $foo) {
  $s = sprintf('%-20s    %-18s    %s',
    $street, 
    $currentDate, 
    $foo
  );
  return $s;
}

echo something('streetA', '14th May, 2010', 'lalala'), "\n";
echo something('Downingstreet 10', '14th May, 2010', 'lalala'), "\n";
echo something('abcdefghijklmnopqrstuvwxyz 10', '14th May, 2010', 'lalala'), "\n";

prints

streetA                 14th May, 2010        lalala
Downingstreet 10        14th May, 2010        lalala
abcdefghijklmnopqrstuvwxyz 10    14th May, 2010        lalala

(as you can see from the third line the width specification is the minimum length, so you might have to use something like substr())

VolkerK
A: 

I presume you are just str_replace()'ing the placeholders with their values?

$streetPlaceHolder = 'STREET                     ';
$streetReplacement = str_pad('Downingstreet 10', strlen($streetPlaceHolder));
$template = str_replace($streetPlaceHolder, $streetReplacement, $template);

Presumably you will run into the same problem with SOMEMOREINFORMATION. This same solution can be used.

I realize you said str_pad was not an ideal solution for you. However, I do not understand why, even if you extend this to three columns. You can still get by with this method.

erisco