dpwdldgn43486 2018-06-21 11:25
浏览 470

尝试在新的浏览器窗口中打开pdf文件

Cant get the PDF open in new browser window. There is a php code I use to set the PDF and open it!

<?php
if (($_POST['name_check']=="in") || ($_GET['type']=="in")){
    header('Content-Type: application/pdf; charset=windows-1257');
    header("Content-Disposition: inline; ");
    header('Accept-Ranges: bytes');
    $file_type = "in";
    $flag = 0;
}
else{
    header('Content-Type: application/pdf; charset=windows-1257');
    header("Content-Disposition: inline;");
    header('Accept-Ranges: bytes');
    $file_type = "out";
    $flag = 1;
}

?>
<?php session_start();   
    if (((isset($_SESSION['UserID'])) and (isset($_SESSION['Pin_code']))) and (isset($_COOKIE['CookieMy']))){
        $form='<form action="login.php" method="get" style="width: 900px;margin: -1px auto -57px auto;" class="exit_but">
        <input type="submit" name="exit" value="EXIT" style="margin-left: 800px;"></form>';
        echo $form;         
    } else {
        header("Location: login.php"); 
    }
?>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/style1.css" />
<script src="http://code.jquery.com/jquery-latest.min.js"></script>
    <script>
        idleTimer = null;
        idleState = false;
        idleWait = 1200000; 
        var theElement = document.getElementById("elem");

        $(document).ready(function(){
          $(document).bind('mousemove keydown scroll click', function(){
            clearTimeout(idleTimer);
            idleState = false;
            idleTimer = setTimeout(function(){
               idleState = true;
               window.location.href = "login.php"             
            }, idleWait);
          });
        });
    </script>
</head>
<body>

<?php 

    function last(){
            if (($_POST['name_check'])=="in"){
                $type = '"pdf-in"';
                $flag=0;
                echo "<script>window.flag_in=false;</script>";
            }
            else{
                $type = '"pdf-out"';
                $flag=1;
                echo "<script>window.flag_in=true;</script>";
            }
            
        if (!isset($aircraftid)){
                    $aircraftid = $_POST['hostcountry'];
                    $aircraftid='"'.$aircraftid.'"';
            }
        if (!isset($date)){
                $date = $_POST['per1'];
                $date=date($date);
            }
        
        include ("config.php");
        
        //////////////////////////////////////////
        
        
        $mysqli = new mysqli($dbconf['host'], $dbconf['user'], $dbconf['password'], $dbconf['dbname']);
        if ($mysqli->connect_errno) {
            echo "MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
        }
        
        $query = mysqli_multi_query( $mysqli, "SELECT dn_get_flight_file_name($date,$aircraftid,$type);") or die( mysqli_error( $mysqli ) ); 
        if( $query ) 
        { 
            if ($result = $mysqli->store_result()) {
                    while ($row = $result->fetch_row()) {
                        $PDF_File_Name=$row[0];
                    }
                    $result->free();
                }
        }
        
        ///////////////////////////////////
        
        mysqli_close( $mysqli ); 
        $qwerty=$PDF_File_Name;

        
        ///////////////////////////////////
        $ftp .='ftp://'.$ftp_user.':'.$ftp_pwd.'@'.$ftp_server;
        
        if($flag==0){
            $file_path_ftp = $ftp_path_pdf_in.$qwerty;
            $file_path_local = "in/".$qwerty;
        }
        else{   
            $file_path_ftp = $ftp_path_pdf_out.$qwerty;
            $file_path_local = "out/".$qwerty;
        }
        
            
            $file_path_ftp = trim($file_path_ftp);
            $file_path_local = trim($file_path_local);
            $file = $file_path_local;
            $current_content = file_get_contents($ftp.$file_path_ftp);
            file_put_contents($file, $current_content);
            $qwerty=$file_path_local;
        
        $date2 = $_POST['date'];
        $num = $_POST['num'];
        return array ($qwerty,$date2,$num);
    }

    if (isset($_GET['name'])){
        $qwerty=$_GET['name'];  
    }
    else {
        list ($qwerty,$date2,$num) = last();
    }
    if (!file_exists($qwerty)) {
        $file_path_local=$qwerty;
        $slash_occur = stripos($qwerty, '/');
        $file_type   = substr($qwerty, 0, $slash_occur);
            $file_name   = substr($qwerty, $slash_occur+1);
        if($file_type=='pdf-in'){
            $file_path_ftp = $ftp_path_pdf_in.$file_name;
        }
        else{   
            $file_path_ftp = $ftp_path_pdf_out.$file_name;
        }
        echo "<h2>file_type = ".$file_type.", file_name = ".$file_name."</h2>";
        $ftp .='ftp://'.$ftp_user.':'.$ftp_pwd.'@'.$ftp_server;
        $current_content = file_get_contents($ftp.$file_path_ftp);
        file_put_contents($qwerty, $current_content);
    }

    if (isset($_GET['date'])){
        $date2=$_GET['date'];
    }
    if (isset($_GET['num'])){
        $num=$_GET['num'];  
    }
?>

<?php
if (($_POST['name_check']=="in") || ($_GET['type']=="in")){
    @readfile($ftp_path_pdf_in.$qwerty);
} else {
    @readfile($ftp_path_pdf_out.$qwerty);
}
?>

</body>
</html>

I'm trying to open PDF file in the new (Chrome) browser window.

document.action =window.open('date_PDF.php', 'newWindow', 'width=1000, height=1000');

Error is Failed to load PDF document.

When I do document.getElementById("edit").action ="date_PDF.php"; it opens pdf.

I get the PDF file from the ftp server and than display it on date_PDF.php @readfile($ftp_path_pdf_in.$qwerty);

Where is the problem? Thank you!

</div>
  • 写回答

1条回答 默认 最新

  • duanqian6295 2018-06-22 02:56
    关注

    You have not defined the name_check html element. Also you have not passed the 'type' parameter in the url. I think the name_check and type are present on the page from where you open the pdf file.

    If you want to open the pdf file using window.open then you should use the url: date_PDF.php?type=in

    After the readfile statement you should have an exit statement. See the example for the readline function: http://php.net/manual/en/function.readfile.php#refsect1-function.readfile-examples

    评论

报告相同问题?

悬赏问题

  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本