php 在浏览器上显示 PDF
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/27039691/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Display PDF on browser
提问by Rakesh Shetty
I want to display a PDF file on browser which is store on our server. Here is my code :-
我想在存储在我们服务器上的浏览器上显示一个 PDF 文件。这是我的代码:-
$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 File to show :
要显示的 PDF 文件:
I am doing this on view of codeignter.
我是根据codeignter的观点来做这件事的。
What I am doing wrong ? Please help me on this. thanks in advance
我做错了什么?请帮我解决这个问题。提前致谢
EDIT :-
编辑 :-
Im doing something like below :
我在做类似下面的事情:
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 ?
那么如何在浏览器上显示多个文件呢?
Edit 2 :-
编辑 2 :-
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
因此,根据下面的答案,我必须使用 phpmerger 合并多个 PDF 文件并显示到浏览器中。我浏览了这个网站http://pdfmerger.codeplex.com/但无法使用到 codeignter。任何人都可以帮我在我的编码器中使用这个 phpmerger
采纳答案by Karan Thakkar
you can use PDF Merger
library to achive your goals.
Here is the link to original library (outdated). Prefer myokyawhtun fork which is maintained
您可以使用PDF Merger
库来实现您的目标。
这是原始库(已过时)的链接。更喜欢维护的 myokyawhtun 叉子
and a sample code will be as following
示例代码如下
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.
Supported modes- browser
, file
, download
and string
.
支持的模式- ,,和。browser
file
download
string
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
编辑:我可以看到你所标记CI,你可以把PDFMerger.php
在applications/libraries
。并将其装入autoload.php
或装入controller
and then can use it like $this->pdfmerger->addPDF()
and merge()
functions.
然后可以像$this->pdfmerger->addPDF()
和merge()
功能一样使用它。
回答by arshad
$filePath="file path here";
$filename="file name here";
header('Content-type:application/pdf');
header('Content-disposition: inline; filename="'.$filename.'"');
header('content-Transfer-Encoding:binary');
header('Accept-Ranges:bytes');
@ readfile($filePath);
please try this code i hope it will working as i try.
请试试这个代码,我希望它能像我一样工作。
回答by Darius
my recommended library for embedding PDFs is PDFObject. Check it out here: http://pdfobject.com/
我推荐的用于嵌入 PDF 的库是 PDFObject。在这里查看:http: //pdfobject.com/
回答by RichardBernards
Instead of using
而不是使用
@readfile($file);
Use
用
echo file_get_contents($file);
Or ommit the @
或者省略 @
回答by Mike Volmar
This method works if you want to display a pdf in the browser page.
如果您想在浏览器页面中显示 pdf,则此方法有效。
$file = 'filename.pdf';
echo "<embed src='$file' type='application/pdf' width='80%' height='600px' />";
回答by Siddu Loni
Friends just use Anchor Tag that's it. Now-day's all newer browsers supports view of any files.
朋友只需使用Anchor Tag就可以了。现在所有较新的浏览器都支持查看任何文件。