Aswin Posted April 20, 2021 Share Posted April 20, 2021 Guys will you help me by Creating multiple pdf using single command using tcpdf in php it will most helpful for me Quote Link to comment https://forums.phpfreaks.com/topic/312515-multiple-pdf/ Share on other sites More sharing options...
Barand Posted April 20, 2021 Share Posted April 20, 2021 (edited) 13 minutes ago, Aswin said: Guys will you help me Yes 13 minutes ago, Aswin said: by Creating multiple pdf using single command using tcpdf in php No. If you want to pay someone to do it for you, post in the "job Offerings" forum. Otherwise, post the code you have already and tell us where you are having probems so we can help you with your coding. Edited April 20, 2021 by Barand 1 Quote Link to comment https://forums.phpfreaks.com/topic/312515-multiple-pdf/#findComment-1585982 Share on other sites More sharing options...
Aswin Posted April 20, 2021 Author Share Posted April 20, 2021 <?php function fetch_data(){ $conn=mysqli_connect("localhost","root","","pdf"); $output=''; $sql="SELECT * FROM tables"; $result=mysqli_query($conn,$sql); $sno=0; while($row=mysqli_fetch_array($result)){ $output.= '<tr> <td>' .$row["product_id"].'</td> <td>' .$row["product_name"].'</td> </tr>'; } return $output; } // if(isset($_POST["createpdf"])){ require_once('tcpdf_min/tcpdf.php'); $obj_pdf= new TCPDF('P',PDF_UNIT,PDF_PAGE_FORMAT,true, 'UTF-8',false); $obj_pdf->SetCreator(PDF_CREATOR); $obj_pdf-> SetTitle("Product PDF"); $obj_pdf->SetHeaderData('', '',PDF_HEADER_TITLE,PDF_HEADER_STRING); $obj_pdf->SetHeaderFont(Array(PDF_FONT_NAME_MAIN, '',PDF_FONT_SIZE_MAIN)); $obj_pdf->SetHeaderFont(Array(PDF_FONT_NAME_DATA, '',PDF_FONT_SIZE_DATA)); $obj_pdf->SetDefaultMonospacedFont('helvetica'); $obj_pdf->SetFooterMargin(PDF_MARGIN_FOOTER); $obj_pdf->SetMargins(PDF_MARGIN_LEFT, '5' ,PDF_MARGIN_RIGHT); $obj_pdf->SetPrintHeader(false); $obj_pdf->SetPrintFooter(false); $obj_pdf->SetAutoPageBreak(TRUE,10); $obj_pdf -> SetFont('helvetica', '',12); $obj_pdf->AddPage(); $content=''; $content .='<h4 align="center">Generate Pdf</h4><br> <table border="1" cellspacing="0" cellpadding="3"> <tr> <th>Id</th> <th>Name</th> </tr>'; $content .= fetch_data(); $content .= '</table>'; $obj_pdf->writeHTML($content); $obj_pdf->Output('Sample Pdf','I'); //} ?> <!DOCTYPE html> <!DOCTYPE html> <html> <head> <title>Pdf Generation</title> <link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"> </head> <body> <br> <!-- <h4 align="center">Pdf Output</h4> --> <table> <tr> <th>Id</th> <th>Name</th> </tr> <?php echo fetch_data(); ?> </table> <form method="post"> <input type="submit" name="createpdf" value="Generate Pdf"> </form> </body> </html> This is the code which am using here i have to create pdf for each product will u help me Quote Link to comment https://forums.phpfreaks.com/topic/312515-multiple-pdf/#findComment-1585983 Share on other sites More sharing options...
Solution Barand Posted April 20, 2021 Solution Share Posted April 20, 2021 It doesn't let you create a PDF once output has already been sent, therefore creating multiple pdfs in a single script doesn't appear to be an option. As an alternative I would suggest give users the option to select each product and create their pdfs separately, or create one pdf but put each product on a separate page Also, I prefer to put my pdf code in a separate file and create the pdf via a "create PDF" link and give the user the option of viewing or downloading the PDF. This version gives both the above options aswin_1.php <?php // // // CREATE YOUR OWN MYSQL CONNECTION HERE // // // function fetch_data($conn){ $output=''; $sql="SELECT * FROM product"; $result=mysqli_query($conn,$sql); $sno=0; while($row=mysqli_fetch_array($result)){ $output.= '<tr> <td><a href="aswin_2.php?product=' . $row["product_id"] . '">' . $row["product_id"] . '</a></td> <td>' .$row["product_name"].'</td> </tr>'; } return $output; } ?> <!DOCTYPE html> <html> <head> <title>Pdf Generation</title> <link rel="stylesheet" type="text/css" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"> </head> <body> <br> <table style='width:400px; margin: 50px auto;'> <tr> <th>Id</th> <th>Name</th> </tr> <?php echo fetch_data($conn); ?> </table> <div style='width: 400px; margin: 20px auto;'> <a href="aswin_2.php?product=all" download>All products</a> </body> </html> aswin_2.php (creates the pdf) <?php // // // CREATE YOUR OWN MYSQL CONNECTION HERE // // // require_once '../tcpdf/tcpdf.php'; if (!isset($_GET['product'])) { exit; } if ($_GET['product']=='all') { $stmt = $conn->query("SELECT product_id , product_name FROM product ORDER BY product_id "); $obj_pdf= new TCPDF('P',PDF_UNIT,PDF_PAGE_FORMAT,true, 'UTF-8',false); $obj_pdf->SetCreator(PDF_CREATOR); $obj_pdf-> SetTitle("Product PDF"); $obj_pdf->SetHeaderData('', '',PDF_HEADER_TITLE,PDF_HEADER_STRING); $obj_pdf->SetHeaderFont(Array(PDF_FONT_NAME_MAIN, '',PDF_FONT_SIZE_MAIN)); $obj_pdf->SetHeaderFont(Array(PDF_FONT_NAME_DATA, '',PDF_FONT_SIZE_DATA)); $obj_pdf->SetDefaultMonospacedFont('helvetica'); $obj_pdf->SetFooterMargin(PDF_MARGIN_FOOTER); $obj_pdf->SetMargins(PDF_MARGIN_LEFT, '5' ,PDF_MARGIN_RIGHT); $obj_pdf->SetPrintHeader(false); $obj_pdf->SetPrintFooter(false); $obj_pdf->SetAutoPageBreak(TRUE,10); $obj_pdf -> SetFont('helvetica', '',12); foreach ($stmt as $row) { $obj_pdf->AddPage(); $content=''; $content .='<h4 align="center">Generate Pdf</h4><br> <table border="1" cellspacing="0" cellpadding="3"> <tr> <th>Id</th> <th>Name</th> </tr>'; $content .= "<tr> <td>{$row['product_id']}</td> <td>{$row['product_name']}</td> </tr> "; $content .= '</table>'; $obj_pdf->writeHTML($content); } $obj_pdf->Output('All Products','D'); } else { $stmt = $conn->prepare("SELECT product_id , product_name FROM product WHERE product_id = ? "); $stmt->bind_param('s', $_GET['product']); $stmt->execute(); $stmt->bind_result($product_id, $product_name); $stmt->fetch(); $obj_pdf= new TCPDF('P',PDF_UNIT,PDF_PAGE_FORMAT,true, 'UTF-8',false); $obj_pdf->SetCreator(PDF_CREATOR); $obj_pdf-> SetTitle("Product PDF"); $obj_pdf->SetHeaderData('', '',PDF_HEADER_TITLE,PDF_HEADER_STRING); $obj_pdf->SetHeaderFont(Array(PDF_FONT_NAME_MAIN, '',PDF_FONT_SIZE_MAIN)); $obj_pdf->SetHeaderFont(Array(PDF_FONT_NAME_DATA, '',PDF_FONT_SIZE_DATA)); $obj_pdf->SetDefaultMonospacedFont('helvetica'); $obj_pdf->SetFooterMargin(PDF_MARGIN_FOOTER); $obj_pdf->SetMargins(PDF_MARGIN_LEFT, '5' ,PDF_MARGIN_RIGHT); $obj_pdf->SetPrintHeader(false); $obj_pdf->SetPrintFooter(false); $obj_pdf->SetAutoPageBreak(TRUE,10); $obj_pdf -> SetFont('helvetica', '',12); $obj_pdf->AddPage(); $content=''; $content .='<h4 align="center">Generate Pdf</h4><br> <table border="1" cellspacing="0" cellpadding="3"> <tr> <th>Id</th> <th>Name</th> </tr>'; $content .= "<tr> <td>$product_id</td> <td>$product_name</td> </tr> "; $content .= '</table>'; $obj_pdf->writeHTML($content); $obj_pdf->Output("Product $product_id",'D'); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/312515-multiple-pdf/#findComment-1585986 Share on other sites More sharing options...
Aswin Posted April 20, 2021 Author Share Posted April 20, 2021 Thank you for your great support. i will try the best. and once again thank u so much Quote Link to comment https://forums.phpfreaks.com/topic/312515-multiple-pdf/#findComment-1585987 Share on other sites More sharing options...
Aswin Posted April 20, 2021 Author Share Posted April 20, 2021 but a small problem while am clicking all products it was downloading even am changed into D Quote Link to comment https://forums.phpfreaks.com/topic/312515-multiple-pdf/#findComment-1585990 Share on other sites More sharing options...
Aswin Posted April 20, 2021 Author Share Posted April 20, 2021 D to I Quote Link to comment https://forums.phpfreaks.com/topic/312515-multiple-pdf/#findComment-1585991 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.