2013-08-04 40 views
2

我想顯示PHP中的PDF文件中顯示PDF,我用:如何在PHP

<html> 
<head> 
<style type="text/css"> 
#myiframe {width:600px; height:100%;} 
</style> 
</head> 
<body> 
<div id="scroller"> 
<iframe name="myiframe" id="myiframe" src="xml.pdf"> 
</div> 
</body> 
</html> 

在HTML中工作得很好,但是當我想用這個代碼到一個PHP文件,它什麼也不顯示,只嘗試下載pdf文件。 任何人都可以幫助我嗎?

+2

那裏沒有PHP代碼。你如何使用PHP? – ironcito

+0

您是否使用過.php文件擴展名? –

+0

您的代碼對我有用 –

回答

15

有相當多的選項可以使用:(都測試)。

這裏有兩種方法。

header("Content-type: application/pdf"); 
header("Content-Disposition: inline; filename=filename.pdf"); 
@readfile('path\to\filename.pdf'); 

或:(注意轉義雙引號)。給它分配一個名字時也需要使用它。

<?php 

echo "<iframe src=\"file.pdf\" width=\"100%\" style=\"height:100%\"></iframe>"; 

?> 

I.e.name="myiframe" id="myiframe"

將需要更改爲:

name=\"myiframe\" id=\"myiframe\" PHP內。

請務必看看:this answer on SO瞭解更多關於此主題的選項。

腳註:嘗試在Windows 8中查看PDF文件時存在已知問題。如果未安裝瀏覽器插件,安裝Adobe Acrobat Reader是查看這些類型文檔的更好方法。

2

https://pdfobject.com/試試這個下面的代碼

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

Demo

0

下載PDFObject庫,並檢查下面的代碼:我希望它會工作,你。

<!DOCTYPE html> 
<html> 
<head> 
    <title>Pdf Read</title> 
    <style> 
      .pdfobject-container { height: 500px;} 
      .pdfobject { border: 1px solid #666; } 
    </style> 
    <script src="pdfobject.min.js"></script> 
</head> 
<body> 
     <div id="example1"></div> 
     <script>PDFObject.embed("pdfread.pdf", "#example1");</script> 
</body> 
</html>