TCPDF - Remove space between 2 tables (PHP) - php

I tried to write the table inside a loop by 1 table/loop by using 1 table equal to 1 row.
However, I got a space between 2 tables like attached image below. I try to remove by using css properties like margin,padding are not worked.
Data is generated in each loop
How can I remove the space between there table? this is my example code.
for ($j=0;$j<count($ins_englishDescriptionOfGoods);$j++){
$html = <<<EOD
<table width="100%;" border="1" align="center" style="padding: 5px 1px 5px 2px; border-collapse: collapse;">
<tr nobr="true">
<td width="6%">$ORDER</td>
<td width="16%" style="text-align:center;">$GCL_NO</td>
<td width="13%" style="text-align:center;">$CONTAINER_NO</td>
<td width="23%" style="text-align:left; font-size:12px; line-height:7px;">$ENG_DESC</td>
<td width="10%" style="text-align:right;" >$NETWEIGTH</td>
<td width="10%" style="text-align:right;">$QTY</td>
<td width="10%" style="text-align:right;">$PACKAGE</td>
<td width="12%" style="text-align:right;">$FOB</td>
</tr>
</table>
EOD;
$pdf->writeHTML(iconv('TIS-620','UTF-8' ,$html), true, false, false, false, '');
}

I was having this exact same problem just now.
I was able to remove the space between two tables by adding line-height: -0.8 to the table and adding line-height: 1.5 to all of the rows in the table:
$pdf->html('<p>These two tables are touching each other:</p>');
$pdf->html('<table class="table-border" style="line-height: -0.8"><tbody><tr style="line-height: 1.5"><td>Hello</td></tr></tbody></table>');
$pdf->html('<table class="table-border"><tbody><tr><td>Goodbye!</td></tr></tbody></table>');
$pdf->html('<p>These two tables are not:</p>');
$pdf->html('<table class="table-border" ><tbody><tr><td>Hello</td></tr></tbody></table>');
$pdf->html('<table class="table-border"><tbody><tr><td>Goodbye!</td></tr></tbody></table>');
TCPDF doesn't support setting a margin with CSS, so you have to fake it with line-height :(

Related

Remove all height tags/attributes from table elements using preg_replace?

