How to view Tables in pdf with MPDF library?

Asked

Viewed 3,372 times

1

I have an html that is automatically generated by a wysiwyg component (a text editor) and within one of the texts I generated, this one has a table and this table appears normally in html, however I need this table to appear in the pdf I am generating.

The PDF is coming out like this: O PDF ESTÁ SAINDO ASSIM:

And I wanted it to come out like this, like in my html: SOLUÇÃO

<?php
session_start();
include('mpdf60/mpdf.php');

$mpdf = new mPDF(
             '',    // mode - default ''
             '',    // format - A4, for example, default ''
             0,     // font size - default 0
             '',    // default font family
             15,    // margin_left
             15,    // margin right
             58,     // margin top
             0,    // margin bottom
             6,     // margin header
             0,     // margin footer
             'L');  // L - landscape, P - portrait
$mpdf->SetDisplayMode('fullpage');



$paragrafo = "<p style=\"text-align:justify;line-height:150%\">
                    <span style=\"font-size: 12pt; line-height: 150%; font-family: Verdana, sans-serif; color: windowtext;\">
                        <!--<o:p>&nbsp;</o:p>-->
                    </span>
              </p>
              <table class=\"table\" border=\"2\" cellspacing=\"1\" cellpadding=\"1\" style=\"width: 940px;\" align=\"\">
                <tbody>
                    <tr>
                        <td style=\"text-align: center;\">
                            <span style=\"font-size: large; background-color: rgb(153, 153, 153);\">2. OBJETIVO</span>
                        </td>
                    </tr>
                </tbody>
              </table>";


$mpdf->WriteHTML($paragrafo);

$mpdf->Output();

exit;
?>
  • 1

    Alexandre, try to generate this table outside the mpdf and then put it in, Oce does not need to use css inline, take a look at my answer about something similar: http://answall.com/questions/111332/como-creat-duas-divs-uma-ao-lado-da-outra-usando-mpdf/111348#111348

  • actually this html I’m not doing, it’s a wysiwyg (a text editor) that does it, so I can’t control this inline css

  • and I didn’t understand what you meant by "generate this table outside the mpdf and then put in"@Gabrielrodrigues

  • You said that in html it is correct and in mpdf it is wrong, if you play the html result in a normal html file or in https://jsfiddle.net/ it will come out well formatted or not ? It’s that $paragraph

  • yes, it exits correct in html file

  • But then how am I supposed to send it? which is in php that this is done, and are several texts, and not just this text, the variable $paragrafo joins the various texts, concatenating the

  • @Gabrielrodrigues unfortunately that’s not the problem, I put the html q ta giving problem, without the bars, in my php code and still does not appear in the generated pdf...

Show 2 more comments

1 answer

2


Your problem lies in using the direct border attribute in the table.

Unfortunately mpdf cannot perform this simple conversion:

<table border="2">
// Para
<table style='border:2px solid black;'>

So he ends up ignoring this setting.

Then you wonder, what are my alternatives ?

1 - You can report to mpdf and wait for a possible fix.

2 - You can look for a text editor that delivers border code in css.

3 - Or you can make an advanced RTA, for example, if you know that all your tables will have borders you can add a css of your own customizing mpdf.

Example:

$stylesheet = "table{
  width: 100%;
  text-align:center;
  border: 2px solid black;
}";

And then you add it to Writehtml:

$mpdf = new mPDF();
$mpdf->WriteHTML($stylesheet,1);
$mpdf->WriteHTML($html,2);

Prior:

inserir a descrição da imagem aqui

  • um, I’m going to test this here, this table, which will have the border, will come from an input text that will be the title, I believe that the other Tables that may arise will not have border (I hope hehe)

  • worked out, thanks for the help Gabriel :)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.