使用domPDF处理动态PHP变量输出 [英] Processing dynamic PHP variable output with domPDF

查看:212
本文介绍了使用domPDF处理动态PHP变量输出的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I've been working on a script where the I should create a PDF from a dynamically created data from a page. After some research, I've decided to use domPDF. I currently use CodeIgniter as my framework so I came up with this tutorial. So from this tutorial, I got the following set of codes for the controller.

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

Controller: dompdf_test.php - This will be used for generating the actual PDF based on the loaded view.

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

Controller: dompdf_view.php - This is just the page to be accessed that will be converted to PDF

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

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

Library: dompdf_gen.php - Located at application/libraries

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模板表的基础。

So here it goes: the page that I want to convert to PDF produces data that are manipulated by a user. Generated data are not from database but based on what's selected by a user. For instance, user selected Female as the gender. That "Female" word will be added to a summary tab (on the same page). All data in summary tab will be included on the pdf file. I thought of passing those data to another php file since that page includes a lot of unrelated data for pdf saving and domPDF does not support some CSS syntax (if my understanding was right). Data passed at that said file will be the basis of a simple PDF template tables.

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

Now here's the problem. I used this very simple code on passing of data:

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

View: summary/sourceData.php - sample page where I should input the data. All entered data will be passed in summary/index view.

<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 - 这是数据传递的地方。

summary/index.php - This is where the data is passed.

 <?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,它保存,但没有得到任何数据。它只是一个BLANK PDF文档,除了单词另存为PDF之外。

Data from summary/sourceData.php are passed to summary/index.php but when I try to save summary/index.php to pdf, it saves but does not get any data. It's just a BLANK PDF document except for the word Save as PDF.

我研究并尝试添加但仍没有运气。

I researched and tried adding but still no luck.

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

Can't domPDF read php echo files? If not, how?

我不知道如何将这些数据保存为pdf。或者,是否有另一种方式传递这些数据?

I have no idea yet how I will be able to save those data to pdf. Or, is there another way to pass those data?

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

NOTE: Code above is working if the page is a static one. But when I add dynamic data, it does not read the PHP results from that page inside the PDF file.

任何帮助/建议将非常感谢。

Any help/advices will be greatly appreciated.

推荐答案

我们想出了一个问题的解决方案。提交的数据未传递给控制器​​,因此它为空。我们做的是,我们通过输入post提交数据,并在控制器中添加以下内容:

We came up with a solution to my problem. Data submitted was not passed to the controller that's why it's blank. What we did was, we submitted the data through input post and added the following in the controller:

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

感谢所有的帮助! :)

Thanks for all the help! :)

这篇关于使用domPDF处理动态PHP变量输出的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