使用PHP在浏览器中预览PDF文件

时间:2016-06-28 14:05:50

标签: php pdf preview

我有一个问题要问你。 我想在我的网络浏览器上显示PDF。

此外,我制作了这段代码。它适用于Firefox,但遗憾的是不适用于Chrome。

    var ObjectID = require("mongodb").ObjectID;
    var eventCollection = req.db.get('events');
    var milliSinceEpoch = new Date().getTime(); 

    // find and return all the documents in the events DB where there is a user who has not payed for an event
    // they RSVP'd for
    eventCollection.find({"attendies.payed" : {$eq : false}}, function(err, documentsWithUnpayedUsers) {

        // if error finding, print it and return 
        if(err) {
          console.log(err); 
          return res.sendStatus(400, "Error cancelling");
        }

        // if everyone has payed for all RSVP'd events
        if(!documentsWithUnpayedUsers) return res.sendStatus(404, "Everyone has payed!");

        // loop through every document which has people who have not yet payed for RSVP'd events
        for(var i = 0; i < documentsWithUnpayedUsers.length; i++) {

          // for each of these documents: 
          eventCollection.update(
            {_id: ObjectID(documentsWithUnpayedUsers[i]._id)},
            {
              // remove the user from the attendie list if they have not payed, 
              // and it has been 10 minutes since they RSVP'd
              $pull:
                {
                  "attendies" : {"timeStamp": {$lt: milliSinceEpoch - 600000}, "payed" : {$eq : false}}
                },
              // then modify the number of spaces available for the event by the number of people who were 
              // removed from the attendie list
              // then modify the amount of people who have not payed for the event yet (will now be 0)
              $inc:
                {
                  spacesAvailable: documentsWithUnpayedUsers[i].unpayed,
                  unpayed: -documentsWithUnpayedUsers[i].unpayed
                }
            }, function(err) {
                 // error checking for the update query
                 if(err){
                   console.log(err); 
                   return res.sendStatus(400, "There was an error removing an attendie fom the event: " 
                    + documentsWithUnpayedUsers[i].eventName);
                 }
               }
          ); // end of update query
        } // end of for loop

        return res.end();

       }
    ); // end of find()
  }); // end of checkPayed

你能帮助我吗?

提前谢谢!

2 个答案:

答案 0 :(得分:2)

标题前面不能有var_dump()(除非您在不告诉我们的情况下使用输出缓冲)。发送标头之前一定不能有任何输出。此外,如果您使用var_dump()转储PDF的内容,这些内容将引入可能导致大多数PDF查看器出现问题的周围工件。

要进行问题排查,请尝试以下最小的工作示例:

<?php
$path = './path/to/file.pdf';

header('Content-Type: application/pdf');
header('Content-Disposition: inline; filename='.$path);
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');

readfile($path);

除此之外,使用关闭的PHP标记(?&gt;)通常也是一个好主意,以避免文件末尾出现任何意外的意外空白,可能会引起问题;并坚持使用一种类型的标记&#39;或&#34;为你的字符串。

答案 1 :(得分:1)

我尝试过不同的方式.. 最好用Imagick制作图像并显示图像.. 要么 用Ghostscript缩小......

像这样,你可以进行预览,所有浏览器都很容易。 对于make PDF-&gt; PNG我喜欢这样:

$save_to = '.png';
exec('convert -density 300 -trim "'.$file.'" -resize 1000 -quality 85 -colorspace RGB -background white  "'.$destination.$save_to.'" &', $output, $return_var);

&amp;在目的地的最后允许为多页pdf制作不同的图像,如: 图像1.png 图像2.png ....

如果您想要显示PDF,但更小,则可以缩小PDF,但所有浏览器都不会以相同的方式显示PDF ...

我的简化PDF代码如下:

exec('gs -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dPDFSETTINGS=/ebook  -dNOPAUSE  -dBATCH -sOutputFile='.$output_file.' '.$file.' ',$output, $return_var);