PHP - How To Print Document

Submitted by: 
Language: 
Visitors have accessed this post 913 times.

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 to learned its syntax. It is mostly used by a newly coders for its user friendly environment. So Let's do the coding.

Before we started:

First you have to download & install XAMPP or any local server that run PHP scripts. Here's the link for XAMPP server https://www.apachefriends.org/index.html.

And this is the link for the jquery that i used in this tutorial https://jquery.com/.

Lastly, this is the link for the bootstrap that i used for the layout design https://getbootstrap.com/.

Creating Database

Open your database web server then create a database name in it db_print, after that click Import then locate the database file inside the folder of the application then click ok.
tut1

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="<a href="https://sourcecodester.com">Sourcecodester</a>
  11. " rel="nofollow">https://sourcecodester.com">Sourcecodester</a>
  12. </a> </div>
  13. </nav>
  14. <div class="col-md-3"></div>
  15. <div class="col-md-6 well">
  16. <h3 class="text-primary">PHP - How To Print A Document</h3>
  17. <hr style="border-top:1px dotted #ccc;" />
  18. <button type="button" class="btn btn-primary" data-toggle="modal" data-target="#exampleModal"><span class="glyphicon glyphicon-plus"></span> Add Product</button>
  19. <br /><br />
  20. <a href="print.php" class="btn btn-success pull-right"><span class="glyphicon glyphicon-print"></span> Print</a>
  21. <br />
  22. <br />
  23. <table class="table table-bordered">
  24. <thead class="alert-success">
  25. <tr>
  26. <th>Product Name</th>
  27. <th>Price</th>
  28. <th>Qty</th>
  29. <th>Data Added</th>
  30. </tr>
  31. </thead>
  32. <tbody style="background-color:#fff;">
  33. <?php
  34. require 'conn.php';
  35.  
  36. $query = $conn->query("SELECT * FROM `product`");
  37. while($fetch = $query->fetch_array()){
  38.  
  39. ?>
  40.  
  41. <tr>
  42. <td><?php echo $fetch['pname']?></td>
  43. <td><?php echo $fetch['price']?></td>
  44. <td><?php echo $fetch['quantity']?></td>
  45. <td><?php echo $fetch['date_added']?></td>
  46. </tr>
  47.  
  48. <?php
  49. }
  50. ?>
  51. </tbody>
  52. </table>
  53. </div>
  54. <div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  55. <div class="modal-dialog" role="document">
  56. <form action="save_query.php" method="POST">
  57. <div class="modal-content">
  58. <div class="modal-body">
  59. <div class="col-md-2"></div>
  60. <div class="col-md-8">
  61. <div class="form-group">
  62. <label>Product Name</label>
  63. <input type="text" name="pname" class="form-control"/>
  64. </div>
  65. <div class="form-group">
  66. <label>Price</label>
  67. <input type="number" name="price" class="form-control"/>
  68. </div>
  69. <div class="form-group">
  70. <label>Qty</label>
  71. <input type="number" name="qty" class="form-control"/>
  72. </div>
  73. </div>
  74. </div>
  75. <div style="clear:both;"></div>
  76. <div class="modal-footer">
  77. <button type="button" class="btn btn-danger" data-dismiss="modal"><span class="glyphicon glyphicon-remove"></span> Close</button>
  78. <button name="save" class="btn btn-primary"><span class="glyphicon glyphicon-save"></span> Submit</button>
  79. </div>
  80. </div>
  81. </form>
  82. </div>
  83. </div>
  84. </body>
  85. <script src="js/jquery-3.2.1.min.js"></script>
  86. <script src="js/bootstrap.js"></script>
  87. </html>

Creating PHP Query

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>

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


Add new comment

Filtered HTML

  • Web page addresses and e-mail addresses turn into links automatically.
  • You may insert videos with [video:URL]
  • Allowed HTML tags: <a> <em> <strong> <cite> <blockquote> <code> <ul> <ol> <li> <dl> <dt> <dd> <table> <tr> <td> <th> <img> <h1> <h2> <h3> <iframe> [video]
  • You can enable syntax highlighting of source code with the following tags: <code>, <blockcode>, <asp>, <c>, <cpp>, <csharp>, <css>, <html4strict>, <java>, <javascript>, <mysql>, <php>, <python>, <sql>, <vb>, <vbnet>. The supported tag styles are: <foo>, [foo].
  • Lines and paragraphs break automatically.

Plain text

  • No HTML tags allowed.
  • Lines and paragraphs break automatically.
CAPTCHA
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.