0

I currently have a homepage set up with a table including array query for fetching table row's of data, I've tested PhpToPDF and TCPDF but i've ran in to the issue of me not knowing what to do, my array limiting my options of sending data accross but the array is reliant on when user log's in, that their login username gives the query the information criteria required to fetch from db. I've also come accross the issue of the webpage being hosted on localhost. Can anyone demonstrate how to convert the table onto a pdf document when Create PDF is clicked? Prefabably free solutions

$query = "select * from orders where userid= ' $userid' order by orderid";

$result = mysqli_query($db,$query);
if ($result){
    while ($rows = mysqli_fetch_array($result))
    {
?>

<tr>
     <td> <?php echo $rows[4]; ?> </td>
     <td> <?php echo $rows[0]; ?>  </td>
     <td> <?php echo $rows[1]; ?> </td>
     <td> <?php echo $rows[2]; ?>  </td>
     <td> <?php echo $rows[3]; ?> </td>                       
     <td>
      <a href ="deleteorderhomepage.php?receiptid=<?php echo $rows[3]?>&orderid=<?php echo $rows[0]?>">Delete </a>
     </td>
</tr>
<?php
    if(isset($_POST["create_pdf"])) {  }
        }
    }
 ?

</table>
    </br>
        <input type="submit" name="create_pdf"  value="Create PDF" />                
    </div>
    </div>             
</body>
</html>
4
  • If you just wanted to convert html table to pdf, better to use datatables [datatables.net/] Commented Aug 18, 2018 at 3:47
  • @LahiruTM how do i go about doing this? i'm trying to look up a tutorial Commented Aug 18, 2018 at 3:56
  • github.com/KnpLabs/snappy is easy you would simply just do a callback to your page, also you could use javascript based tools but imo they don't work that well. Commented Aug 18, 2018 at 4:10
  • @Reached_The_Apex Use my below answer as an example. It is very easy client side plugin. You can generate table from PHP and add those Copy, Excel, Pdf & Print from Client side using Datatables Plugin. Not only that, you can have many more features like sort and order ... Commented Aug 18, 2018 at 5:39

2 Answers 2

0

You this very easy plugin: Datatables

<html>
<head>
<title>Datatables Exmple by LahiruTM</title>

<link href="https://cdn.datatables.net/1.10.19/css/jquery.dataTables.min.css" rel="stylesheet"/>
<link href="https://cdn.datatables.net/buttons/1.5.2/css/buttons.dataTables.min.css" rel="stylesheet"/>


<script src="https://code.jquery.com/jquery-3.3.1.js"></script>
<script src="https://cdn.datatables.net/1.10.19/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/buttons/1.5.2/js/dataTables.buttons.min.js"></script>
<script src="https://cdn.datatables.net/buttons/1.5.2/js/buttons.flash.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jszip/3.1.3/jszip.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.36/pdfmake.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pdfmake/0.1.36/vfs_fonts.js"></script>
<script src="https://cdn.datatables.net/buttons/1.5.2/js/buttons.html5.min.js"></script>
<script src="https://cdn.datatables.net/buttons/1.5.2/js/buttons.print.min.js"></script>


