PHP to PDF from MySQL Database -
i have read , tried multiple tutorials , each time run block cannot pass.
i have database 59 fields currently, have form agents fills in sales each day. each day 15-50 sales captured. have html template have populated variables of database fields , run through loop displaying sales each in separate table, print pdf each sale table on separate page. can imagine how labour intensive every day.
i trying convert each loop run record of sale own pdf , name pdf file according mix of variables database table.
i cannot php variables work in pdf generator. can display data in table, , generate pdf off template, add php mysql database field variables keep getting errors variables , pdf gereator fails.
here have done:
tcpdf:
<?php $pdf = new tcpdf('l', pdf_unit, 'a4', true, 'utf-8', false); // set document information $pdf->setcreator(pdf_creator); $pdf->setauthor('asimegroup'); $pdf->settitle('asime loan app'); $pdf->setsubject('asime loan application form'); // set default monospaced font $pdf->setdefaultmonospacedfont(pdf_font_monospaced); // set margins $pdf->setmargins('5', '0', '0'); // set auto page breaks $pdf->setautopagebreak(true, '0'); // set image scale factor $pdf->setimagescale(pdf_image_scale_ratio); // set language-dependent strings (optional) if (@file_exists(dirname(__file__).'examples\lang\eng.php')) { require_once(dirname(__file__).'examples\lang\eng.php'); $pdf->setlanguagearray($l); } // set font $pdf->setfont('helvetica', '', 10); // add page $pdf->addpage(); // including 2 rows here example sake. $html = ' <body> <br/> <table> <tr> <td height="40px"></td> </tr> <tr> <td align="right" class="heading" width="175px">personal</td> <td align="left" class="heading" width="210px">details:</td> <td width="10px"></td> <td align="right" class="heading" width="150px">relative not</td> <td align="left" class="heading" width="170px">living you:</td> <td width="10px"></td> <td align="right" class="heading" width="185px">expenses:</td> <td width="100px"></td> </tr> <tr> <td class="subheading">sa citizen?:</td> <td class="data"></td> <td></td> <td class="subheading">name:</td> <td class="data"></td> <td></td> <td class="subheading">bond / rent:</td> <td class="data"></td> </tr> </table>'; // output html content $pdf->writehtml($html, true, false, true, false, ''); // reset pointer last page $pdf->lastpage(); // --------------------------------------------------------- //close , output pdf document $pdf->output('example_061.pdf', 'f'); ?>
php database loop:
<?php // server info $server = 'localhost'; $user = 'root'; $pass = ''; $db = 'debtdb'; // connect database $conn = new mysqli($server, $user, $pass, $db); // show errors (remove line if on live site) mysqli_report(mysqli_report_error); if(!isset($_post['agentname'])) { $search_sql="select * daily"; $search_query=mysqli_query($conn, $search_sql); $search_rs=mysqli_fetch_assoc($search_query); } if(!empty($search_query)) { while($search_rs = mysqli_fetch_array($search_query)) { ?> <body> // including 2 rows here example sake. <table> <tr> <td align="right" class="heading" width="175px">personal</td> <td align="left" class="heading" width="210px">details:</td> <td width="10px"></td> <td align="right" class="heading" width="150px">relative not</td> <td align="left" class="heading" width="170px">living you:</td> <td width="10px"></td> <td align="right" class="heading" width="185px">expenses:</td> <td width="100px"></td> </tr> <tr> <td class="subheading">sa citizen?:</td> <td class="data"><?php echo $search_rs["per_citizen"]; ?></td> <td></td> <td class="subheading">name:</td> <td class="data"><?php echo $search_rs["rel_name"]; ?></td> <td></td> <td class="subheading">bond / rent:</td> <td class="data">r <?php echo $search_rs["exp_bondrent"]; ?></td> </tr> </table> <?php } } ?>
each of these 2 examples works. following got stuck when merge 2:
<?php // server info $server = 'localhost'; $user = 'root'; $pass = ''; $db = 'debtdb'; // connect database $conn = new mysqli($server, $user, $pass, $db); // show errors (remove line if on live site) mysqli_report(mysqli_report_error); if(!isset($_post['agentname'])) { $search_sql="select * daily"; $search_query=mysqli_query($conn, $search_sql); $search_rs=mysqli_fetch_assoc($search_query); } if(!empty($search_query)) { while($search_rs = mysqli_fetch_array($search_query)) { $pdf = new tcpdf('l', pdf_unit, 'a4', true, 'utf-8', false); // set document information $pdf->setcreator(pdf_creator); $pdf->setauthor('asimegroup'); $pdf->settitle('asime loan app'); $pdf->setsubject('asime loan application form'); // set default monospaced font $pdf->setdefaultmonospacedfont(pdf_font_monospaced); // set margins $pdf->setmargins('5', '0', '0'); // set auto page breaks $pdf->setautopagebreak(true, '0'); // set image scale factor $pdf->setimagescale(pdf_image_scale_ratio); // set language-dependent strings (optional) if (@file_exists(dirname(__file__).'examples\lang\eng.php')) { require_once(dirname(__file__).'examples\lang\eng.php'); $pdf->setlanguagearray($l); } // set font $pdf->setfont('helvetica', '', 10); // add page $pdf->addpage(); // including 2 rows here example sake. $html = ' <table> <tr> <td align="right" class="heading" width="175px">personal</td> <td align="left" class="heading" width="210px">details:</td> <td width="10px"></td> <td align="right" class="heading" width="150px">relative not</td> <td align="left" class="heading" width="170px">living you:</td> <td width="10px"></td> <td align="right" class="heading" width="185px">expenses:</td> <td width="100px"></td> </tr> <tr> <td class="subheading">sa citizen?:</td> <td class="data"><?php echo $search_rs["per_citizen"]; ?></td> <td></td> <td class="subheading">name:</td> <td class="data"><?php echo $search_rs["rel_name"]; ?></td> <td></td> <td class="subheading">bond / rent:</td> <td class="data">r <?php echo $search_rs["exp_bondrent"]; ?></td> </tr> </table>'; // output html content $pdf->writehtml($html, true, false, true, false, ''); // reset pointer last page $pdf->lastpage(); // --------------------------------------------------------- //close , output pdf document $pdf->output('<?php echo $search_rs["agentname"]; ?> - <?php echo $search_rs["dateofsale"]; ?>.pdf', 'f'); } } ?>
gives me :
the localhost page isn’t working
localhost unable handle request. http error 500
you have put line:
$pdf = new tcpdf('l', pdf_unit, 'a4', true, 'utf-8', false);
in mysql while loop, means creating new pdf each sql result, probebly not want. browser complain cause cannot handle request multiple pdf files. put stuff needs run once, creating pdf variable , author stuff etc, above while loop...
Comments
Post a Comment