mpdf columns width is not getting fixed and increases with data - php

I am creating a pdf of 9 columns using mpdf that shows a list of users In each row, 1 column is used for photograph. My problem is that I want the columns to be almost equal in width but in result, I am getting columns with different widths.
I have tried to set CSS of table as well as through mpdf option, but none of them worked.
My code is as under:
$a='<style>#page {
margin: 10pt;
}</style>';
$a .= '<table autosize="1" border="1" class="atable" style="font-family:Arial"><tr>
<th style="min-width: 60pt;max-width: 60pt; font-size:7pt">Sr.<br>No.</th>
<th style="min-width: 60pt;max-width: 60pt; font-size:7pt">Admission No.<br>Boarder/DayScholar<br>Admit Class<br>Admit Date<br>Exit Class<br>Exit Date</th>
<th style="min-width: 60pt;max-width: 60pt; font-size:7pt">Photograph</th>
<th style="min-width: 60pt;max-width: 60pt; font-size:7pt">Student Details<br>[Student Name<br>Father Name<br>Father Occupation<br>Mother Name<br>Mother Occupation]</th>
<th style="min-width: 60pt;max-width: 60pt; font-size:7pt">Date of Birth<br>Current Class<br>Current City<br>Board Roll No.<br>Percentage</th>
<th style="min-width: 60pt;max-width: 70pt; font-size:7pt">Old Details<br>[Address<br>Father Email<br>Father Mobile]</th>
<th style="min-width: 60pt;max-width: 75pt; font-size:7pt">Present Profile<br>(Alumi,<br>Ex-Student,<br>Current Student)</th>
<th style="min-width: 60pt;max-width: 65pt; font-size:7pt">Contact Details<br>[Email Id<br>Mobile<br>Father Mobile<br>Facebook Id<br>Twitter Id<br>Linked In Id]</th>
<th style="min-width: 60pt;max-width: 65pt; font-size:7pt">Remarks</th>
</tr><tr>'
//Here goes the data fetching php code followed by the below code
<td style="font-size: 10pt !important; text-align:center;">' . ++$i . '</td>
<td style="font-size:10pt !important; text-align:center;">' . $row['adm_no'] .'<br><font color="green">' . $row['d_b'] . '</font><br>' . $row['adm_class'] . '<br><font color="green">' . $fadmdate . '</font><br>' . $row['exit_class'] . '<br><font color="green">' . $fexit . '</font></td>
<td style="font-size:10pt !important; text-align:center;">'. '<div id="testimage"><img src="' . $gdImage . '" width="60pt" /></div></td><td style="font-size:10pt !important; text-align:center;">'. $row['stu_name'] .
'<br><font color="green">' . $row['fat_name'] . '</font><br>' . $row['fat_occ'] . '<br><font color="green">' . $row['mot_name'] . '</font><br>' . $row['mot_occ'] . '</td>
<td style="font-size:10pt !important; text-align:center;">'.
$fdob . '<br><font color="green">' . $row['stu_class'] . '</font><br>' . $row['cur_city'] . '<br><font color="green">' .$row['board_roll'] . '</font><br>' . $row['perc'] . '</td>
<td style="font-size:10pt !important; text-align:center;">'.
$row['addr']. '<br><font color="green">' . $row['fat_email'] . '</font><br>' . $row['fat_mob'] . '</td>
<td style="font-size:10pt !important; text-align:center;">'.
$cprofile .'</td>
<td style="font-size:10pt !important; text-align:center;">' . $row['email_id'] .'<br><font color="green">' . $row['phone_no'] . '</font><br>' . $row['fb_id'] . '<br><font class="green">' . $row['twitter_id'] . '</font><br>' . $row['linkedin_id'] . '</td><td style="font-size:10pt !important; text-align:left;">SMS:<br><font color="green">Email:</font><br>FB:</td>
</tr></table>
<?php
include("\mpdf\mpdf.php");
$mpdf=new mPDF('','A4');
$keep_table_proportions = TRUE;
$mpdf->shrink_tables_to_fit=1;
$mpdf->setFooter('Details ::: ' . date("d/m/Y") . ' ::: {PAGENO} / {nb}');
$mpdf->WriteHTML($a);
$mpdf->Output('mytable.pdf', 'D');
exit;
Even after setting max width to 70pt, The Old details column takes around 25% of the page width due to which other columns are squeezed badly and look goes weird and also the footer is no more visible.

