php 如何在php中显示pdf

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18040386/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 16:51:06  来源:igfitidea点击:

How to display pdf in php

phppdf

提问by Lamiya

I'm trying to display a pdf file in php, I used:

我试图在 php 中显示一个 pdf 文件,我用过:

<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>

it works in html well, but when I want to used this code into a php file, it displays nothing and only try to download the pdf file. Can anybody help me?

它在 html 中运行良好,但是当我想将此代码用于 php 文件时,它什么也不显示,只尝试下载 pdf 文件。有谁能够帮助我?

回答by Funk Forty Niner

There are quite a few options that can be used: (both tested).

有很多选项可以使用:(都经过测试)。

Here are two ways.

这里有两种方法。

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

or:(note the escaped double-quotes). The same need to be use when assigning a name to it.

或:(注意转义的双引号)。为其分配名称时也需要使用相同的方法。

<?php

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

?>

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

name="myiframe" id="myiframe"

would need to be changed to:

需要改为:

name=\"myiframe\" id=\"myiframe\"inside PHP.

name=\"myiframe\" id=\"myiframe\"在 PHP 里面。

Be sure to have a look at: this answer on SOfor more options on the subject.

请务必查看:this answer on SO有关该主题的更多选项。

Footnote: There are known issues when trying to view PDF files in Windows 8. Installing Adobe Acrobat Reader is a better method to view these types of documents if no browser plug-ins are installed.

脚注:尝试在 Windows 8 中查看 PDF 文件时存在已知问题。如果未安装浏览器插件,安装 Adob​​e Acrobat Reader 是查看这些类型文档的更好方法。

回答by M. Lak

Try this below code

试试下面的代码

<?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

演示

回答by Ravi Shrimali

Download PDFObject library from https://pdfobject.com/and check the below code: I hope it will work you.

https://pdfobject.com/下载 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>

回答by Jesrel Pabillon Letrondo

if(isset($_GET['content'])){
  $content = $_GET['content'];
  $dir = $_GET['dir'];
  header("Content-type:".$content);
  @readfile($dir);
}

$directory = (file_exists("mydir/"))?"mydir/":die("file/directory doesn't exists");// checks directory if existing.
 //the line above is just a one-line if statement (syntax: (conditon)?code here if true : code if false; )
 if($handle = opendir($directory)){ //opens directory if existing.
   while ($file = readdir($handle)) { //assign each file with link <a> tag with GET params
     echo '<a target="_blank" href="?content=application/pdf&dir='.$directory.'">'.$file.'</a>';
}

}

}

if you click the link a new window will appear with the pdf file

如果您单击链接,将出现一个带有 pdf 文件的新窗口

回答by Sayali Patil

Simple way to display pdf files from database and we can download it.
$resume is pdf file name which comes from database.
../resume/filename is path of folder where your file is stored.

从数据库中显示pdf文件的简单方法,我们可以下载它。
$resume 是来自数据库的 pdf 文件名。
../resume/filename 是存储文件的文件夹的路径。

<a href="../resumes/<?php echo $resume; ?>"/><?php echo $resume; ?></a>