In our system, users often copy/paste tables from other sources, such as Excel/Word, which results in tables that have height tags or attributes in a number of places. I was thinking I could use pattern matching (preg_replace) to find and remove those instances as the inclusion of height specifications is causing issues in our system when this HTML is used by our PHP API to output formatted reports, but I've been trying to do so for the last 3 days without much success as I'm not adept at using regular expressions in this way.
I've read the documentation and examples on php.net and reviewed quite a few of the posts here on this topic, but I still don't know how to only apply the pattern matching to only instances within a tag, etc.
Also, how would I remove the entire tag if it only includes a height attribute, and then only the height attribute if other attributes are included?
Here is an example of the code I need to clean. This is just a small portion, as it typically will include multiple table elements similar to what's I've included below, along with images, text, etc.:
<table style="height:126px;" width="243">
<tbody>
<tr style="height: 18px;">
<td style="width: 38.5px; height: 18px;">ABC</td>
<td style="width: 41.5469px; height: 18px;">123</td>
<td style="width: 50.6562px; height: 18px;">DEF;</td>
<td style="width: 99.2969px; height: 18px;">456</td>
</tr>
<tr style="height:18px;">
<td style="width: 38.5px; height: 18px;">GHI</td>
<td style="width: 41.5469px; height: 18px;">789</td>
<td style="width: 50.6562px; height: 18px;">JKL</td>
<td style="width: 99.2969px; height: 18px;">012</td>
</tr>
<tr style="height:18px;">
<td style="width: 38.5px; height: 18px;">MNO</td>
<td style="width: 41.5469px; height: 18px;">345</td>
<td style="width: 50.6562px; height: 18px;">PQR</td>
<td style="width: 99.2969px; height: 18px;">678</td>
</tr>
</tbody>
</table>
Can this be done using preg_replace, or do I need to use a different technique? Any guidance or assistance would be greatly appreciated. A "cleaned" version of the above would look like this:
Cleaned
<table width="243">
<tbody>
<tr>
<td style="width: 38.5px;">ABC</td>
<td style="width: 41.5469px;">123</td>
<td style="width: 50.6562px;">DEF;</td>
<td style="width: 99.2969px;">456</td>
</tr>
<tr>
<td style="width: 38.5px;">GHI</td>
<td style="width: 41.5469px;">789</td>
<td style="width: 50.6562px;">JKL</td>
<td style="width: 99.2969px;">012</td>
</tr>
<tr>
<td style="width: 38.5px;">MNO</td>
<td style="width: 41.5469px;">345</td>
<td style="width: 50.6562px;">PQR</td>
<td style="width: 99.2969px;">678</td>
</tr>
</tbody>
</table>
Have you considered simply swapping the 'height:' style attribute with a non-existent one (ie: the DOM will ignore unknown tags); for example:
$str = '<table style="height:126px;" width="243">
<tbody>
<tr style="height: 18px;">
<td style="width: 38.5px; height: 18px;">ABC</td>
<td style="width: 41.5469px; height: 18px;">123</td>
<td style="width: 50.6562px; height: 18px;">DEF;</td>
<td style="width: 99.2969px; height: 18px;">456</td>
</tr>
<tr style="height:18px;">
<td style="width: 38.5px; height: 18px;">GHI</td>
<td style="width: 41.5469px; height: 18px;">789</td>
<td style="width: 50.6562px; height: 18px;">JKL</td>
<td style="width: 99.2969px; height: 18px;">012</td>
</tr>
<tr style="height:18px;">
<td style="width: 38.5px; height: 18px;">MNO</td>
<td style="width: 41.5469px; height: 18px;">345</td>
<td style="width: 50.6562px; height: 18px;">PQR</td>
<td style="width: 99.2969px; height: 18px;">678</td>
</tr>
</tbody>
</table>';
$str = str_replace("height:","nulled:",$str);
echo $str;
I took your table HTML, put it into a string variable and did a simple str_replace to swap all references to height: to nulled: which strips the height attribute out the string and when I echo the string back I get the cleaned table you put in your example.
There may be an even prettier approach, but this worked for me. :)

TCPDF manual page break

I am working on invoice printout.
I am facing a problem in printing the invoice. let's say there is an invoice that consist of items that are exactly in 1 page. The problem is there are subtotal, tax, and grandtotal after all the items.
In this case these 3 would be on the second page(new page), while the items are on 1st page.
I have done checking the row that one page can hold in tcpdf(after all my layout format, etc) and found out 1 page will consist of 28 items. This will work well if all the item consists of 1 row. However, if some of the items consist of 2 or more rows, the calculation will face a problem, and the whole structure will be a mess.
How can i automatically move some items to second page if the total items are 28 items, so the second page will not only consist of the subtotal, tax, and grand total?
here is my code
$count = 0;
$i = 0;
if(count($finalProduct)>0){
foreach($finalProduct as $product){
foreach($product['product'] as $prod){
if($prod['qty'] > 0){
/* check the row */
if($count >= 27){
$PDFCONTENT .= '<tr style="page-break-after:initial"><td colspan="6"></td></tr>';
$PDFCONTENT .= '<tr><td colspan="6"></td></tr>';
$count = 0;
}
$PDFCONTENT .= '
<tr nobr="true">';
$PDFCONTENT .= '<td align="center" width="7%">'.$prod['index_number'].'</td>';
</tr>';
}
$count++;
}
$PDFCONTENT .= '<tr><td></td></tr>';
}
}
$PDFCONTENT .= '<tfoot>
<tr> <td> </td> </tr>
<tr>
<th align="right" colspan="6">SUB TOTAL $:-</th>
<th style="border-top: 1px solid black;border-bottom: 1px solid black;" align="center">'.number_format($data['total'],2).'</th>
</tr>
<tr>
<th align="right" colspan="6">ADD '.number_format($data['transaction_tax_percentage']).'% $:-</th>
<th style="border-top: 1px solid black;border-bottom: 1px solid black;" align="center">'.number_format($data['transaction_tax_amount'],2).'</th>
</tr>
<tr>
<th align="right" colspan="6">TOTAL $:-</th>
<th style="border-top: 1px solid black;border-bottom: 1px solid black;" align="center">'.number_format($data['grand_total'],2).'</th>
</tr>
</tfoot>';
I remember that this was a real problem with tables. so we calculatet if there is a pagebreak to be set. If so, we closed the tabel, added a new site and reopened the table and put the rest of the table in it.
To force the pagebreak we used <br pagebreak="true"/>
if you don't close the table it leads to other problems.
A good threat is also here on SO: Manual Page Break in TCPDF

