문제

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.

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");
    }
}

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');
    }
}

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;
    }
}

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:

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

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.

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

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?

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.

도움이 되었습니까?

해결책

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! :)

다른 팁

When you are generating your pdf by clicking the link "Save as PDF" you trigger a new request from the view summary/index view without any POST parameters. Then your controller to that is generating the pdf is called server side and renders the summary index view for pdf generation, but now without any post parameters. That is why your pdf is empty. You can replace

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

with:

  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
  }

this should give you a hint on how dompdf works.

        //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 - Should look something like this.

$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>';

You can also run php code to generate $html inside file_to_generate_html_from_php.php

$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>';
                                                }
                                        }
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top