How to Print Document in PHP and JavaScript

In this tutorial we will tackle on How To Print Document using PHP. PHP is a server-side scripting language designed primarily for web development. Using PHP, you can let your user directly interact with the script and easily learn its syntax. It is mostly used by a newly coders for its user-friendly environment.

So Let's do the coding.

Before we started:

Creating Database

Open your database web server then create a database naming "db_print". After that, click Import then locate the database file inside the folder of the application then click ok.

tut1

Or, you can copy and paste the SQL script below in SQL page of the database.

  1. CREATE TABLE `product` (
  2.   `product_id` int(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
  3.   `pname` varchar(50) NOT NULL,
  4.   `price` int(12) NOT NULL,
  5.   `quantity` int(5) NOT NULL,
  6.   `date_added` varchar(50) NOT NULL
  7.  
  8. INSERT INTO `product` (`product_id`, `pname`, `price`, `quantity`, `date_added`) VALUES
  9. (1, 'Bear Brand', 14, 200, '2018-06-12'),
  10. (2, 'Fresh Milk', 90, 500, '2018-06-12'),
  11. (3, 'Tobleron', 60, 100, '2018-06-12'),
  12. (4, 'Avocado', 50, 50, '2018-06-12');

Creating the database connection

Open your any kind of text editor(notepadd++, etc..). Then just copy/paste the code below then name it "conn.php".

  1. <?php
  2.         $conn = new mysqli('localhost', 'root', '', 'db_print');
  3.        
  4.         if(!$conn){
  5.                 die("Error: Can't connect to database");
  6.         }
  7. ?>

Creating The Interface

This is where we will create the appearance of an application. To create this simply copy and write this block of code inside the text editor, then save it as index.php.
  1. <!DOCTYPE html>
  2. <html lang="en">
  3.         <head>
  4.                 <meta charset="UTF-8" name="viewport" content="width=device-width, initial-scale=1"/>
  5.                 <link rel="stylesheet" type="text/css" href="css/bootstrap.css"/>
  6.         </head>
  7.         <body>
  8.         <nav class="navbar navbar-default">
  9.                 <div class="container-fluid">
  10.                         <a class="navbar-brand" href="https://sourcecodester.com">Sourcecodester</a>
  11.                 </div>
  12.         </nav>
  13.         <div class="col-md-3"></div>
  14.         <div class="col-md-6 well">
  15.                 <h3 class="text-primary">PHP - How To Print A Document</h3>
  16.                 <hr style="border-top:1px dotted #ccc;" />
  17.                 <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal"><span class="glyphicon glyphicon-plus"></span> Add Product</button>
  18.                 <br /><br />
  19.                 <a href="print.php" target="_blank" class="btn btn-success pull-right"><span class="glyphicon glyphicon-print"></span> Print</a>
  20.                 <br />
  21.                 <br />
  22.                 <table class="table table-bordered">
  23.                         <thead class="alert-success">
  24.                                 <tr>
  25.                                         <th>Product Name</th>
  26.                                         <th>Price</th>
  27.                                         <th>Qty</th>
  28.                                         <th>Data Added</th>
  29.                                 </tr>
  30.                         </thead>
  31.                         <tbody style="background-color:#fff;">
  32.                                 <?php
  33.                                         require 'conn.php';
  34.                                        
  35.                                         $query = $conn->query("SELECT * FROM `product`");
  36.                                         while($fetch = $query->fetch_array()){
  37.                                                
  38.                                 ?>
  39.                                
  40.                                 <tr>
  41.                                         <td><?php echo $fetch['pname']?></td>
  42.                                         <td><?php echo $fetch['price']?></td>
  43.                                         <td><?php echo $fetch['quantity']?></td>
  44.                                         <td><?php echo $fetch['date_added']?></td>
  45.                                 </tr>
  46.                                
  47.                                 <?php
  48.                                         }
  49.                                 ?>
  50.                         </tbody>
  51.                 </table>
  52.         </div>
  53.         <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  54.                 <div class="modal-dialog" role="document">
  55.                         <form action="save_query.php" method="POST">
  56.                                 <div class="modal-content">
  57.                                         <div class="modal-body">
  58.                                                 <div class="col-md-2"></div>
  59.                                                 <div class="col-md-8">
  60.                                                         <div class="form-group">
  61.                                                                 <label>Product Name</label>
  62.                                                                 <input type="text" name="pname" class="form-control"/>
  63.                                                         </div>
  64.                                                         <div class="form-group">
  65.                                                                 <label>Price</label>
  66.                                                                 <input type="number" name="price" class="form-control"/>
  67.                                                         </div>
  68.                                                         <div class="form-group">
  69.                                                                 <label>Qty</label>
  70.                                                                 <input type="number" name="qty" class="form-control"/>
  71.                                                         </div>
  72.                                                 </div>
  73.                                         </div>
  74.                                         <div style="clear:both;"></div>
  75.                                         <div class="modal-footer">
  76.                                                 <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  77.                                                 <button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Submit</button>
  78.                                         </div>
  79.                                 </div>
  80.                         </form>
  81.                 </div>
  82.         </div>
  83. </body>
  84. <script src="js/jquery-3.2.1.min.js"></script>
  85. <script src="js/bootstrap.js"></script>
  86. </html>

Creating PHP

This code contains the save query of the application. This code will store the inputs to the database server. To do that copy and write these block of codes inside your text editor and save it as "save_query.php".

  1. <?php
  2.         require_once 'conn.php';
  3.  
  4.         if(ISSET($_POST['save'])){
  5.                 $pname = $_POST['pname'];
  6.                 $price = $_POST['price'];
  7.                 $qty = $_POST['qty'];
  8.                 $date = date("Y-m-d", strtotime("+6 HOURS"));
  9.                
  10.                 $conn->query("INSERT INTO `product` VALUES('', '$pname', '$price', '$qty', '$date')") or die(mysqli_errno());
  11.                
  12.                 header('location: index.php');
  13.         }
  14. ?>

Creating the Main Function

This code contains the main function of the application. This code will display the data that will be printed when the button is clicked. To do this all you have to do is copy and write the code inside the text editor and save it as print.php.

  1. <!DOCTYPE html>
  2. <?php
  3.         require 'conn.php';
  4. ?>
  5. <html lang="en">
  6.         <head>
  7.                 <style>
  8.                 .table {
  9.                         width: 100%;
  10.                         margin-bottom: 20px;
  11.                 }      
  12.                
  13.                 .table-striped tbody > tr:nth-child(odd) > td,
  14.                 .table-striped tbody > tr:nth-child(odd) > th {
  15.                         background-color: #f9f9f9;
  16.                 }
  17.                
  18.                 @media print{
  19.                         #print {
  20.                                 display:none;
  21.                         }
  22.                 }
  23.                 @media print {
  24.                         #PrintButton {
  25.                                 display: none;
  26.                         }
  27.                 }
  28.                
  29.                 @page {
  30.                         size: auto;   /* auto is the initial value */
  31.                         margin: 0;  /* this affects the margin in the printer settings */
  32.                 }
  33.         </style>
  34.         </head>
  35. <body>
  36.         <h2>Sourcecodester</h2>
  37.         <br /> <br /> <br /> <br />
  38.         <b style="color:blue;">Date Prepared:</b>
  39.         <?php
  40.                 $date = date("Y-m-d", strtotime("+6 HOURS"));
  41.                 echo $date;
  42.         ?>
  43.         <br /><br />
  44.         <table class="table table-striped">
  45.                 <thead>
  46.                         <tr>
  47.                                 <th>Product Name</th>
  48.                                 <th>Price</th>
  49.                                 <th>Qty</th>
  50.                                 <th>Data Added</th>
  51.                         </tr>
  52.                 </thead>
  53.                 <tbody>
  54.                         <?php
  55.                                 require 'conn.php';
  56.                                
  57.                                 $query = $conn->query("SELECT * FROM `product`");
  58.                                 while($fetch = $query->fetch_array()){
  59.                                        
  60.                         ?>
  61.                        
  62.                         <tr>
  63.                                 <td style="text-align:center;"><?php echo $fetch['pname']?></td>
  64.                                 <td style="text-align:center;"><?php echo $fetch['price']?></td>
  65.                                 <td style="text-align:center;"><?php echo $fetch['quantity']?></td>
  66.                                 <td style="text-align:center;"><?php echo $fetch['date_added']?></td>
  67.                         </tr>
  68.                        
  69.                         <?php
  70.                                 }
  71.                         ?>
  72.                 </tbody>
  73.         </table>
  74.         <center><button id="PrintButton" onclick="PrintPage()">Print</button></center>
  75. </body>
  76. <script type="text/javascript">
  77.         function PrintPage() {
  78.                 window.print();
  79.         }
  80. </script>
  81. </html>

Set printable table to Print View automatically

If you wanted to set the "print.php" into Print View automatically. Copy and paste the code below after the PrintPage() function inside the "script" tag. The code will open in a new window and automatically viewed in print view after the data loaded.

  1.         window.addEventListener('DOMContentLoaded', (event) => {
  2.                 PrintPage()
  3.                 setTimeout(function(){ window.close() },750)
  4.         });

The javascript code above helps also to automatically close the print page after printing or cancel printing.

There you have it we already learn on How To Print Document using PHP. I hope that this simple tutorial helps you understand the difficulties of PHP. For more updates and tutorials just kindly visit this site.

Enjoy Coding!!!

Add new comment