mPDF : text align with p , h1 - h6 in table not work

this code not work in table with mPDF php class
<table>
<tr>
<td class="contentDetails">
<td class="contentDetails">
<h3 style="text-align: right;"><strong>text align right</strong></h3>
<h3 style="text-align: center;"><strong>text align center</strong></h3>
<h3 style="text-align: left;"><strong>text align left</strong></h3>
</td>
</tr>
</table>
i triad use
.contentDetails > h3 {display: block;}
but not work and between td it's html from Editor tinymce
this full code from script
and when do output found content the td text align left not right or center
<?php
$html = '
<h1>mPDF</h1>
<table style="border-collapse: collapse;
font-size: 12px;
font-weight: 700;
margin-top: 5px;
border-top: 1px solid #777;
width: 100%;">
<tbody>
<tr>
<td class="contentDetails">
<h3 style="text-align: right;"><strong>text align right</strong></h3>
<h3 style="text-align: center;"><strong>text align center</strong></h3>
<h3 style="text-align: left;"><strong>text align left</strong></h3>
</td>
</tr>
</tbody>
</table>';
include("mpdf.php");
$mpdf=new mPDF('c');
$mpdf->WriteHTML($html);
$mpdf->Output();
exit;
?>
Try the below code. I think it will help you.
<table width="100%">
<tr>
<td class="contentDetails">
<th align="left"> <h3><strong>text align right</strong></h3></th>
<th align="center"> <h3><strong>text align center</strong></h3></th>
<th align="right"> <h3><strong>text align left</strong></h3></th>
</td>
</tr>
</table>
using extra tags in this.
I have prepared a lot of hours a content to print (mPdf v.6). Now i can give an advice to all. Don't use tables to align content or simply hold the content. Almost all css properties of elements work fine. But - but must not be nested in table.
The problem is the width of the container which is fitting with the content. Set table width to 100%
table {
width: 100%;
}
<table>
<tr>
<td class="contentDetails">
<h3 style="text-align: right;"><strong>text align right</strong></h3>
<h3 style="text-align: center;"><strong>text align center</strong></h3>
<h3 style="text-align: left;"><strong>text align left</strong></h3>
</td>
</tr>
</table>

dompdf generating PDF in with many blank pages between content

