PHP另存为PDF不起作用

Posted

技术标签:

【中文标题】PHP另存为PDF不起作用【英文标题】:PHP Save as PDF not working 【发布时间】:2016-10-11 06:03:29 【问题描述】:

美好的一天!

我有一个下拉菜单,可以选择员工编号并根据该特定选择显示表格数据。我已另存为 pdf 代码,它将表格数据保存为 pdf。它工作正常,但我的表格列标题只显示不值。

我在一个包含 javascript 的文件中有下拉菜单,该文件被发送到显示我的表格的另一个文件。

以下是我所有文件的代码,以及到目前为止我为此尝试过的代码。

对于下拉菜单:

    <?php 
include("connect-db.php");
?>
<html>
<head>
<title>PHP insertion</title>
        <link rel="stylesheet" href="css/insert.css" />
        <link rel="stylesheet" href="css/navcss.css" />
<script>
function showUser(str) 
    if (str == "") 
        document.getElementById("txtHint").innerHTML = "";
        return;
     else 
        if (window.XMLHttpRequest) 
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
         else 
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        
        xmlhttp.onreadystatechange = function() 
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) 
                document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
            
        ;
        xmlhttp.open("GET","search_empnumber.php?emp_id="+str,true);
        xmlhttp.send();
    


/*function showAssessories(acc) 
    if (acc == "") 
        document.getElementById("txtHint2").innerHTML = "";
        return;
     else 
        if (window.XMLHttpRequest) 
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
         else 
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        
        xmlhttp.onreadystatechange = function() 
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) 
                document.getElementById("txtHint2").innerHTML = xmlhttp.responseText;
            
        ;
        xmlhttp.open("GET","getassessories.php?emp_id="+acc,true);
        xmlhttp.send();
    
*/
</script>
</head>
<body>
<div class="maindiv">
            <?php include("includes/head.php");?>
            <?php include("menu.php");?>
            <div class="form_div">


<form>
<div class="forscreen">
        <h1>Search  Records:</h1> 
        <p>You  may search by Employee Number</p>

<select name="employeenumber" class="input" onchange="showUser(this.value)">
  <option value="">Please Select Employee Number:</option>
  </div>
  <?php 
  $select_emp="SELECT distinct(emp_number) FROM employees"; 
  $run_emp=mysql_query($select_emp);
  while($row=mysql_fetch_array($run_emp))
      $emp_id=$row['emp_id'];
      $first_name=$row['first_name'];
      $last_name=$row['last_name'];
      $emp_number=$row['emp_number'];
      $total_printers=$row['total_printers'];
      $total_scanners=$row['total_scanners'];
      $total_pc=$row['total_pc'];
      $total_phones=$row['total_phones'];
      $other_equips=$row['other_equips'];

?>
  <option value="<?php echo $emp_number;?>"><?php echo $emp_number;?></option>
  <?php  ?>
  </select>
  <html>


</form>
</div>
<br>
<div id="txtHint"></div>
<br>
<div id="txtHint2"></div>

</div>
<?php include 'includes/foot.php';?>
</body>

</html>

下拉功能:onchange="showUser(this.value)"

function showUser(str) 
    if (str == "") 
        document.getElementById("txtHint").innerHTML = "";
        return;
     else 
        if (window.XMLHttpRequest) 
            // code for IE7+, Firefox, Chrome, Opera, Safari
            xmlhttp = new XMLHttpRequest();
         else 
            // code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        
        xmlhttp.onreadystatechange = function() 
            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) 
                document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
            
        ;
        xmlhttp.open("GET","search_empnumber.php?emp_id="+str,true);
        xmlhttp.send();
    

search_empnumber.php 文件:

<!DOCTYPE html>
<html>
<form action="search_empnumber.php" method="POST">
<input type="submit" value="SAVE AS PDF" class="btnPDF" name="submit" />
</form>

<head>
<style>
table 
    width: 100%;
    border-collapse: collapse;


table, td, th 
    border: 1px solid black;
    padding: 5px;


