使用php打开pdf文件

时间:2010-06-04 10:01:47

标签: php

我使用php打开pdf文件,它运行良好,但它提示我在指定文件夹中下载该文件..我想要做的只是单击链接pdf文件打开而不提示下载..任何人都有关于它的想法?提前使用thanx ..以下代码

php代码:

$mypdf = PDF_new();
PDF_open_file($mypdf, "");
PDF_begin_page($mypdf, 595, 842);
$myfont = PDF_findfont($mypdf, "Times-Roman", "host", 0);
PDF_setfont($mypdf, $myfont, 10);
PDF_show_xy($mypdf, "hello my first pdf converted file", 50, 750);
PDF_show_xy($mypdf, "Made with the PDF libraries for PHP.", 50, 730);


PDF_end_page($mypdf);
PDF_close($mypdf);

$mybuf = PDF_get_buffer($mypdf);
$mylen = strlen($mybuf);
header("Content-type: application/pdf");
header("Content-Length: $mylen");
header("Content-Disposition: inline; filename=gen01.pdf");
print $mybuf;

PDF_delete($mypdf);

.....................................
html code:


<html>
<body>
Click here to see pdf file <a href="gen01.php" target="_blank">pdf1</a>
</body>
</html>

2 个答案:

答案 0 :(得分:1)

删除header("Content-Disposition: inline; filename=gen01.pdf"); 但如果访问者没有与'application / pdf'mime-type相关联的PDF阅读器,浏览器将下载该文件。

答案 1 :(得分:0)

1.创建一个名为fileHolder的mysql表,该表具有以下属性;

2.id,filename

3.然后使用HTML创建以下表单

<form enctype="multipart/form-data" action="upload.php" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="1000000" />
<input type="file" required name="uploaded_file">
<input type=submit value=upload >
</form>

4.然后使用以下代码

创建upload.php
if ((!empty($_FILES["uploaded_file"])) && ($_FILES['uploaded_file']['error'] == 0)) {
//Check if the file is JPEG image and it's size is less than 350Kb
$filename = basename($_FILES['uploaded_file']['name']);
$ext = substr($filename, strrpos($filename, '.') + 1);
if (($ext == "pdf") && ($_FILES["uploaded_file"]["type"] == "application/pdf") &&
($_FILES["uploaded_file"]["size"] < 350000000)) {
 //Determine the path to which we want to save this file
 $newname = dirname(__FILE__) . '\\Abstract\\' . $filename;
    //Check if the file with the same name is already exists on the server
    if (!file_exists($newname)) {
        //Attempt to move the uploaded file to it's new place
        if ((move_uploaded_file($_FILES['uploaded_file']['tmp_name'], $newname))) {
            echo "It's done! The file has been saved as: " . $newname;
        } else {
            echo "Error: A problem occurred during file upload!";
        }
    } else {
        echo "Error: File " . $_FILES["uploaded_file"]["name"] . " already exists";
    }
} else {
    echo "Error: Only .pdf images under 350MB are accepted for upload";
}
} else {
echo "Error: No file uploaded";
}

// ============================================= =========== 上面的代码负责将文件放在名为abstract的目录中。

最后,我们需要在浏览器上查看该文件。使用以下

在view.php文件中查询

//假设您有连接并选择db

header('Content-type: application/pdf');

select filename from fileHolder where id = 1;

if (empty($filename) && !empty($_GET['filename'])) /*file name comes from a link called 
$filename = $_GET['filename'];                     view which is used to identify the 
                                                    exact file that we are lookingfor*/
$filenames = "..\\user\\Abstract\\".$filename."";
$file = fopen($filenames, "r");

enter code here $ fread = fread($ file,filesize($ filenames));     echo $ fread;