将使用FPDF php库创建的PDF保存在MySQL Blob字段中 [英] Save a PDF created with FPDF php library in a MySQL blob field

查看:87
本文介绍了将使用FPDF php库创建的PDF保存在MySQL Blob字段中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要使用 fpdf 库创建一个pdf文件,并将其保存在MySQL的blob字段中数据库. 问题是,当我尝试从blob字段中检索文件并将其发送到浏览器进行下载时,下载的文件已损坏并且无法正确显示.

I need to create a pdf file with the fpdf library and save it in a blob field in my MySQL database. The problem is, when I try to retrieve the file from the blob field and send it to the browser for the download, the donwloaded file is corrupted and does not display correctly.

如果我立即将其发送给浏览器而不将其存储在数据库中,则会正确显示相同的pdf文件,因此当插入数据库时​​,似乎某些数据已损坏.

The same pdf file is correctly displayed if I send it immediately to the browser without storing it in the db, so it seems some of the data gets corrupted when is inserted in the db.

我的代码是这样的:

$pdf = new MyPDF(); //class that extends FPDF and create te pdf file
$content = $pdf->Output("", "S"); //return the pdf file content as string
$sql = "insert into mytable(myblobfield) values('".addslashes($content)."')";
mysql_query($sql);

存储pdf,就像这样:

to store the pdf, and like this:

$sql = "select myblobfield from mytable where id = '1'";
$result = mysql_query($sql);
$rs = mysql_fetch_assoc($result);
$content = stripslashes($rs['myblobfield']);
header('Content-Type: application/pdf');
header("Content-Length: ".strlen(content));
header('Content-Disposition: attachment; filename=myfile.pdf');
print $content;

将其发送到浏览器进行下载. 我在做什么错了?

to send it to the browser for downloading. What am I doing wrong?

如果我将代码更改为:

$pdf = new MyPDF(); 
$pdf->Output(); //send the pdf to the browser

文件显示正确,所以我认为文件生成正确,问题出在数据库中.

the file is correctly displayed, so I assume that is correctly generated and the problem is in the storing in the db.

谢谢.

推荐答案

您不应删除任何(反)斜杠.添加它们时,它们将用作查询中的转义符,并且不会出现在字段中.

You shouldn't strip any (back)slashes. When you add them, they are used as escape characters in the query and do not appear in the field.

尝试更改此内容

$content = stripslashes($rs['myblobfield']);

对此:

$content = $rs['myblobfield'];

这篇关于将使用FPDF php库创建的PDF保存在MySQL Blob字段中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