在浏览器上显示PDF [英] Display PDF on browser

查看:79
本文介绍了在浏览器上显示PDF的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在存储在我们服务器上的浏览器中显示PDF文件.这是我的代码:-

I want to display a PDF file on browser which is store on our server. Here is my code :-

$path = $_SERVER['DOCUMENT_ROOT'].folder_name.'/resources/uploads/pdf/pdf_label_'.$order['id'].'.pdf';

$filename = 'pdf_label_'.$order['id'].'.pdf';

$file = $path;
$filename = $filename;

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

但是它在浏览器的输出下面返回:

But it returns below output on browser :

%PDF-1.4 % 5 0 obj >stream

要显示的PDF文件:

我是基于codeignter这么做的.

I am doing this on view of codeignter.

我做错了什么?请帮我.预先感谢

What I am doing wrong ? Please help me on this. thanks in advance

-

我正在执行以下操作:

foreach($orders as $order){

$path = $_SERVER['DOCUMENT_ROOT'].folder_name.'/resources/uploads/pdf/pdf_label_'.$order['id'].'.pdf';

$filename = 'pdf_label_'.$order['id'].'.pdf';

$file = $path;
$filename = $filename;

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

  }

那么如何在浏览器中显示多个文件?

So how can I show more than one file on browser ?

-

因此,按照下面的答案,我必须使用phpmerger合并多个PDF文件并显示到浏览器中.我浏览了该网站 http://pdfmerger.codeplex.com/,但无法用于codeignter.谁能帮我在我的编码器中使用这个phpmerger

So as per the answer below I have to use phpmerger to merge multiple PDF file and display into the browser. I gone through this website http://pdfmerger.codeplex.com/ but unable to use into codeignter. Can anyone please help me to use this phpmerger inside my codeingter

推荐答案

您可以使用 PDF Merger 库实现您的目标.此处是原始库的链接(已过时).更喜欢保留的myokyawhtun叉

you can use PDF Merger library to achive your goals. Here is the link to original library (outdated). Prefer myokyawhtun fork which is maintained

,示例代码如下

include 'PDFMerger.php';
$pdf = new PDFMerger;
$pdf->addPDF('path_to_pdf/one.pdf', '1, 3, 4')  //include file1 - pages 1,3,4
    ->addPDF('path_to_pdf/two.pdf', '1-2')      //include file2 -  pages 1 to 2
    ->addPDF('path_to_pdf/three.pdf', 'all')    //include file3 - all pages
    ->merge('browser', 'test.pdf');  // OUTPUT : make sure you choose browser mode here.

支持的模式-浏览器文件下载 string .

编辑:正如我所看到的,您已经标记了CI,您可以将 PDFMerger.php 放在应用程序/库中.并将其加载到 autoload.php controller

Edit : As i can see you have tagged CI you can put PDFMerger.php in applications/libraries. and load it in autoload.php or in controller

,然后可以像 $ this-> pdfmerger-> addPDF() merge()函数那样使用它.

and then can use it like $this->pdfmerger->addPDF() and merge() functions.

这篇关于在浏览器上显示PDF的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