How to center an html table?

Trying to create the correct PDF using PHP and TCPDF.

Can you help me how can I use the writeHTML function to create and the central table in TCPDF?

Tried using:

 $html = '

 <div style="margin-left: auto; margin-right: auto; width: 50%">
  <table border="1" width="200" align="center"><tr><td><b>Invoice number: '.$this->xInvoiceNumber.'</b></td></tr></table>
  <br />
  <table border="1" width="200" align="center"><tr><td>'.$this->xClient.'</td></tr></table>
  <br />
 </div>

... but no luck.

+3
source share
5 answers

You need to create a table with three columns, set the width for each of them, and in the middle you have to create your table.

<table>
<tr>
<td style="width:25%"></td>
<td style="width:50%"><table><tr><td>Your content</td></tr></table></td>
<td style="width:25%"></td>
</tr>
</table>

I am not proud of this method, but it works :)

+13
source

Ok, so I don’t know if there is a solution for my problem ...

However, I managed to solve this problem using the writeHTMLCell function, i.e.

$this->writeHTMLCell(50, 0, 50, 50, 'cellcontent', 'LRTB', 1, 0, true, 'L'); 

If someone can find a better solution, answer.

Tnx!

+3

div div ...

<div style="margin:5px auto; width:50%">
0
source

Never do anything like this, this is the code you need to center a cross-browser compatible table.

<div style="text:align:center;">
  <table style="margin:0px auto" border="1" width="200" align="center">
    <tr>
      <td><b>Invoice number: </b></td>
    </tr>
  </table>
  <br />
  <table style="margin:0px auto"border="1" width="200" align="center">
    <tr>
      <td>Client</td>
    </tr>
  </table>
  <br />
</div>

If pdfs supports css, I would suggest styling html elements with css

table{
  border:1px solid black;
  margin:0px auto;
  text-align:center;
  width:200px;
}

Hope this helps!

0
source

Try this code; this worked for me:

<table style="width:100%">
  <tr>
    <td style="width:30%">left margin</td>
    <td style="width:40%">
      <table border="1" style="width:100%">
        <thead>
          <tr>
            <td style="width:100%" colspan="2"></td>
          </tr>
          <tr>
            <td style="width:40%"><b></b></td>
            <td style="width:60%"><b></b></td>
          </tr>
        </thead>
      </table>
    </td>
    <td style="width:30%">rigth margin</td>
  </tr>
</table>
0
source

Source: https://habr.com/ru/post/1740480/


All Articles