Word wrapping the course name

Word wrapping the course name

by Leslie Sandoval -
Number of replies: 9
I finally had an instructor create a course with a name so long it goes right off the edges of the certificate. So I searched the forums and found a previous post about word wrapping (but on a portrait certificate...mine's landscape). I tried to adapt that code to my certificate and instead I absolutely killed the certificates all together.
I restored my backup of the certificate.php file but I'm feeling squeamish about trying this again since my knowledge of PDF generation could fill a thimble.

I'm pasting my certificate.php code as it is below (this is without the modifications of adding margins, etc.) So how can I wrap the text of the course title without making a bigger mess than I have today?

// Add text
$pdf->SetTextColor(0,0,120);
$pdf->SetLeftMargin(30);
$pdf->SetRightMargin(30);
cert_printtext(170, 125, 'C', 'Helvetica', 'B', 30, utf8_decode(get_string('titlevoice1', 'certificate')));
$pdf->SetTextColor(0,0,0);
cert_printtext(170, 180, 'C', 'Times', 'B', 20, utf8_decode(get_string('introvoice1', 'certificate')));
cert_printtext(170, 230, 'C', 'Helvetica', '', 30, utf8_decode($studentname));
cert_printtext(170, 280, 'C', 'Helvetica', '', 20, utf8_decode(get_string('statementvoice1', 'certificate')));
cert_printtext(170, 330, 'C', 'Helvetica', '', 20, '');
$pdf->Write(20,utf8_decode($course->fullname));
cert_printtext(170, 330, 'C', 'Helvetica', '', 20, utf8_decode($classname));
cert_printtext(500, 520, 'C', 'Helvetica', '', 14, utf8_decode($certificatedate));
cert_printtext(170, 420, 'C', 'Times', '', 10, utf8_decode($grade));
cert_printtext(170, 431, 'C', 'Times', '', 10, utf8_decode($outcome));
cert_printtext(170, 442, 'C', 'Times', '', 10, utf8_decode($credithours));
cert_printtext(170, 500, 'C', 'Times', '', 10, utf8_decode($code));
$i = 0 ;
if($certificate->printteacher){
$context = get_context_instance(CONTEXT_MODULE, $cm->id);
if ($teachers = get_users_by_capability($context, 'mod/certificate:printteacher', '', $sort='u.lastname ASC','','','','',false)) {
foreach ($teachers as $teacher) {
$i++;
cert_printtext(130, 440+($i *12) , 'L', 'Times', '', 12, utf8_decode(fullname($teacher)));
}}}

cert_printtext(150, 450, '', '', '', '', '');
$pdf->SetLeftMargin(130);
$pdf->WriteHTML($customtext);
?>
Average of ratings: -
In reply to Leslie Sandoval

Re: Word wrapping the course name

by Ken Gibson -

I assume that:

cert_printtext(170, 330, 'C', 'Helvetica', '', 20, utf8_decode($classname));

is the line that is killing you?

If so, try php wordwrap......something like this (placed above this $classname):

$classname = wordwrap($classname, 100, "<br />\n");

will make the name wrap (in this example at 100 characters).

Here is some more info: http://php.net/manual/en/function.wordwrap.php/span>

Then....you will have to move the next line down a bit:
cert_printtext(130, 440+($i *12) , 'L', 'Times', '', 12, utf8_decode(fullname($teacher)));
}}}

to maybe:

cert_printtext(130, 480+($i *12) , 'L', 'Times', '', 12, utf8_decode(fullname($teacher)));
}}}

as the wrapping line will now fall lower on the page.

  

In reply to Ken Gibson

Re: Word wrapping the course name

by Leslie Sandoval -
Tried it and still no luck....it's not breaking after 100 characters. The wordwrap part just needs to be before the line below, right? Am I missing anything else?

cert_printtext(170, 330, 'C', 'Helvetica', '', 20, utf8_decode($classname));
In reply to Ken Gibson

Re: Word wrapping the course name

by Leslie Sandoval -
So on closer inspection it is working - kinda...it's printing the <br /> in there (see image). I tried it with just the new line instead and that didn't work either.

