Question

I am generating pdf file using html2pdf (that is using tcpdf library, _tcpdf_5.0.002). Unfourtunately I cannot generate table borders properly.

require_once('inc/html2pdf/html2pdf.class.php');
$html2pdf = new HTML2PDF('P','A4','en');
$pdf_output = '';

// HEADER
$pdf_output .='<page backtop="8mm" backbottom="8mm" backleft="10mm" backright="10mm" style="font-size: 11px;font-family:freeserif;">';
$pdf_output .= '<style> 
    table {
        border-collapse:collapse;
    }
    </style>';

 $pdf_output.='<table>';
 $pdf_output.= '<tr><td style="border-top: 1px solid black;">A</td></tr>';
 $pdf_output.= '<tr><td style="border-top: 1px solid black;">A</td></tr>';
 $pdf_output.= '<tr><td style="">A</td></tr>';
 $pdf_output.= '<tr><td style="border-top: 1px solid black;">A</td></tr>';
 $pdf_output.='</table>';
 $pdf_output .='</page>';
 $html2pdf->WriteHTML($pdf_output);
 $attachment= $html2pdf->Output('attachment.pdf', 'S');
 ...

It doesn't show top borders for cells. It shows top-border only for first row and not for others. I have simplified this html markup for test purposes (in reality it doesn't render just "A" letters in one cell, there is content in differnt columns, but top-border is not rendering either).

How to render top-border for cells ucing html2pdf (tcpdf)?

Was it helpful?

Solution 5

Workaround for this would be using <hr> tag.

OTHER TIPS

Another workaround is to replace the css property border-collapse: collapse; by cellspacing="0"

<table cellspacing="0">

Reference

Just add border to table tag

<table  border="1" cellspacing="0"   >

for this quick and easy solution, use like this

$pdf_output.= '<tr><td style="border-top-color:#000000;border-top-width:1px;border-top-style:solid;">A</td></tr>';

i hope this will work well as i used this in pdf generation and it worked well for me.

Original solution didn't work, leaving it at the end and trying something else:

Instead of having a border, add an empty row above the cells to receive border-top, and color its cells black, with a 1px heigth. Before every lines that should have the border:

<tr><td style="background-color:#000;height:1px;font-size:0px;">&nbps;</td></tr>

--ORIGINAL--

The html styles of html2pdf is not very strong still... I have stumbled on similar problems before and I suggest you use some other element than table if you want fancy styling such as border-top... If you can be happy using , good. Else, change to something like

$pdf_output.= '<div><span style="border-top: 1px solid black;">A</span></div>';

I have not tested, as I am not on a dev PC but you might be able to do

style="border-top: 1px solid black;display:inline-block;width:50px"

To make your spans have the same widths and look like it is a table.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top