th text-align: left;
.link color:#00F; cursor:pointer;
</style>
</head>
<body>

<?php
//$emp_id =($_REQUEST['emp_id']);
$emp_id =($_REQUEST['emp_id']);
$count=1;
include("connect-db.php");
 //$sql = "SELECT * from employees where department='".$department."'";
 //$sql = "select * from employees as pg inner join accessories as pd on pg.emp_number= pd.emp_number ";
//$result = mysql_query($sql) or die(mysql_error());

//$result = mysql_query($query) or die(mysql_error());
//$emp_id =($_POST['employeenumber']);
$sql="SELECT * FROM employees WHERE emp_number = '".$emp_id."'";
$result = mysql_query($sql) or die(mysql_error());

$count=1;
echo "<table>
<tr>
<th>S.No</th>
<th>First Name</th>
<th>Last Name</th>
<th>Employee Number</th>
<th>Department</th>
<th>Email</th>
<th>Total Printers</th>
<th>Total Scanners</th>
<th>Total Pc</th>
<th>Total Phones</th>
<th>Other Equipments</th>

</tr>";
while($row = mysql_fetch_array($result))
    echo "<tr>";
    echo "<td>" . $count . "</td>";
    echo "<td>" . $row['first_name'] . "</td>";
    echo "<td>" . $row['last_name'] . "</td>";
    echo "<td>" . $row['emp_number'] . "</td>";
    echo "<td>" . $row['department'] . "</td>";
    echo "<td>" . $row['email'] . "</td>";
    echo "<td>" . $row['total_printers'] . "</td>";
    echo "<td>" . $row['total_scanners'] . "</td>";
    echo "<td>" . $row['total_pc'] . "</td>";
    echo "<td>" . $row['total_phones'] . "</td>";
    echo "<td>" . $row['other_equips'] . "</td>";

    echo "</tr>";
    $count++;

 
echo "</table>";
echo '<div class="forscreen">';
      // echo '<br />';echo '<button class="btnPrint" type="submit" value="Submit" onclick="window.print()">Print</button>';
      //echo '<input type="submit" value="SAVE AS PDF" class="btnPDFsearch" name="submit"/>';
     // echo '<input type="submit" value="SAVE AS PDF" class="btnPDF" name="submit" />';

      echo '</div>';
echo '<div class="forscreen">';
      //echo '<br />';echo '<button class="btnPrevious" type="submit" value="Submit" onclick="history.back(); return false;">Previous Page</button>';

       echo '</div>';


?>

</body>
</html>

PDF 代码(我试过)在 search_empnumber 的同一文件中:

<?php 
error_reporting(E_ALL ^ E_DEPRECATED);
include('connect-db.php');
?>
<?php
//if($_POST['submit'])
if (isset($_POST['submit']))
    require_once 'includes/practice.php';
 $pdf->SetFont('times', '', 11);
  $tb1 = '</br></br>


                            <table cellspacing="0" cellpadding="5" border="1"> 

                                <tr style="background-color:#f7f7f7;color:#333; ">

                                    <td >First Name</td>

                                    <td >Last Name</td>

                                      <td >Employee Number</td>

                                      <td >Department</td>

                                    <td >Email</td>

                                    <td >Total Printers</td>
                                     <td >Total Scanners</td>
                                      <td >Total PCs</td>
                                      <td >Total Phones</td>
                                       <td >Other Equipments</td>



                                </tr>

                            </table>';


