在新标签页中打开pdf文件

时间:2018-11-01 09:00:55

标签: php pdf

大家早上好。 我正在尝试在新选项卡中打开pdf文件,我已成功将pdf上传到了我想要的文件夹中,并且pdf的标题位于该特定产品的数据库中。从“产品列表”中,我使用一个按钮并打开一个模式,其中有一个选项可以引用该产品的pdf(当前需要上传2个,但将来可能需要更多),我们需要打开。

这就是我所说的情态:

<a href="#docs<?php echo $row['prod_id'];?>" data-target="#docs<?php echo $row['prod_id'];?>" data-toggle="modal" style="color:#fff;" class="small-box-footer"><i class="glyphicon glyphicon-duplicate text-blue"></i></a>

这是模态:

<div id="docs<?php echo $row['prod_id'];?>" class="modal fade in" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display: none;">
<div class="modal-dialog">
  <div class="modal-content" style="height:auto">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close">
              <span aria-hidden="true">×</span></button>
            <h4 class="modal-title">Supporting Documents of <?php echo $row['prod_name'];?></h4>
          </div>
          <div class="modal-body">
             <div class="form-group">
               <label class="control-label col-lg-6" for="price">Technical Datasheet</label>

               <a href="view_docs.php" data-target="view_docs.php" src="../dist/docs/<?php echo $row['prod_tds'];?>"target="_blank">View TDS</a>

              <div class="col-lg-9">

                </div>  
             </div>

            <div class="form-group">
                <label class="control-label col-lg-6" for="price">Material Safety Datasheet</label>
                <a href="view_docs.php" data-target="view_docs.php" src="../dist/docs/<?php echo $row['prod_msds'];?>"target="_blank">View MSDS</a>
                <div class="col-lg-9">

                </div>
            </div> 
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
          </div>
          </form>
        </div>

    </div>

在阅读此书的同时,我需要调用一个新的php,其中包含一些我不知道如何使用它们的标头,并且出现“无法加载pdf文档”错误。

这是view_docs.php上的代码:

    <?php session_start();
if(empty($_SESSION['id'])):
header('Location:../index.php');
endif;

include('../dist/includes/dbcon.php');
$file = 'view'; 




$filename = '$file'; /* Note: Always use .pdf at the end. */

header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="filename.pdf"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($file));
header('Accept-Ranges: bytes');


@readfile($file);

?>

我没有弄清楚什么?我认为view_docs.php中有问题,但是我不知道如何使它工作...

2 个答案:

答案 0 :(得分:1)

我认为您缺少一些东西(例如文件扩展名):

//no extension
$file = 'view';  

//this is literally $file and never used again
$filename = '$file'; 

header('Content-type: application/pdf');
//the name of the transfered file is filename.php (not really an issue in this case)
//but is that what you wanted there
header('Content-Disposition: inline; filename="filename.pdf"');
header('Content-Transfer-Encoding: binary');
//still no extension on 'view' so your file is just named view (nothing else)
//no file path either.
header('Content-Length: ' . filesize($file));
header('Accept-Ranges: bytes');
//and you guessed it still no extension (error suppression hides the file not found error)
@readfile($file);

由于使用单引号,因此$filename实际上是$file的变量值。 PHP不会对单引号字符串进行变量插值(值替换)。没关系,因为它不再使用了。

@可能不应该存在,否则您将找不到找不到扩展名为view的文件。您应该会看到类似这样的东西

  

警告:filesize():无法在第2行的C:\ Unit \ eval \ 1541064182.php中查看统计信息

     

警告:readfile(view):无法打开流:第3行的C:\ Unit \ eval \ 1541064182.php中没有此类文件或目录

除了它将拥有您的路径,而没有我到单元测试服务器的路径。

换句话说,您的readfile调用实际上是这样的:

 @readfile('view');

您可以将下载名称保留为filename.php,但我不认为这是故意的。

$file = 'view.pdf'; 

$pathname = $path.$file;

header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="'.$file.'"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($pathname));
header('Accept-Ranges: bytes');

readfile($pathname);

很显然,您必须输入路径的值,或者如果将其放在同一文件夹中,则可以忽略它。

哦,最后一件事摆脱了?>这个问题,因为如果此后有换行,它可能会损坏您的文件。我忘记了这(特别是仅行返回)是否是PDF的问题,但是在结束标记(或之前)之后的任何内容都将成为文件数据的一部分。结束标签是可选的,为什么要冒险呢?

答案 1 :(得分:0)

您应在链接href中插入PDF网址 // view_docs.pdf

<a href="view_docs.pdf" data-target="view_docs.php" src="../dist/docs/<?php echo $row['prod_tds'];?>"target="_blank">View TDS</a>