使用domPDF处理动态PHP变量输出


Processing dynamic PHP variable output with domPDF

我一直在编写一个脚本,我应该从页面中动态创建的数据创建PDF。经过一番研究,我决定使用domPDF。我目前使用CodeIgniter作为我的框架,所以我提出了这个教程。因此,从本教程中,我得到了控制器的以下代码集。

控制器:dompdf_test.php-这将用于根据加载的视图生成实际的PDF。

class Dompdf_test extends CI_Controller {
    public function index() {   
        // Load all views as normal
        $this->load->view('summary/index');
        // Get output html
        $html = $this->output->get_output();
        // Load library
        $this->load->library('dompdf_gen');
        // Convert to PDF
        $this->dompdf->load_html($html);
        $this->dompdf->render();
        $this->dompdf->stream("SavedData.pdf");
    }
}

控制器:dompdf_view.php-这只是要访问的页面,将转换为PDF

class Dompdf_view extends CI_Controller {
    public function index()
    {
        $this->load->view('summary/index');
    }
}

库:dompdf_gen.php-位于应用程序/库

class Dompdf_gen {
    public function __construct() {
        require_once APPPATH.'third_party/dompdf/dompdf_config.inc.php'; //this is where the whole dompdf plugin is located
        $pdf = new DOMPDF();
        $CI =& get_instance();
        $CI->dompdf = $pdf;
    }
}

所以它是这样的:我想转换成PDF的页面产生的数据是由用户操纵的。生成的数据不是来自数据库,而是基于用户选择的数据。例如,用户选择女性作为性别。"女性"这个词将被添加到摘要选项卡中(在同一页上)。摘要选项卡中的所有数据都将包含在pdf文件中。我想把这些数据传递到另一个php文件,因为该页面包含了许多不相关的数据用于保存pdf,而domPDF不支持一些CSS语法(如果我的理解正确的话)。该文件中传递的数据将作为一个简单的PDF模板表的基础。

现在问题来了。我在传递数据时使用了这个非常简单的代码:

查看:summary/sourceData.php-我应该在其中输入数据的示例页。所有输入的数据都将在摘要/索引视图中传递。

<form method="post" action="http://siteURL/dompdf_view"> //dompdf_view is simply the summary/index page
 <input type="text" name="name0" placeholder="Add text"/><br/>
 <input type="text" name="name1" placeholder="Add text"/><br/>
 <input type="text" name="name2" placeholder="Add text"/><br/>
 <input type="text" name="name3" placeholder="Add text"/><br/> 
 <input type="text" name="name4" placeholder="Add text"/><br/>
<input type="submit" name="submit" value="Pass Variable"/>
</form>

summary/index.php-这是数据传递的地方。

 <?php 
$name0 = isset($_POST['name0']) ? $_POST['name0'] : "";
$name1 = isset($_POST['name1']) ? $_POST['name1'] : "";
$name2 = isset($_POST['name2']) ? $_POST['name2'] : "";
$name3 = isset($_POST['name3']) ? $_POST['name3'] : "";
$name4 = isset($_POST['name4']) ? $_POST['name4'] : "";
    echo $name0.'<br/><br/>'; 
    echo $name1.'<br/><br/>'; 
    echo $name2.'<br/><br/>'; 
    echo $name3.'<br/><br/>'; 
    echo $name4.'<br/><br/>';
    ?>
    <a href="<http://siteURL/dompdf_test>">Save as PDF</a>  

summary/sourceData.php中的数据被传递到summary/index.php,但当我尝试将summary/index.php保存到pdf时,它会保存但不会获得任何数据。这只是一个空白的PDF文档,除了单词"另存为PDF"。

我研究并尝试添加,但仍然没有成功。

domPDF无法读取php echo文件?如果没有,怎么办?

我还不知道如何将这些数据保存为pdf。或者,还有其他方法可以传递这些数据吗?

注意:如果页面是静态页面,则上面的代码有效。但是当我添加动态数据时,它不会读取PDF文件中该页面的PHP结果。

如有任何帮助/建议,我们将不胜感激。

我们为我的问题找到了解决方案。提交的数据没有传递给控制器,这就是它为空的原因。我们所做的是,我们通过输入帖子提交了数据,并在控制器中添加了以下内容:

$html = $this->load->view('summary/index', $data, true);

谢谢你的帮助!:)

当您通过单击链接"另存为pdf"生成pdf时,您将从视图摘要/索引视图触发一个新请求,而不需要任何POST参数。然后,生成pdf的控制器被称为服务器端,并为pdf生成呈现摘要索引视图,但现在没有任何后期参数。这就是为什么你的pdf是空的。您可以更换

<a href="<http://siteURL/dompdf_test>">Save as PDF</a> 

带有:

  if($_POST['submit']!='Save as PDF'){
    ?>
      <form method="post" action="http://siteURL/dompdf_test"> 
        <input type="hidden" name="name0" value="<?php echo htmlspecialchars($name0,ENT_COMPAT,'UTF-8')?>"/><br/>
        <input type="hidden" name="name1" value="<?php echo htmlspecialchars($name1,ENT_COMPAT,'UTF-8')?>" /><br/>
        <input type="hidden" name="name2" value="<?php echo htmlspecialchars($name2,ENT_COMPAT,'UTF-8')?>"/><br/>
        <input type="hidden" name="name3" value="<?php echo htmlspecialchars($name3,ENT_COMPAT,'UTF-8')?>"/><br/> 
        <input type="hidden" name="name4" value="<?php echo htmlspecialchars($name4,ENT_COMPAT,'UTF-8')?>"/><br/>
        <input type="submit" name="submit" value="Save as PDF"/>
      </form>
    <?php
  }

这应该会给您一个关于dompdf如何工作的提示。

        //File when you gererate $html
        $dir = dirname(__FILE__);
        require_once($dir.'/file_to_generate_html_from_php.php');  
        // Load the dompdf files  
        require_once($dir.'/include/dompdf/dompdf_config.inc.php');  
        $dompdf = new DOMPDF(); // Create new instance of dompdf  
        $dompdf->load_html($html); // Load the html  
        $dompdf->render(); // Parse the html, convert to PDF  
        $pdf_content = $dompdf->output(); // Put contents of pdf into variable for later

file_to_generate_html_from_php.php-应该是这样的。

$html = '
<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <html lang="sv">
</head>
<body>';
$html .='<h2>Report</h2>';
$html .='
</body>
</html>';

您还可以运行php代码在file_to_generate_html_from_php.php内生成$html

$SQLQUERY= mysql_query("SELECT * FROM table WHERE Column = '$variable'") or die(mysql_error());
while($row = mysql_fetch_array( $SQLQUERY)) {
                                                // echo out the contents of each row into a table
                                                if(empty($row['Column'])){
                                                $html .= '<p>No results!</p>';
                                                }
                                                else{
                                                $html .= '<pre>Yes we found this: '.$row['Column'].'</pre>';
                                                }
                                        }