1

1) Below is Street 1 address entered by customer & displaying in invoice pdf :

enter image description here

I am getting Street 1 address in Custom pdf. but its displaying in single line.

2) My custom Pdf :

enter image description here

Issue :

its not displaying complete Street 1 address.

Requirement :

Restrict Number of charactars & words in single line so that complete street address will display in multiple lines.

$page->drawText($shippingAddress->getStreet(1) , 20, $page->getHeight()-240);
1

You could split the string using str_split then loop through the resulting array, drawing it line-by-line. For example:

$addressArr = str_split($shippingAddress->getStreet(1), 20);

foreach($addressArr as $line) {
    $page->drawText($line, 20, $page->getHeight()-240);
}

The 20 at the end of the first line would split it into chunks of 20 characters (see PHP Manual on str_split), and the loop may require you to move down the page for each line, depending on whether you are using extensions and which ones. If you haven't already seen it, Boost My Shop has a free extension for PDFs of all kinds, with documentation. It is free, and it has a built-in function for Multiline Text.

1

i used textChunk & wordwrap

$line = 600; 
$textChunk = wordwrap($shippingAddress->getStreet(1), 50, "\n");
foreach(explode("\n", $textChunk) as $textLine)
{
  if ($textLine!=='') 
  {
    $page->drawText(strip_tags(ltrim($textLine)), 20,  $line,  'UTF-8');
    $line -=20; // increase space between words
  }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.