include('connect-db.php');
//$emp_id =($_POST['emp_id']);
$emp_id =($_POST['employeenumber']);
$result1= mysql_query("SELECT * FROM employees where emp_number = '".$emp_id."'");

        while($row = mysql_fetch_assoc($result1))
            $tb2 = '<table cellspacing="0" cellpadding="5" border="1"> 
                <tr> 
                    <td >' . $row['first_name'] . '</td>
                    <td >' . $row['last_name'] . '</td>
                    <td >'. $row['emp_number'] .'</td>
                    <td >'.$row['department'].'</td>
                    <td >'.$row['email'].'</td>
                    <td >'.$row['total_printers'].'</td>
                    <td >'.$row['total_scanners'].'</td>
                    <td >'.$row['total_pc'].'</td>
                    <td >'.$row['total_phones'].'</td>
                     <td >'.$row['other_equips'].'</td>

                </tr>
            </table>';
            $tb1 = $tb1.$tb2;
        


       $pdf->writeHTML($tb1, true, false, false, false, '');
       ob_end_clean();
      $pdf->Output('Report.pdf', 'D');



?>

没有添加文件,即practice.php 和 pdf 库,因为它们只是以定义的格式创建表格

【问题讨论】:

试试 fpdf : fpdf.org Pdf 库不是问题。我已经在使用 tcpdf 目前还不是很清楚代码的哪一部分出错了。我在构建选择列表的部分看到了一些奇怪的东西。是这个问题吗? @BenHillier PDF 生成的部分只是错误的。 PDF 已创建,但仅显示表列而不显示值。我认为查询是错误的 1) 我在结束 &lt;/div&gt; 时指出的问题仍然存在。您无法确定是否发送了任何内容 2) 您从未为 &lt;form 标记指定 method,因此它可能会发送获取请求。然而你尝试将其作为$_POST 变量读回 【参考方案1】:

您混淆了GETPOST。尝试以下一种(或两种):

1) 在下拉代码中,将&lt;form&gt; 替换为:

<form method="POST">

2) 在表生成代码中,使用以下命令读回值:

$emp_id =($_REQUEST['employeenumber']);

还有:

if (isset($_REQUEST['submit']))

为了增加您的问题,在您发布的search_empnumber.php 的第一个版本中,您回读$emp_id =($_REQUEST['emp_id']);,而发布的值实际上是在$_REQUEST['employeenumber'] 中。

【讨论】:

对于 $emp_id =($_REQUEST['employeenumber']);它说,未定义的索引员工编号...【参考方案2】:

我不知道您的哪一部分出了问题,但下拉代码中发生了一些奇怪的事情:

   <select name="employeenumber" class="input" onchange="showUser(this.value)">
      <option value="">Please Select Employee Number:</option>
      </div>  <!-- ISSUE: Closed a div which was never opened. You're lucky the select box even shows anything-->
      <?php 
      $select_emp="SELECT distinct(emp_number) FROM employees"; 
      $run_emp=mysql_query($select_emp);

      //ISSUE: You loop through a lot of data, but then do nothing with it. With every pass of the loop, you simply change the variables, and nothing else. I think you meant to move your <option> into the loop
      while($row=mysql_fetch_array($run_emp))

          //ISSUE: You only selected 'emp_number' in your query. Most of these array keys are never filled
          $emp_id=$row['emp_id'];  
          $first_name=$row['first_name'];
          $last_name=$row['last_name'];
          $emp_number=$row['emp_number'];  //This is the only one filled
          $total_printers=$row['total_printers'];
          $total_scanners=$row['total_scanners'];
          $total_pc=$row['total_pc'];
          $total_phones=$row['total_phones'];
          $other_equips=$row['other_equips'];

    ?>
      <!--ISSUE: Only the last $emp_number from the query is filled. The rest you kept overwriting in your loop-->
      <option value="<?php echo $emp_number;?>"><?php echo $emp_number;?></option>
      <?php  ?>
      </select>

【讨论】:

这不是一个完整的下拉代码,我以为你会得到。不管怎样,我把它贴在那里。

以上是关于PHP另存为PDF不起作用的主要内容,如果未能解决你的问题,请参考以下文章

保存前不适用于另存为

在 Windows PHP exec() 上运行(生成 PDF)soffice LibreOffice 不起作用

SaveAs 命令不起作用,但 SaveCopyAs 可以

用 .htaccess 重写不起作用

saveAs for Excel 文件在 VB.NET 中不起作用

使用VB自动化IE“目标另存为”