PDF created with FPDF and how to save and retrieve the pdf [closed]

二次信任 提交于 2019-12-02 23:45:40

问题


How do I store a PDF created by FPDF in a MySQL database?


回答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.



来源:https://stackoverflow.com/questions/9437761/pdf-created-with-fpdf-and-how-to-save-and-retrieve-the-pdf

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!