问题描述
我需要将pdf文件保存到BLOB(数据库是MsSql 2012,但我也需要在2008年使用).
I need to save a pdf file into BLOB (database is MsSql 2012, but I would need it work on 2008 too).
这是到目前为止我尝试过的内容的汇编:
Here is compilation of what I have tried so far:
(文件where I save pdf into database
)
function preparePDF2String($filepath)
{
$handle = @fopen($filepath, 'rb');
if ($handle)
{
$content = @fread($handle, filesize($filepath));
$content = bin2hex($content);
@fclose($handle);
return "0x".$content;
}
}
$out = preparePDF2String('pdf/pdf.pdf');
$q_blob = "INSERT INTO HISTORIA_ARCHIWUM_test(PDFName, PDFData) VALUES('First test pdf', $out) ";
$r_blob = mssql_query($q_blob,$polacz);
Results
(好的,我想)
(现在是php文件where I try to output the pdf
)
// header('Content-type: application/pdf');
// readfile('pdf/pdf.pdf'); - this works just fine, so pdf is ok
$q_blob = "select top 1 * from HISTORIA_ARCHIWUM_test";
$r_blob = mssql_query($q_blob,$polacz);
$w_blob = mssql_fetch_array($r_blob);
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="the.pdf"');
echo $w_blob['PDFData'];
//echo hex2bin ($w_blob['PDFData']); - I also tried this - same problem
问题::当我尝试在浏览器中输出pdf时出现错误(此pdf格式不受支持).它也不会在Adobe Reader中打开.我的猜测是我将其输出错误,但是也许这是我保存它的方法?
Problem: when I try to output the pdf in the browser I get error (this pdf is of not supported format). It won't open in Adobe reader also. My guess is that I output it wrong, but maybe it is the way I save it?
推荐答案
根据评论进行编辑
从数据库中获取字符串时,似乎php + mssql会截断字符串,但是在php.ini中它是可管理的.我设置了mssql.textlimit = 160000和mssql.textsize = 160000,并且可以在十六进制转换和varchar(max)下正常工作.
EDIT from comments
It seems php+mssql truncates string when you get it from db, but its manageable in php.ini. I set mssql.textlimit = 160000 and mssql.textsize = 160000 and it works fine with Hex conversion and varchar(max).
要对pdf数据进行转换,然后再将其保存到数据库中,必须先撤消转换,然后再将其输出.
You are transforming the pdf data before you save it to the database, you have to undo the transformation before you output it.
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="the.pdf"');
echo hex2bin(substr($w_blob['PDFData'], 2));
还为什么要首先转换数据,只需将其保存为二进制文件即可.
Also why are you transforming the data in the first place, just save it as binary.
这篇关于将pdf保存到BLOB(在mssql服务器中),然后将其输出回网站(php)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!