2016-02-13 14 views
0

所以我有一個表單,用戶輸入值並將它們放入數據庫。我正在使用MVC體系結構。 現在我怎樣才能讓我在發送數據到數據庫的同時創建PDF。如何使變量在PDF文件中工作?如何使用數據庫中的值生成pdf

 <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發送值到db和create_pdf應該使PDF文件

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(); 
} 

這是我在指數爲行動create_PDF和IM困惑應該如何IM使在pdf文件中可用的值。

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(); 
} 
} 
+0

我會考慮使用類似jsPDF的東西在客戶端執行此操作。 –

+0

我必須使用tcpdf – lukas13x

+0

看起來您可以在'get_customers()'內啓動pdf創建過程,並將'$ fname,$ lname etc ...'作爲參數傳遞給您,您可以使用它來生成PDF如果你願意,也可以插入到你的數據庫中。 – Steve

回答

0

,你可以使用類似的PHP爲PDF API這樣

http://phptopdf.com/download ==>它需要免費註冊

,這裏是一個例子,如何從HTML製作的PDF和節省您的網站

<?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>"); 
?> 

這個問題可以幫助你Convert HTML + CSS to PDF with PHP?