Attachment moodleerror.jpg
In reply to Leslie Sandoval

Re: Word wrapping the course name

by Ken Gibson -

ah....ok, the PDF printing is ignoring the <br/> tagssad

try this:

First (make it break?):
$classname = wordwrap($classname, 100, "<br>\n");
See if just a <br> tag, not <br/> is caught (longshot and we break xhtml here)

Or...If that still doesn't work try this (assuming here we only need to get to 200 characters):

//ABOVE THE PRINT CODE

if (strlen ($classname>99)){ //classname over 99 characters
   $classname1 = substr($classname, 0, 99); //first 99 charsacters
   $classname2 = substr($classname, 100); //charaters 100+

}

//IN THE PRINT CODE

if ($classname1) {
   cert_printtext(170, 330, 'C', 'Helvetica', '', 20, utf8_decode($classname1));
}

else {
cert_printtext(170, 330, 'C', 'Helvetica', '', 20, utf8_decode($classname));
}

same for classname2......

Ken

In reply to Ken Gibson

Re: Word wrapping the course name

by Leslie Sandoval -
Hi Ken,
This just gets more and more baffling....but thank you so much for trying to help. I put in the code above just as you said. (I'm attaching the file so you can see I'm not a complete spazz.) ;) The only thing I changed was from 99 to 75 because I'm trying to do it for 75 characters instead.
It's still not breaking at all, so I'm guessing it's triggering the else statement somehow...?
In reply to Leslie Sandoval

Re: Word wrapping the course name

by Raymond Fürst -
Maybe there is a bracket in the wrong place?

try
if(strlen($classname)>99) ...

instead of
if(strlen($classname>99)) ...

In reply to Raymond Fürst

Re: Word wrapping the course name

by Leslie Sandoval -
See this is why blondes shouldn't play with PHP....my bad. smile

It works. I'm sure my code is ugly...I had to make a few other mods. But I'm posting it in case anybody wants to see it.

Now this is purely hypothetical....and probably nightmarish to ask. Is there an easy way I could do this so that it would only break on a space instead of in the middle of a word? I seem to remember doing that a long time ago in a galaxy far away in my PHP class....maybe...

I'm just so happy that this works for now...I didn't want to have to redo 300+ certs by hand.
In reply to Leslie Sandoval

Re: Word wrapping the course name

by Raymond Fürst -
The tcpdf-library provides several functions that print boxes of text onto a pdf document, such as MultiCell and htmlCell. Such boxes can contain several lines that are wrapped automatically.

The certificate module (to be precise: the lib.php therein) only uses the simple Cell-function from the tcpdf-library.

I modified my certificate/lib.php in a way that I can use those multicell-functions in the certificate-types.

I needed this, because I wanted an abstract of the course contents to be printed onto the certificate. I had to add some extra fields to the database and to the user-form too.

If you just need some text bits to be wrappable, adding a cert_printmultitext function to the library may help.
In reply to Leslie Sandoval

Re: Word wrapping the course name

by Ken Gibson -
Hey Leslie,

Sorry for the missed bracket (thanks Raymond for the catch). The Code I posted also cuts the classname at 100 characters regardless (should have posted this...see below).

//------ABOVE THE PRINT CODE------
$classname = wordwrap($classname, 75, "<br />\n"); //wrap at 75 characters

//Pull out text before and after the <br /> in the wrap
if ($pos = strpos($classname, "<br />")){ //look for the <br/>
$classname1 = substr($classname, 0, $pos); //make $classname1 everything before that <br/>
$classname2 = substr($classname, $pos+5); //make $classname2 everything after that <br/> - the $pos+5 is 5 characters past start of <br/>
}

//------IN THE PRINT CODE-------
if ($classname1) {
cert_printtext(170, 330, 'C', 'Helvetica', '', 20, utf8_decode($classname1));
}
else {
cert_printtext(170, 330, 'C', 'Helvetica', '', 20, utf8_decode($classname));
}
if ($classname2) {
cert_printtext(170, 370, 'C', 'Helvetica', '', 20, utf8_decode($classname2));
}