<script>
$(document).ready(function() {
    $('#example').DataTable( {
    dom: 'Bfrtip',
    buttons: [
        { extend:'copy', attr: { id: 'allan' } }, 'csv', 'excel', 'pdf', 'print'
    ]
    } );
} );
</script>
</head>
<body>
<table id="example" class="display" style="width:100%">
    <thead>
        <tr>
            <th>Name</th>
            <th>Position</th>
            <th>Office</th>
            <th>Age</th>
            <th>Start date</th>
            <th>Salary</th>
        </tr>
    </thead>
    <tbody>
        <tr>
            <td>Tiger Nixon</td>
            <td>System Architect</td>
            <td>Edinburgh</td>
            <td>61</td>
            <td>2011/04/25</td>
            <td>$320,800</td>
        </tr>
        <tr>
            <td>Garrett Winters</td>
            <td>Accountant</td>
            <td>Tokyo</td>
            <td>63</td>
            <td>2011/07/25</td>
            <td>$170,750</td>
        </tr>
        <tr>
            <td>Ashton Cox</td>
            <td>Junior Technical Author</td>
            <td>San Francisco</td>
            <td>66</td>
            <td>2009/01/12</td>
            <td>$86,000</td>
        </tr>
        <tr>
            <td>Cedric Kelly</td>
            <td>Senior Javascript Developer</td>
            <td>Edinburgh</td>
            <td>22</td>
            <td>2012/03/29</td>
            <td>$433,060</td>
        </tr>
        <tr>
            <td>Airi Satou</td>
            <td>Accountant</td>
            <td>Tokyo</td>
            <td>33</td>
            <td>2008/11/28</td>
            <td>$162,700</td>
        </tr>
        <tr>
            <td>Brielle Williamson</td>
            <td>Integration Specialist</td>
            <td>New York</td>
            <td>61</td>
            <td>2012/12/02</td>
            <td>$372,000</td>
        </tr>
    </tbody>
    <tfoot>
        <tr>
            <th>Name</th>
            <th>Position</th>
            <th>Office</th>
            <th>Age</th>
            <th>Start date</th>
            <th>Salary</th>
        </tr>
    </tfoot>
    </table>
</body>
</html>
Sign up to request clarification or add additional context in comments.

4 Comments

will i be able to use mysqli_query to fetch rows? i'll need a dynamic table produced in pdf, as each user will have 1 or many orders (as in your example the table is populated with default values)
Yes, You can generate table dynamically, Only thing is to add the same id to the table specified in the JavaScript section.
Hey there, sorry for the late response, went to bed. I've attempted using this demo and added in mysqli_query and used php rows, but when producing the table, the expected results are produced, but when using the buttons (Print/Copy/PDF) only the first row is being applied.
I've gotten my solution by messing around abit, the "<tbody> and </tbody>" Tags were limiting the fetch from being read by the plugin's. Meaning when i removed both tags and left It worked as expected. Thank you very much
0

Use one of these libraries commonly this http://www.tcpdf.org/:

  1. FPDF

  2. TCPDF

  3. EzPDF

it's an example from TCPDF pdf library and link export mysql database table contents on to a PDF file using php:

require('fpdf17/fpdf.php');
$pdf=new FPDF();
$pdf->AddPage();
$pdf->SetFont('Arial','B',10);
$pdf->Ln();
$pdf->Ln();
$pdf->SetFont('times','B',10);
$pdf->Cell(25,7,"Stud ID");
$pdf->Cell(30,7,"Student Name");
$pdf->Cell(40,7,"Address");
$pdf->Cell(30,7,"Class");
$pdf->Cell(30,7,"Phone No");
$pdf->Cell(30,7,"E-mail");
$pdf->Ln();
$pdf->Cell(450,7,"----------------------------------------------------------------------------------------------------------------------------------------------------------------------");
$pdf->Ln();

        include ('db.php');
        $sql = "SELECT studid,name,address,class,phone,email FROM studinfo";
        $result = mysql_query($sql);

        while($rows=mysql_fetch_array($result))
        {
            $studid = $rows[0];
            $name = $rows[1];
            $address = $rows[2];
            $class = $rows[3];
            $phone = $rows[4];
            $email = $rows[5];
            $pdf->Cell(25,7,$studid);
            $pdf->Cell(30,7,$name);
            $pdf->Cell(40,7,$address);
            $pdf->Cell(30,7,$class);
            $pdf->Cell(30,7,$phone);
            $pdf->Cell(30,7,$email); 
            $pdf->Ln(); 
        }
$pdf->Output();

3 Comments

eek mysql_query?
it doesn't matter you can use your own query on any PHP framework.
Eh, Fatal error: Uncaught exception 'Exception' with message 'FPDF error: Some data has already been output, can't send PDF file' in C:\wamp64\www\NaturesZone\pages\fpdf181\fpdf.php on line 271 ( ! ) Exception: FPDF error: Some data has already been output, can't send PDF file in C:\wamp64\www\NaturesZone\pages\fpdf181\fpdf.php on line

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.