How can i view a pdf document using php?
Tags: programming, php
Published: 2023-03-08 10:26:46

Hi Team

I need a help, i have working code but what im struggling is to how to view a document as a pdf using php. This code uses php mail function to attach document as pdf and see below;

// code to attach document as pdf.
`<?php

   $filenameee = $_FILES['id']['name'];
   $fileName = $_FILES['id']['temp_name'];
    $amount  =$_REQUEST['amount'];
   $purpose =$_REQUEST['purpose'];
   $voucher =$_REQUEST['voucher'];
   $gender =$_REQUEST['gender'];
   $fname =$_REQUEST['fname'];
   $lname =$_REQUEST['lname'];
   $title =$_REQUEST['title'];
   $typeID =$_REQUEST['typeID'];
   $email =$_REQUEST['email'];
   $number =$_REQUEST['number'];
   $maritalstatus =$_REQUEST['maritalstatus'];
   $street1 =$_REQUEST['street1'];
   $street2 =$_REQUEST['street2'];
   $town =$_REQUEST['town'];
   $province =$_REQUEST['province'];
   $code =$_REQUEST['code'];
   $propertyownership =$_REQUEST['propertyownership'];
   $placeofwork =$_REQUEST['placeofwork'];
   $jobtitle =$_REQUEST['jobtitle'];
   $jobstreet=$_REQUEST['jobstreet'];
   $jobstreet2 =$_REQUEST['jobstreet2'];
   $jobtown =$_REQUEST['jobtown'];
   $worknumber =$_REQUEST['worknumber'];
   $jobprovince =$_REQUEST['jobprovince'];
   $jobcode =$_REQUEST['jobcode'];
   $income =$_REQUEST['income'];
   $bankname=$_REQUEST['bankname'];
   $branchname =$_REQUEST['branchname'];
   $accountno =$_REQUEST['accountno'];
   $accounttype =$_REQUEST['accounttype'];
   $signature =$_REQUEST['signature'];
   $bankstatement =$_REQUEST['bankstatement'];
   $payslip=$_REQUEST['payslip'];
   $usermessage = $_POST['message'];
   
   $message ="Name = ". $fname . "\r\n Email = " . $email . "\r\n Surname = " . $lname . "\r\n Message =" . $usermessage;
   
   $subject ="Application Form";
   $fromname ="ACI FINANCE";
   $fromemail = $email; //if u dont have an email create one on your cpanel
   $mailto = 'Ongeziwe1998@gmail.com'; //the email which u want to recv this email
   $content = file_get_contents($filenameee);
   $content = chunk_split(base64_encode($content));
   
   // a random hash will be necessary to send mixed content
   $separator = md5(time());
   
   // carriage return type (RFC)
   $eol = "\r\n";
   
    // main header (multipart mandatory)
   $headers = "From: ".$fromname." <".$fromemail.">" . $eol;
   $headers .= "MIME-Version: 1.0" . $eol;
   $headers .= "Content-Type: multipart/mixed; boundary=\"" . $separator . "\"" . $eol;
   $headers .= "Content-Transfer-Encoding: 7bit" . $eol;
   $headers .= "This is a MIME encoded message." . $eol;
    
    $logo = 'img/logo.png';
    $link = '#';

	$body = "<!DOCTYPE html><html lang='en'><head><meta charset='UTF-8'><title>Express Mail</title></head><body>";
	$body .= "<table style='width: 100%;'>";
	$body .= "<thead style='text-align: center;'><tr><td style='border:none;' colspan='2'>";
	$body .= "<a href='{$link}'><img src='{$logo}' alt=''></a><br><br>";
	$body .= "</td></tr></thead><tbody><tr>";
	$body .= "<td style='border:none;'><strong>Name:</strong> {$fname} {$lname}</td>";
	$body .= "<td style='border:none;'><strong>Email:</strong> {$from}</td>";
	$body .= "</tr>";
	$body .= "<tr><td style='border:none;'><strong>CellNumber:</strong> {$number}</td></tr>";
	$body .= "<tr><td></td></tr>";
	$body .= "<tr><td style='border:none;'><strong>Loan Amount:</strong> {$amount}</td></tr>";
	$body .= "<tr><td></td></tr>";
	$body .= "<tr><td style='border:none;'><strong>Monthly income:</strong> {$income}</td></tr>";
	$body .= "<tr><td></td></tr>";
	$body .= "<tr><td style='border:none;'><strong>Town:</strong> {$town}</td></tr>";
	$body .= "<tr><td></td></tr>";
	$body .= "<tr><td style='border:none;'><strong>Upload bankstatement </strong> {$bankstatement}</td></tr>";
	$body .= "<tr><td></td></tr>";
	$body .= "<tr><td style='border:none;'><strong>Upload payslip:</strong> {$payslip}</td></tr>";
	$body .= "<tr><td></td></tr>";
	$body .= "<tr><td colspan='2' style='border:none;'><strong></strong> </td></tr>";
	// message
  $body = "--" . $separator . $eol;
  $body .= "Content-Type: text/plain; charset=\"iso-8859-1\"" . $eol;
  $body .= "Content-Transfer-Encoding: 8bit" . $eol;
	// attachment
 $body .= "--" . $separator . $eol;
 $body .= "Content-Type: application/pdf; name=\"" . $filenameee . "\"" . $eol;
 $body .= "Content-Transfer-Encoding: base64" . $eol;
 $body .= "Content-Disposition: attachment" . $eol;
$body .= $content . $eol;
$body .= "--" . $separator . "--";
$body .= "</tbody></table>";
	
$body .= "</body></html>";

//SEND Mail
 if (mail($mailto, $subject, $body, $headers)) {
echo "mail send ... OK"; // do what you want after sending the email
 } else {
echo "mail send ... ERROR!";
print_r( error_get_last() );
 }
    
    $send = mail($to, $subject, $body, $headers); 
    
   $amount  =$_POST['amount'];
   $purpose =$_POST['purpose'];
   $voucher =$_POST['voucher'];
   $gender =$_POST['gender'];
   $fname =$_POST['fname'];
   $lname =$_POST['lname'];
   $title =$_POST['title'];
   $typeID =$_POST['typeID'];
   $email =$_POST['email'];
   $number =$_POST['number'];
   $maritalstatus =$_POST['maritalstatus'];
   $street1 =$_POST['street1'];
   $street2 =$_POST['street2'];
   $town =$_POST['town'];
   $province =$_POST['province'];
   $code =$_POST['code'];
   $propertyownership =$_POST['propertyownership'];
   $placeofwork =$_POST['placeofwork'];
   $jobtitle =$_POST['jobtitle'];
   $jobstreet=$_POST['jobstreet'];
   $jobstreet2 =$_POST['jobstreet2'];
   $jobtown =$_POST['jobtown'];
   $worknumber =$_POST['worknumber'];
   $jobprovince =$_POST['jobprovince'];
   $jobcode =$_POST['jobcode'];
   $income =$_POST['income'];
   $bankname=$_POST['bankname'];
   $branchname =$_POST['branchname'];
   $accountno =$_POST['accountno'];
   $accounttype =$_POST['accounttype'];
   $signature =$_POST['signature'];
   $bankstatement =$_POST['bankstatement'];
   $payslip=$_POST['payslip'];
   
   
   
   // Connect to database
   
   $conn = new mysqli('localhost','acifinan_acifinan','Finance_01','acifinan_acifinanc');
   if ($conn->connect_error)
   {die('Connection Failed:'.$conn-> connect_error); }
   else 
  {
      $stmt= $conn->prepare("insert into application(amount, purpose, voucher, gender, fname, lname, title, typeID, email, number, maritalstatus, street1, street2, town, province, code, propertyownership, placeofwork, jobtitle, jobstreet, jobstreet2, jobtown, worknumber, jobprovince, jobcode, income, bankname, branchname, accountno, accounttype, signature, id, bankstatement, payslip) values (? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ? , ?)");
      $stmt-> bind_param("ssssssssssssssssssssssssssssssssss" , $amount,$purpose,$voucher,$gender,$fname,$lname,$title,$typeID,$email,$number,$maritalstatus,$street1,$street2,$town,$province,$code,$propertyownership,$placeofwork,$jobtitle,$jobstreet,$jobstreet2,$jobtown,$worknumber,$jobprovince,$jobcode,$income,$bankname,$branchname,$accountno,$accounttype,$signature,$id,$bankstatement,$payslip);
      $stmt->execute();
      echo "Submitted Successfully";
      $stmt->close();
      $conn->close();
  
      
  }
    
?>`