how to add pdf file in php code example

Example 1: upload pdf file in php

<html>
<head>
<title>  FORM </title>
</head>
<body align="left">
<h1> FILE UPLOAD </h1>

<form action = "term5b.php" method = "POST" enctype="multipart/form-data"/>

    <input type = "file" name = "fileupload"/></br>  
    <input type = "submit" name = "opt" value = "upload"/></br> </br>  

</form>
</body>
</html>







<?php
   $target_dir="E:\ ";
   $filename=$_FILES["fileupload"]["name"];

   $tmpname=$_FILES["fileupload"]["tmp_name"];
   $filetype=$_FILES["fileupload"]["type"];
   $errors=[];
   $fileextensions=["pdf"];
	$arr=explode(".",$filename);
   $ext=strtolower(end($arr));

   $uploadpath=$target_dir.basename($filename);
if(! in_array($ext,$fileextensions))
   {
     $errors[]="Invalid filename";
   }
   if(empty($errors))
   {
     if(move_uploaded_file($tmpname,$uploadpath))
     {
       echo "file uploaded successfully";
     }
     else
     {
       echo "not successfull";
     }
   }
   else
   {
      foreach($errors as $value)
      {
         echo "$value";
      }
   }
?>

Example 2: php create pdf

<?php
//install Dompdf library at link below:
//https://github.com/dompdf/dompdf
use Dompdf\Dompdf;
session_start();

  // Include the database
  $servername = "";
  $username = "";
  $password = "";
  $dbname = "";

  // Create connection
  $conn = new mysqli($servername, $username, $password, $dbname);
  // Check connection
  if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
  }  

    $html = '<table border=1>';	
    $html .= '<thead>';
    $html .= '<tr>';
    $html .= '<th>ID</th>';
    $html .= '<th>Collum1</th>';
    $html .= '<th>Collum2</th>';
    $html .= '<th>Collum3</th>';
    $html .= '</tr>';
    $html .= '</thead>';
    $html .= '<tbody>';
    
    $sql = "SELECT * FROM tableName";
    $sql = mysqli_query($conn, $sql);
    while($row = mysqli_fetch_assoc($sql)){      
      $html = ''; // your html code		
    }   

    // include autoloader
    require_once("dompdf/autoload.inc.php");

    //Create instance
    $dompdf = new DOMPDF();
    
    // Upload your HTML code
    $dompdf->load_html('
        <h1 style="text-align: center;">RentCar</h1>
        '. $html .'
      ');

    //Render html
    $dompdf->render();

    //Create and output the pdf 
    $pdf = $dompdf->output();

    //Visualize the page
    $dompdf->stream(
      "form.pdf", 
      array(
        "Attachment" => false //To download turn it to true, to preview pdf turn it to false
      )
    );

?>

Tags:

Php Example