4

I'm starting to think is not possible? (out of the box/default class)

I am using FPDF/FPDI PHP class to generate a .pdf

In this situation, I have some text that can be short, or be long (no telling)

So I set a width on my cell.. and change from Cell() to MultiCell()..

This allows long text to wrap.. however it seems to vertically align to the top?

Which is no good if the string is NOT long enough to wrap..

How can you force the text to the bottom of the cell regardless if it is a single line or a wrapped line?

Is there a default way to do this?

I saw mention of TCPDF (or whatever).. and some dead links to 'plug-ins'.. (but not sure if they were FPDI or not?)

This works and looks fine:

$pdf->MultiCell(185, 12, 'ABC-123-DEF-456 And-Last-Name-Here', 1, 'C', false); 

However this way, does NOT have the (single line) text at the bottom.. so there is a cap between the string output and the 'underline' it shoudl match up with with (baseline)

$pdf->MultiCell(185, 12, 'ABC-123 DEF-456', 1, 'C', false); 

How do you overcome this?

whispers
  • 962
  • 1
  • 22
  • 48

3 Answers3

1

I think, I just found solution.

As default FPDF class I use this code: https://github.com/Setasign/tFPDF/blob/master/tfpdf.php (it supports UTF-8 chars) Than have edited this class code: http://www.fpdf.org/en/script/script52.php

In Function drawRows() I have changed this row:

$l+=$cw[$c]; 

To this:

$l+=$this->GetStringWidth($c)/$this->FontSize*1000;

And than use drawTextBox() as is descripted in http://www.fpdf.org/en/script/script52.php

Now I will try to explain:

GetStringWidth() function supports utf-8 chars soo we can use it to calculate char width. And than we can get correct value with some multiplications and divisions for compability with that fpdf class.

I didnt test this code extensivly... only basic testing.

0

This seems could be the answer:

http://www.fpdf.org/en/script/script52.php

Unfortunately, at least to me, it doesn't seem to work when using UTF-8 characters.

ZioBit
  • 905
  • 10
  • 29
  • Thanks for this! I wonder why the down-vote. This script works for me for vertical alignment. For example ```$this->drawTextBox("Your text here", 100, 80, "J", "M", true);```. ```J``` for justify, ```M``` for vertical centre align. You can use ```B``` for bottom vertical alignment. I am yet to test @Vlastimil Schmid's workaround for the UTF-8 case, I'll post feedback here after that. – Jeromy Adofo Jul 09 '23 at 19:11
  • The workaround by @Vlastimil Schmid's answer indeed works for my UTF-8 case! Now if only I could figure out a way to tweak the line spacing between the wrapped text lines... – Jeromy Adofo Jul 09 '23 at 20:43
0

You can do it with a new line, "\n". But you need to keep in mind that cell height needs to be determined by number of "\n", as multi_cell will increase cell height automatically when required.

This is how I have done it:

spacing = " \n\n\n\n\n\n\n\n\n"
contents = "\n" + "hello" + spacing

pdf.multi_cell(10, 3, contents, border=1, align="R")

This will align "hello" to the top right corner.

When creating the spacing I added a space before the \n's otherwise it will be very close to the edge. I also added a \n before the "hello" as otherwise it will be too close to the top. I kept the multi_cell height to 3, but you can even put it to 1 or anything that makes sure your size of \n's is not smaller than the cell height.

I know this is very gimmicky, so if you have a better way please let me know.