try the following :
<table cellpadding="5px" autosize="1" border="1" width="100%" style="overflow: wrap">
Then add appropriate widths to your th and td long with appropriate font size, say 8 to 10 pt.
Then, at the end, modify your code as follows:
include("\mpdf\mpdf.php");
$mpdf=new mPDF('','A4');
$mpdf->simpleTables = true;
$mpdf->packTableData = true;
$mpdf->keep_table_proportions = TRUE;
$mpdf->shrink_tables_to_fit=1;
$mpdf->setFooter('Birthday Details ::: ' . date("d/m/Y") . ' ::: {PAGENO} / {nb}');
$mpdf->WriteHTML($a);
$mpdf->Output('mytable.pdf', 'D');
exit;
This will lso improve the performance. Dont forget to upvote if this helps.

Related

replace string in the table

I have to check a string and replace if the attribute is equal to "Destaque:" in the case where the attribute matches it fails to print the line, but I'm confused with the functions, can someone help me?
<?php
class Produto_Atributo
{
function __construct()
{
}
function produtoAtributo($codigo)
{
Mage::app()->setCurrentStore(Mage_Core_Model_App::ADMIN_STORE_ID);
$banco_magento = Mage::getSingleton('core/resource')->getConnection('core_write');
$sql = "SELECT * FROM tw_produto_atributo where entity_id=" . $codigo;
$resultadoSql = $banco_magento->fetchAll($sql);
$count = 0;
foreach ($resultadoSql as $linha) {
$classe = '';
if ($count % 2 == 0) {
$classe = 'par';
}
else {
$classe = 'impar';
}
$_atributo = $this->recuperarAtributo($linha['attribute_id']);
$_optionText = $this->recuperarOptionid($_atributo, $linha['option_id']);
$descricao=$linha['ds_conteudo'] ;
$order = array("\n");
$replace = '<br />';
$newstr = str_replace($order, $replace, $descricao);
if (isset($linha['ds_conteudo']) && $linha['ds_conteudo'] != '') {
echo '<tr class="active '.$classe.'">
<td class="table-caracteristica-titulo" style="padding-left: 45px !important;padding-right: 45px !important; "> ' . ucwords( strtolower($_optionText)) . '</td>
<td class="table-caracteristica-descricao" style="padding-left: 45px !important;padding-right: 45px !important;"> ' . $newstr. '</td>
</tr>';
} else {
echo '<tr class="'.$classe.'">
<td class="table-caracteristica-titulo" style="padding-left: 45px !important;padding-right: 45px !important;">'if($_atributo->getFrontend_label()==!"Destaque:"){$_atributo->getFrontend_label();}else {'Loko'.$_atributo->getFrontend_label();} '</td>
<td class="table-caracteristica-descricao" style="padding-left: 45px !important;padding-right: 45px !important;"> ' . $_optionText . '</td>
</tr>';
}
$count = $count + 1;
}
}
function recuperarAtributo($atributo)
{
$cod = $atributo;
$attr = Mage::getModel('eav/entity_attribute')->getCollection()->addFieldToFilter('attribute_id', array('eq' => $cod))->getFirstItem();
return $attr;
}
function recuperarOptionid($atributo, $option_id)
{
return ($atributo->getSource()->getOptionText($option_id));
}
}
already got hahah
was very simple, here's the answer:
if (isset($linha['ds_conteudo']) && $linha['ds_conteudo'] != '') {
echo '<tr class="active '.$classe.'">
<td class="table-caracteristica-titulo" style="padding-left: 45px !important;padding-right: 45px !important; "> ' . ucwords( strtolower($_optionText)) . '</td>
<td class="table-caracteristica-descricao" style="padding-left: 45px !important;padding-right: 45px !important;"> ' . $newstr. '</td>
</tr>';
} else {
if($_atributo->getFrontend_label()<>"Destaque:"){
echo '<tr class="'.$classe.'">
<td class="table-caracteristica-titulo" style="padding-left: 45px !important;padding-right: 45px !important;">' . $_atributo->getFrontend_label() . '</td>
<td class="table-caracteristica-descricao" style="padding-left: 45px !important;padding-right: 45px !important;"> ' . $_optionText . '</td>
</tr>';
}
}

Can not insert image in table <td> cell

