Alex Rivera | Logout

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

Asked 2012-08-31T06:43:13.390
16

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 fr

Edit
Report

1 Answer

10

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

answered 2012-09-08T11:32:45.797

Your Answer