【问题标题】:embed pdf from blob to html/php page将 pdf 从 blob 嵌入到 html/php 页面
【发布时间】:2014-03-31 07:33:48
【问题描述】:

我将 PDF 文档作为 LONGBLOB 存储在数据库中。是否有将这些 PDF 嵌入网页的解决方案?我找到了 JavaScript 的 PDFObject,但它只适用于存储在文件夹中的 PDF 文件。或者,我可以将 Blob 中的 PDF 转换成足够长的时间来阅读它吗?

目前,我使用以下脚本打开 PDF:

require_once 'init.php';

$sql = "SELECT data FROM dms_files WHERE id=42";

// the result of the query
$result = mysql_query("$sql") or die("Invalid query: " . mysql_error());

// set the header for the image
header( "Pragma: private");
header( "Cache-Control: max-age=0");
header("Content-type: application/pdf");
header('Content-Transfer-Encoding: binary');
header('Content-Description: File Transfer');
header('Content-Transfer-Encoding: binary');

echo $pdf;

它将 PDF 作为文件打开,但不会将其嵌入到页面中。

上传 PDF:

$docData = addslashes(file_get_contents($_FILES['document']['tmp_name']));
$docProperties = filesize($_FILES['document']['tmp_name']);
$sqldoc = "INSERT INTO dms_files(mime ,data) VALUES('{$docProperties}', '{$docData}')";
$current_id = mysql_query($sqldoc) or die("<b>Error:</b> Problem on Doc. Insert<br/>" . mysql_error());
$docId = mysql_insert_id();

我试过用这个:

<?php
require_once 'init.php';

$sql = "SELECT * FROM dms_files WHERE id = 42 ";

// the result of the query
$result = mysql_query("$sql") or die("Invalid query: " . mysql_error());
$row = mysql_fetch_row($result);

$basedir = 'LIB/';
$pdfname = strtolower(preg_replace('/([^\w\d\-_]+)/', '-', $row->nazov_rd));
$filename = $basedir . $pdfname . '_' . $row->id. '.pdf';
$file_content = base64_decode($row->data);
file_put_contents('filename.pdf', $row->data);

?>

但是,它显示了以下错误:

注意:尝试在第 11 行的 /Applications/XAMPP/xamppfiles/htdocs/D/PDFSHOW/pdfshow.php 中获取非对象的属性

注意:试图在第 12 行的 /Applications/XAMPP/xamppfiles/htdocs/D/PDFSHOW/pdfshow.php 中获取非对象的属性

注意:尝试在第 13 行的 /Applications/XAMPP/xamppfiles/htdocs/D/PDFSHOW/pdfshow.php 中获取非对象的属性

注意:试图在第 14 行的 /Applications/XAMPP/xamppfiles/htdocs/D/PDFSHOW/pdfshow.php 中获取非对象的属性

怎么了?当我打开 filename.pdf 时,预览显示“文件为空,无法打开”


我能够解决它;这是丢失的:

while($row = mysql_fetch_object($rs)){

【问题讨论】:

  • 你能发布你现在使用的完整代码吗?
  • @Bram 看看第一篇文章我添加了上传部分

标签: php mysql pdf embed blob


【解决方案1】:

这在 Laravel 5.7 中为我工作了一步从数据库中获取的安排

 foreach ($documento as $obj) 
  { 
    // $file = substr($obj->Nombre,0,strlen($obj->Nombre)-3).'pdf';
    $filenombre = $Nombre.'.pdf';

    $bytes = base64_decode($obj->file);

    //header('Content-type: "'.$tipo.'"; charset=utf-8');
    header("Content-type: application/pdf"); 
    header('Content-Transfer-Encoding: binary'); 
    header('Content-disposition: attachment; filename='.basename($filenombre));    
    //header('Content-Length: ' . filesize($filenombre));   
    print $bytes; 
    //header('Content-Length: ' . filesize($filenombre));   
    //readfile($filenombre);    
    exit(0); 

  }

【讨论】:

    【解决方案2】:

    使用 javascript pdfobject 创建一个嵌入此元素的不同页面。

    【讨论】:

    • 我在考虑这与 iframe 相关,但是。我不知道如何为存储在 blob 中的 pdf 生成链接......有什么想法吗?
    • ulink all pdf on tempdir(如果存在)..从数据库中读取 blob...并将其下载到临时目录...加载嵌入创建的 pdf...
    猜你喜欢
    • 2019-05-13
    • 2011-04-15
    • 2015-03-24
    • 2015-09-18
    • 1970-01-01
    • 1970-01-01
    • 2023-03-28
    • 2013-09-15
    相关资源
    最近更新 更多