I am trying to write a shopping cart for my site and I want to display a table with a row for each item, including the part #, name, quantity, price and a small image. Everything displays fine except for the image, which appears outside (above) the table and not in the cell where I've placed it. Here's the code:
//create a table for the items in the cart
print("<table class='cart_table'>");
print("<tr class='first_row'>
<td> Item No. </td>
<td> Item Name </td>
<td> Quantity </td>
<td> Price </td>
<td> Image </td>
</tr>");
//for each item in the cart
foreach ($_SESSION['cart'] as $value) {
//create a new product object for the item
$product = new LG_Product($_SESSION['cart'][$i]['item']);
print("<tr>");
print("<td> " . $product->itemID . " </td>");
print("<td> " . $product->productName . " </td>");
print("<td> " . $_SESSION['cart'][$i++]['qty'] . " </td>");
print("<td> $" . $product->productPrice . " </td>" );
print("<td>" . $product->showPrimaryImage('small') . "</td>");
print("</tr>");
}
//end the table
print("</table>");
And here's the function that gets the image:
public function showPrimaryImage($variant) {
if($variant == "medium") {
print ("<img style = \" max-width:400px;
max-height:400px;
display: block;
margin-left: auto;
margin-right: auto;\"
src=\"" . WEBSITE_URL . PATH_DYNAMIC . "/products_" . $this->productNumber . "_1_" . $variant . ".jpg\"
alt=\"" . $this->productName . "\">
");
}
else if($variant == "large") {
print ("<img style = \" max-width:600px;
max-height:600px;
display: block;
margin-left: auto;
margin-right: auto;\"
src=\"". WEBSITE_URL . PATH_DYNAMIC . "/products_" . $this->productNumber . "_1_" . $variant . ".jpg\"
alt=\"" . $this->productName . "\">
");
}
else if($variant == "small") {
print ("<img style = \" max-width:100px;
max-height:100px;
display: block;
margin-left: auto;
margin-right: auto;\"
src=\"". WEBSITE_URL . PATH_DYNAMIC . "/products_" . $this->productNumber . "_1_" . $variant . ".jpg\"
alt=\"" . $this->productName . "\">
");
}
}//end function showPrimaryImage()
When I view the source in the browser, it appears the code that renders the image is not inside the pair. When I view the red warning in the browser source, it says:
start tag "img" seen in "table"
Here's a screen capture of where the image appears:
And here's the source that's output from my PHP code (when viewed in the browser, the entire tag statement is in red):
<table class='cart_table'>
<tr class='first_row'>
<td> Item No. </td>
<td> Item Name </td>
<td> Quantity </td>
<td> Price </td>
<td> Image </td>
</tr>
<tr>
<td> BQ2819 </td>
<td> A WWII Period U.S. Theater-made Fighting Knife with Sheath </td>
<td> 1 </td>
<td> $270.00 </td>
<img style = " max-width:100px;
max-height:100px;
max-height:100px;
display: block;
margin-left: auto;
margin-right: auto;"
src="http://www.mywebsite.com/dynamic/products_2056_1_small.jpg"
alt="A WWII Period U.S. Theater-made Fighting Knife with Sheath">
<td></td>
</tr>
<tr>
<td> BQ2819 </td>
<td> A WWII Period U.S. Theater-made Fighting Knife with Sheath </td>
<td> 1 </td>
<td> $270.00 </td>
<img style = " max-width:100px;
max-height:100px;
display: block;
margin-left: auto;
margin-right: auto;"
src="http://www.mywebsite.com/dynamic/products_2056_1_small.jpg"
alt="A WWII Period U.S. Theater-made Fighting Knife with Sheath">
<td></td>
</tr>
</table>
For the line:
print("<td>" . $product->showPrimaryImage('small') . "</td>");
You have a string concatenation, which means you will concatenate whatever the return value of the call to showPrimaryImage is. However, showPrimaryImage doesn't return anything. Therefore the program first outputs the image's html (from the showPrimaryImage call) and then outputs <td></td> from the outer print.
You need to change print to a return statement for your showPrimaryImage function
function showPrimaryImage($variant) {
if($variant == "medium") {
return img;
} else if() {
//...
}
}

PHP / HTML / CSS Table scroll

I'm trying to implement my table with a scroll bar that will only how about 10 teams until it forces you to scroll down. However, I can't seem to get the coding right.
I'm planning on implementing the scrollable table across several different pages, in which the column headers will change in width. All the examples I have found required a fixed width for each column, is there a better way to do this?
** I want to keep fixed headers **
Here is my PHP code:
echo '<table align="center" cellspacing="3" cellpadding="3" width="80%" border="1">
<tr>
<th align="center">Team Name</th>
<th align="center">Wins</th>
<th align="center">Losses</th>
<th align="center">Winning %</th>
</tr><div id="scrll_tbl">';
$bg = '#eeeeee';
for($i = 0; $i < count($teams); $i++) {
$bg = ($bg == '#eeeeee' ? '#fffffff' : '#eeeeee');
$wnpctg = $teams[$i][1] / ($teams[$i][1] + $teams[$i][2]);
echo '<tr bgcolor="' .$bg. '">
<td align="center">' . $teams[$i][0] . '</td>
<td align="center">' . $teams[$i][1] . '</td>
<td align="center">' . $teams[$i][2] . '</td>
<td align="center">' . number_format($wnpctg,3) . '</td>
</tr>';
}
echo '</div></table>';
and here is my CSS:
#scrll_tbl {
overflow: scroll;
height: 100px;
}
You can't actually have a div tag inside a table (it will be rendered outside the table as an empty div). The only way to have a fixed header is to have two tables and the second table inside a scrollable div. If you want a flexible width for table cells, you can use JavaScript to check the width of the first row table cells of your data and set the header cell width accordingly.

Failed to load pdf, dompdf error -> PDF.js v0.8.787 (build: 58407cf) Message: InvalidPDFException

Following is my code to display a pdf using dompdf but the error was failed to load pdf
PDF.js v0.8.787 (build: 58407cf)
Message: InvalidPDFException
table name is acad_act and database name is pbas_db. Details of database connection is stored in db_connect.php. Any help would be appreciable
<?php
include('db_connect.php');
require 'dompdf/dompdf_config.inc.php';
$sqlinv="SELECT * FROM acad_act";
$res_inv = mysql_query($sqlinv) or die(mysql_error());
while ($sql3=mysql_fetch_array($res_inv)) {
$inv_id = $sql3['User_id'];
$date = $sql3['Year'];
$cname = $sql3['Gen_Info_AQ'];
$subtotal = $sql3['Gen_Info_NOC'];
$disc = $sql3['Gen_Info_Place'];
$subtotal2 = $sql3['Gen_Info_Duration'];
$tax = $sql3['Gen_Info_SA'];
$gtotal = $sql3['Gen_Info_Aqyear'];
}
$dompdf= new DOMPDF();
$html .="<html>
<head>
<style>
body {
font-family: arial;
font-size: 13px;
}
table {
width: 100%;
border-collapse: collapse;
}
th {
font-weight: bold;
font-size: 14px;
border: 1px solid #333333;
}
tr {
width: 100%;
}
td {
border: 1px solid #333333;
text-align: center;
padding: 10px;
font-weight: normal!important;
}
</style>
</head><body>";
$html .="
<table>
<thead>
<tr>
<th class=''>Sr.</th>
<th class=''>Particulars</th>
<th class=''>Size</th>
<th class=''>Quantity / Insertion</th>
<th class=''>Rate</th>
<th class=''>Amount</th>
<th class=''>Amount 2</th>
<th class=''>Amount 3</th>
</tr>
</thead>
<tbody>
<tr>
<td>" . $inv_id . "</td>
<td>" . $date . "</td>
<td>" . $cname . "</td>
<td>" . $subtotal . "</td>
<td>" . $disc . "</td>
<td>" . $subtotal2 . "</td>
<td>" . $tax . "</td>
<td>" . $gtotal . "</td>
</tr>
</tbody>
</table>
</body>
</html>";
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("sample.pdf");
exit(0);
?>
The exception is due to pdf.js plugin installed in the browser. Instead of rendering the error the pdfjs shows an exception which doesn't display the error. Just remove the pdfjs plugin than you will be able to see the required error.

Border around specific table row

I am trying to create a border around the top row ONLY! Currently, a border will be displayed only around the outside of the table. But I also want a border around the first row. Can someone help me do this? I want the row with 'Team, Correct Picks, and Points' to have a border around it.
<body>
<?=$leaguename?>
<center><table cellspacing="0" style="width:400px; border:1px solid gray">
<?
echo "<tr border=\"1\"><td> Team </td><td>Correct Picks</td><td>Points</td></tr>";
while($row = mysql_fetch_array($memberslist)) {
if ($row['User_ID'] == $id) {
echo "<tr style=\"border:1px solid gray\" bgcolor=\"gray\"><td>" . $row['User_ID'] . "</td><td><b>" . $row['Correct_Picks'] . " </b> /" . $maxcorrectpicks . "</td><td>" . $row['Points'] . "</td></tr>";
} else {
echo "<tr><td>" . $row['User_ID'] . "</td><td><b>" . $row['Correct_Picks'] . " </b> /" . $maxcorrectpicks . "</td><td>" . $row['Points'] . "</td></tr>";
}
}
?>
</table></center>
</body>
This is presentational, so mixing it into the PHP is a bad idea. Instead, use CSS:
tr:first-child td { border-top: 1px solid black; border-bottom: 1px solid black; }
tr:first-child td:first-child { border-left:1px solid black; }
tr:first-child td:last-child { border-right:1px solid black; }
IE7+8 support for the *-child selectors can be a bit buggy. If it's not working in those browsers, consider using JavaScript polyfills.
You may also want to put the headers inside <thead> and <th> elements to make the <tr> with data the first row in a <tbody> element.
e.g.
<table>
<thead>
<tr>
<th>Team</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
</tr>
</tbody>
</table>

Categories