且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

用FPDF创建的PDF以及如何保存和检索pdf

更新时间:2023-01-30 18:35:05

在您的数据库中:

  • 将Cloumn类型设置为BLOB

保存:

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

检索:

$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;
}