2

I have some form data and after submitting, a PDF is generated. The problem is size of tables in PDF. If some string in my form is too long the PDF is generated wrongly. It's because the tables. They have static size so if string is too long the table did not accommodate it. I use mPDF library.

How can I make this tables size dynamics so as to their size changed depending on the length of string?

If string {p} is longest then width of table the table size decrease. I want to if the string {p} is longest than width of table then {p} should writing out in next line and the height of table should increase.

A screenshot if string in field (name) is short:

short string {p}

A screenshot if string in field (name) is long:

long string {p}

A screenshot if string in field (name) is very long:

very_long string

Here is a fragment of code with mPDF:

$mpdf=new mPDF('UTF-8','A4','','',20,15,48,25,10,10);

$mpdf->shrink_tables_to_fit=1;
$mpdf->keep_table_proportions = true;
$mpdf->WriteHTML(generatePDF());
$mpdf->AddPage();
$mpdf->WriteHTML(generatePDF2());

$mpdf->Output();
exit;

And fragment of HTML tables:

function generatePDF(){
    global $a,$b,$c,$d;
    $html = getHTMLStyle().'

<div style="text-align: left;"><span style="font-size: 11pt;font-weight: bold;">FORMULARZ KONSULTACJI<br />PROJEKTU PRAWA MIEJSCOWEGO<br /> W ZAKRESIE DZIAŁALNOŚCI STATUTOWEJ ORGANIZACJI POZARZĄDOWEJ*<br /><br /></span></div>

<table class="items" width="100%" style="font-size: 9pt; border-collapse: collapse;" cellpadding="8">

<tr>
<td width="5%">A</td>
<td width="95%"><b>'.$a.'</b><br /><br /> '.$_POST['title'].'</td>
</tr>
3

4 Answers 4

2
<style>
    table {
        overflow: wrap;
    }
</style>
1

Here:

<tr>
<td width="5%">A</td>
<td width="95%"><b>'.$a.'</b><br /><br /> '.$_POST['title'].'</td>
</tr>

Try using a fixed value for table width instead of a percent. something like:

<table class="items" width="950" style="font-size: 9pt; border-collapse: collapse;" cellpadding="8">

And see what happens.

1

It's a late answer but it's worth of answering now. I too faced same problem when a table tr have very big content. I tried the below solution but no luck.

$mpdf->shrink_tables_to_fit=0;

Then I have changed my html structure from table into div, boom it's worked. So now it's working perfectly and the big content is spiting properly between the pages without overlapping header and footer. I hope it will help someone. Cheers

1
  • 1
    You are correct, after changing the table structure to div, the pdf looks beautiful. Thanks. You can find this link html-cleaner.com/features/replace-html-table-tags-with-divs useful for converting table to div format. NOTE: I have tried all the solutions given in the SO, but this is the last one tried and succeeded.
    – Boopathi D
    May 30, 2021 at 12:59
0

It is really hard to find the problem, but the answer is simple. table with style="overflow:wrap" after reading the source code:

elseif ($table['overflow']=='wrap')
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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