I'm using DOMPDF to generate pdf files from html. PDF file is getting generated but there are many blank pages are appearing in between content. Following is my php code.
<?php
$html=file_get_contents("views/testnew.html");
// echo $html;die();
$paper_orientation = 'landscape';
ob_start();
require_once("dompdf/dompdf_config.inc.php");
$pdfcontent = $html;
$dompdf = new DOMPDF();
$dompdf->set_paper('A4', $paper_orientation);
$dompdf->load_html($pdfcontent);
$dompdf->render();
// $pdf = $dompdf->output();
$pdf=$dompdf->stream("my_pdf.pdf", array("Attachment" => 0));
// file_put_contents('Brochure.pdf', $pdf);
?>
Following is my HTML that is I'm writing in PDF file
http://jsfiddle.net/6RmmB/
Since PDF is getting generated I don't think there is any problem with PHP code. Something must be wrong in html, But not able to figure it out what exactly ?
Or I'm missing something in PHP code ?
Content next to "Employer identification number" is appearing after about 8-9 blank pages
This looks to be due to a bug in how dompdf handles paging of table cells. If you remove the outer table, which appears to only exist to supply a border, the page will render better. You may still need to tweak the structure/styling, however, to get exactly what you want.
For example, instead of this:
<table width="100%" border="0" cellpadding="0" cellspacing="0" id="" style="border:1px solid #ccc;color: #000;font-family: Arial,Helvetica,sans-serif;font-size:14px;">
<tr>
<td>
<table width="100%" cellspacing="0" cellpadding="0" border="0" style="border-bottom:1px solid #ccc">
<tr>
<td width="15%" align="left" valign="top" style="border-right:1px solid #ccc;padding:10px;line-height:20px">Form
<img src="/var/www/html/pm5/bodytechniques/working/development/version5/therapist/images/w9-form.JPG" width="83" height="37" style="margin-left:15px" />
<br>(Rev. August 2013)
<br>Department of the Treasury
<br>Internal Revenue Service</td>
<td width="69%" align="left" valign="top" style="border-right:1px solid #ccc;padding:10px;text-align:center;line-height:45px">
<h1 align="center">Request for Taxpayer Identification Number and Certification</h1>
</td>
<td width="16%" align="left" valign="top">
<h3 style="line-height:25px;padding:10px">Give Form to the requester. Do not send to the IRS</h3>
</td>
</tr>
</table>
</td>
</tr>
</table>
Do this:
<div style="border:1px solid #ccc;color: #000;font-family: Arial,Helvetica,sans-serif;font-size:14px;">
<table width="100%" cellspacing="0" cellpadding="0" border="0" style="border-bottom:1px solid #ccc">
<tr>
<td width="15%" align="left" valign="top" style="border-right:1px solid #ccc;padding:10px;line-height:20px">Form
<img src="/var/www/html/pm5/bodytechniques/working/development/version5/therapist/images/w9-form.JPG" width="83" height="37" style="margin-left:15px" />
<br>(Rev. August 2013)
<br>Department of the Treasury
<br>Internal Revenue Service</td>
<td width="69%" align="left" valign="top" style="border-right:1px solid #ccc;padding:10px;text-align:center;line-height:45px">
<h1 align="center">Request for Taxpayer Identification Number and Certification</h1>
</td>
<td width="16%" align="left" valign="top">
<h3 style="line-height:25px;padding:10px">Give Form to the requester. Do not send to the IRS</h3>
</td>
</tr>
</table>
</div>
I have problem when multiple tables doesn't fit to one page.
Solution for this is easy, add <div style="clear: both;"> after table.

How to align image in table td, tcpdf

I use tcpdf to generate pdf files.
And there's a problem in aligning image in table td.
I am uising $pdf->writeHTML($html, true, false, true, false, ''); in tcpdf.
html is clear from the image i have shared.
html is just simple as
<table>
<tr>
<td>&nbsp</td>
<td>Picture</td>
.......
</tr>
<tr>
<td>1</td>
<td style="text-align:center"><img src="abc.jpg" width="20" height="20" ></td>
.......
</tr>
</table>
Note: align="center", valign="middle" , css margin and padding ... nothing seems to work I tried on
text-align:center push into center but not vertically.
Adding the following CSS to your td should work:
td {
vertical-align: middle;
text-align:center;
}
See a fiddle of it working.
Did you check image alignment?
<img align=center src="abc.jpg" width="20" height="20" >
This will work. As shown below, we can add a line break inside the <p> tag.
<table width="100%" style="border:1px solid #000;">
<tr>
<td style="text-align: center;">
<p><br/>
<img src="path/to/image" />
</p>
</td>
</tr>
</table>
you can use :
CSS:
td img{
margin:0 auto;
}
or use like #gayan

Categories