Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I pass data to the Codeigniter output class without displaying it?

I'm working on a way for users to be able to generate PDF copies of invoices and other tabular data. To do this, I've wrapped dompdf into a library that I can use with CI and created a method that will generate a PDF based on the return value of CI's output->get_output(). The wrapper is similar to this one on Github.

The problem is, I can't figure out a way to get the view (and HTML/CSS needed for the PDF) into CI's output class other than load->view(), which is going to write to the browser.

My only other choice would be to use curl to request the page, but that seems so silly to do since I can get it right from the output buffer. I just don't want the HTML sent to the browser, since I set headers telling the browser to expect a PDF.

To be clear, this is what I want to accomplish (in the order that I want to accomplish it):

  1. Do everything I'd normally do to prepare the view for display
  2. Load the view into the CI output class, but not display it
  3. Pass the return value of output->get_output() to my dompdf library
  4. Set the appropriate headers
  5. Execute my dompdf method that will send the PDF to the browser

I don't see any way of doing step 2 based on the output class documentation.

Is it possible to get a view into the output class without displaying it? If so, how? I'm using CI 2.0.3.

Edit

The very helpful Anthony Sterling pointed out that I can just get what I want from the loader class by setting the third argument telling it to return a string rather than render the view to TRUE. E.g.:

$lotsaHtml = $this->load->view('fooview', $somearray, TRUE);

And that would be better in my particular instance since I don't need to load partials. However, this is still a valid and (I think) interesting question, it would also be handy to know if I could get the same from the OB, perhaps if I did have a bunch of partials. Those could be concatenated, but yuck.

It seems like I should be able to get the output class to not render anything (else, why does get_output() exist?) so I can do something else with everything it knows about. I just can't find a way to make that happen.

Edit 2

Some pseudo (but not far from reality) code illustrating what I hope to do, by showing what I did and then explaining what I actually wanted to do.

Let's say I have a public method genpdf($id) in a controller named invoice using a model named inv:

public function genpdf($invoiceId) {
    $this->load->library('dompdflib');
    $this->pagedata['invoice_data'] = $this->inv->getInvoice($invoiceId);
    $html = $this->load->view('pdfgen', $this->pagedata, TRUE);
    $this->dompdflib->sendPdf($html);
}

That is almost identical to code that I have that works right now. There, I ask the loader to parse and give me the results of the pdfgen view as a string, which I pass to the function in my dompdf wrapper that sets headers and sends the PDF to the browser.

It just seemed like this would be easy to do by just getting the output buffer itself (after setting headers correctly / etc).

Or do I just have to call the output class append_output() in succession with every partial I load?

Multiple methods loading a plethora of models need to work together to generate these (they're going in as an afterthought), so I was hoping to just collect it all and retrieve it directly from the output class. It could be that I just have to talk gradually to output->append_output() to make that happen.

like image 851
Tim Post Avatar asked Aug 31 '12 06:08

Tim Post


People also ask

How pass data from one page to another in CodeIgniter?

call first controller from first view and pass form data to second view. On second view you can create hidden inputs and set their values from controller data. Now submit the second form to final controller and you will get all the values of both form. Hope this helps you.

What is $data in CodeIgniter?

$data should be an array or an object: http://codeigniter.com/user_guide/general/views.html $data = array( 'title' => 'My Title', 'heading' => 'My Heading', 'message' => 'My Message' ); $this->load->view('results_view', $data); results_view.php <html> <? php //Access them like so echo $title.$

What is output in CodeIgniter?

The Output class is a core class with one main function: To send the finalized web page to the requesting browser. It is also responsible for caching your web pages, if you use that feature. Note. This class is initialized automatically by the system so there is no need to do it manually.


1 Answers

...so - do I understand correctly - you want to get the whole final output (not just the view) as a string AND not display it to the user? Why dont you just overload the controllers _output() function?

class Your_controller extends CI_Controller
{
       function stuff()
       {
           // do whatever - prep $data etc
           $this->load->view('your_view', $data);
       }

       function _output($output)
       {
           // send $output to your library - get results blah blah
           $result_pdf_file = $this->your_pdf_library_generator($output);

           // Show something else to the user
           echo "hi - I'm not what you expected - but here is your PDF";

           echo $result_pdf_file; // or something like that
       }
 }

This means you can send ANYTHING you like to the output class - but nothing is displayed except what you want.

There are ways to improve this idea (i.e. hooks, variables to turn output on/off etc) - but the simplest would be to have this controller specifically for your pdf_generation command.

I don't see any way of doing step 2 based on the output class documentation. Is it possible to get a view into the output class without displaying it? If so, how? I'm using CI 2.0.3.

The controller _output() documentation is actually in the CI controller documentation, which is why it eluded you.

like image 129
Laurence Avatar answered Oct 18 '22 00:10

Laurence