【问题标题】:Trying to open pdf file in new browser window尝试在新的浏览器窗口中打开 pdf 文件
【发布时间】:2018-06-21 11:25:21
【问题描述】:

无法在新的浏览器窗口中打开 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);

问题出在哪里? 谢谢!

【问题讨论】:

  • window.open() 的路径是否正确?
  • date_PDF.php 需要打开,所以是的,它确实采用了正确的路径
  • 您正在混合 HTML、JavaScript 和 PDF 输出。如果您要发送 PDF,则不得发送 PDF 以外的其他内容!

标签: javascript php html pdf window.open


【解决方案1】:

您尚未定义 name_check html 元素。此外,您还没有在 url 中传递 'type' 参数。我认为 name_check 和 type 出现在您打开 pdf 文件的页面上。

如果您想使用 window.open 打开 pdf 文件,那么您应该使用 url:date_PDF.php?type=in

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

【讨论】:

  • 我会试着检查一下。谢谢!
猜你喜欢
  • 1970-01-01
  • 2016-08-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-22
  • 1970-01-01
相关资源
最近更新 更多