0

So i have a form where the user enters the values and they are put into the database. I am using MVC architecture. Now how can I make it that I make the PDF the same time as the values are sent to the DB. How do I make the variables work in the PDF file.

        <form action="index.php" method="post" id="make_order_form">
        <input type="hidden" name="action" value="make_order">
        <input type="hidden" name="action" value="create_PDF">

make_order sends the values into the db and create_pdf is supposed to make the pdf file

function get_customers() {
global $db;
$query = 'SELECT * FROM customers
          ORDER BY customerID';
$statement = $db->prepare($query);
$statement->execute();
return $statement;    
}
<?php

function create_customer($fname,$lname,$mobile,$email,$password1,$password2) {
global $db;
$query = 'INSERT INTO customers
             (fname,lname,mobile,email,password1,password2)
          VALUES
             (:fname,:lname,:mobile,:email,:password1,:password2)

          '
        ;
$statement = $db->prepare($query);
$statement->bindValue(':fname', $fname);
$statement->bindValue(':lname', $lname);
$statement->bindValue(':mobile', $mobile);
$statement->bindValue(':email', $email);
$statement->bindValue(':password1', $password1);
$statement->bindValue(':password2', $password2);
$statement->execute();
$statement->closeCursor();
}

This is what i have in index for the action create_PDF and im confused how im supposed to make the value available in the pdf file.

else if ($action == 'createPDF.php') {

$fname = filter_input(INPUT_POST, 'fname');
$lname = filter_input(INPUT_POST, 'lname');
$mobile = filter_input(INPUT_POST, 'mobile');
$email = filter_input(INPUT_POST, 'email');
$password1 = filter_input(INPUT_POST, 'password1');
$password2 = filter_input(INPUT_POST, 'password2');
$series = filter_input(INPUT_POST,'series');
$bodyType = filter_input(INPUT_POST, 'bodyType');
$trim = filter_input(INPUT_POST, 'trim');
$paint = filter_input(INPUT_POST, 'paint');
$brakes = filter_input(INPUT_POST, 'brakes');
$body = filter_input(INPUT_POST, 'body');
$media = filter_input(INPUT_POST, 'media');

if ($fname == NULL || $fname == FALSE || $lname == NULL || $lname == FALSE || $mobile == NULL || $mobile == FALSE || $email == NULL || $email == FALSE || $password1 == NULL || $password1 == FALSE || $password2 == NULL || $password2 == FALSE || $series == NULL || $series == FALSE || $bodyType == NULL || $bodyType == FALSE || $trim == NULL || $trim == FALSE || $paint == NULL || $paint == FALSE || $brakes == NULL || $brakes == FALSE || $body == NULL || $body == FALSE || $media == NULL || $media == FALSE) {
    $error = "Invalid product data. Check all fields and try again.";
    include('./errors/error.php');
} else {
    get_customers();
}
}
lukas13x
  • 73
  • 1
  • 12
  • I would look into using something like jsPDF to do this on the client side. – Adam Copley Feb 13 '16 at 23:08
  • Looks like you could launch the pdf creation process inside `get_customers()` and pass the `$fname, $lname etc...` to that as arguments which you could use to make the pdf which you could then also insert into your database if you wanted to. – Steve Feb 14 '16 at 07:16
  • This page has what claims to be a tested method of setting up a pdf using tcpdf: http://stackoverflow.com/questions/18223743/to-generate-pdf-download-using-tcpdf which you could put into your function – Steve Feb 14 '16 at 07:29
  • You could also make a separate function `make_pdf($fname,$lname,$mobile,$email,...);` which you would call after `get_customers();` and put the code in that. Essentially you are making an HTML table with your variables in it and printing that into your pdf which you can then do what you like with - save, download, display etc. The answer given by @shankar-damodaran seems fairly clear so I will not duplicate it unless you really need clarification. – Steve Feb 14 '16 at 07:52

1 Answers1

0

you can use something like php to pdf api like this

http://phptopdf.com/download ==>its require a free registration

and here is an example how to make pdf from html and save on your site

<?php
 // INCLUDE THE phpToPDF.php FILE
require("phpToPDF.php"); 

// PUT YOUR HTML IN A VARIABLE
$my_html="<HTML>
<h2>Test HTML 01</h2><br><br>
<div style=\"display:block; padding:20px; border:2pt solid:#FE9A2E; background-color:#F6E3CE; font-weight:bold;\">
phpToPDF is pretty cool!
</div><br><br>
For more examples, visit us here --> http://phptopdf.com/examples/
</HTML>";

// SET YOUR PDF OPTIONS
// FOR ALL AVAILABLE OPTIONS, VISIT HERE:  http://phptopdf.com/documentation/
$pdf_options = array(
  "source_type" => 'html',
  "source" => $my_html,
  "action" => 'save',
  "save_directory" => '',
  "file_name" => 'html_01.pdf');

// CALL THE phpToPDF FUNCTION WITH THE OPTIONS SET ABOVE
phptopdf($pdf_options);

// OPTIONAL - PUT A LINK TO DOWNLOAD THE PDF YOU JUST CREATED
echo ("<a href='html_01.pdf'>Download Your PDF</a>");
?>

and this question can help you Convert HTML + CSS to PDF with PHP?

Community
  • 1
  • 1
Mo Shal
  • 1,587
  • 18
  • 25