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

时间:2018-06-21 11:25:22

标签: javascript php html pdf window.open

无法在新的浏览器窗口中打开PDF。我有一个PHP代码用来设置PDF并打开它!

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

我正在尝试在新的(Chrome)浏览器窗口中打开PDF文件。

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

错误是无法加载PDF文档。

当我执行document.getElementById("edit").action ="date_PDF.php";时,它将打开pdf。

我从ftp服务器获取PDF文件,然后将其显示在date_PDF.php @readfile($ftp_path_pdf_in.$qwerty);

问题出在哪里? 谢谢!

1 个答案:

答案 0 :(得分:0)

您尚未定义name_check html元素。另外,您尚未在网址中传递“ type”参数。我认为name_check和type存在于您打开pdf文件的页面上。

如果要使用window.open打开pdf文件,则应使用以下网址:date_PDF.php?type = in

在readfile语句之后,您应该有一个exit语句。请参见readline函数的示例:http://php.net/manual/en/function.readfile.php#refsect1-function.readfile-examples