将 pdf 保存到 BLOB(在 mssql 服务器中)并将其输出回网站 (php)
Saving pdf to BLOB (in mssql server) and output it back on website (php)
我需要将 pdf 文件保存到 BLOB(数据库是 MsSql 2012,但我也需要它在 2008 上工作)。
以下是我目前尝试过的内容的汇编:
(文件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 中打开。我的猜测是我输出错误,但也许是我保存的方式?
根据评论编辑
当您从数据库中获取字符串时,似乎 php+mssql 会截断它,但它在 php.ini 中是可管理的。我设置 mssql.textlimit = 160000 和 mssql.textsize = 160000 并且它适用于十六进制转换和 varchar(max).
您在将pdf数据保存到数据库之前进行了转换,您必须在输出之前撤消转换。
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="the.pdf"');
echo hex2bin(substr($w_blob['PDFData'], 2));
还有你为什么要首先转换数据,只是将其保存为二进制文件。
我需要将 pdf 文件保存到 BLOB(数据库是 MsSql 2012,但我也需要它在 2008 上工作)。
以下是我目前尝试过的内容的汇编:
(文件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 中打开。我的猜测是我输出错误,但也许是我保存的方式?
根据评论编辑
当您从数据库中获取字符串时,似乎 php+mssql 会截断它,但它在 php.ini 中是可管理的。我设置 mssql.textlimit = 160000 和 mssql.textsize = 160000 并且它适用于十六进制转换和 varchar(max).
您在将pdf数据保存到数据库之前进行了转换,您必须在输出之前撤消转换。
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="the.pdf"');
echo hex2bin(substr($w_blob['PDFData'], 2));
还有你为什么要首先转换数据,只是将其保存为二进制文件。