How do I store a PDF created by FPDF in a MySQL database?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 8 years ago.
回答1:
In Your Database:
- Set cloumn type to BLOB
Save:
//make a pdf
$pdf=new FPDF();
$pdf->AddPage();
//set pdf to savable type
$pdfcontent = $pdf->Output("", "S");
//save pdf to database
$mysqli=new mysqli("hostname", "username", "password", "database");
$stmt = $mysqli->prepare("INSERT INTO pdfs (pdf) VALUES (?)");
$stmt->bind_param('s', $pdfcontent);
$stmt->execute();
Retrieve:
$mysqli=new mysqli("hostname", "username", "password", "database");
$stmt = $mysqli->prepare("SELECT pdf FROM pdfs WHERE id = ?");
$stmt->bind_param('i',$id);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($pdfcontent);
while($stmt->fetch()){
header("Content-Length: " . strlen($pdfcontent) );
header("Content-Type: application/octet-stream");
header('Content-Disposition: attachment; filename="WarriorDeals_Voucher_'.$number.'-'.$numIndex.'.pdf"');
header("Content-Transfer-Encoding: binary\n");
echo $pdfcontent;
}
回答2:
Are you sure you truly need to save the resultant PDF to the database? Perhaps you could persist the PDF to the file system, and save the path to the file in the database.
回答3:
I am not sure I understand your question, but in case it's what I think you're asking... You will need the root path as well as the file name you use to store the file to create a proper database entry. You have to use a mysql databse and and an insert query to input it as long as a select query to retrieve its location and use it in your website. Read the basics of PHP/mySQL at w3schools and also considering FPDF, read the uploaded FAQ here.