Note that there are some explanatory texts on larger screens.

plurals
  1. POCan I pass data to the Codeigniter output class without displaying it?
    text
    copied!<p>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 <a href="http://code.google.com/p/dompdf/" rel="nofollow noreferrer">dompdf</a> 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 <code>output-&gt;get_output()</code>. The wrapper is similar to <a href="https://github.com/hadiariawan/CodeIgniter-Dompdf-Library" rel="nofollow noreferrer">this one on Github</a>.</p> <p>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 <code>load-&gt;view()</code>, which is going to write to the browser.</p> <p>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.</p> <p>To be clear, this is what I want to accomplish (in the order that I want to accomplish it):</p> <ol> <li>Do everything I'd normally do to prepare the view for display</li> <li>Load the view into the CI output class, but not display it</li> <li>Pass the return value of <code>output-&gt;get_output()</code> to my dompdf library</li> <li>Set the appropriate headers</li> <li>Execute my dompdf method that will send the PDF to the browser</li> </ol> <p>I don't see any way of doing step 2 based on the <a href="http://codeigniter.com/user_guide/libraries/output.html" rel="nofollow noreferrer">output class documentation</a>.</p> <p>Is it possible to get a view into the output class without displaying it? If so, how? I'm using CI 2.0.3.</p> <p><strong>Edit</strong></p> <p>The very helpful <a href="https://stackoverflow.com/users/734499/anthony-sterling">Anthony Sterling</a> pointed out that <a href="https://twitter.com/AnthonySterling/status/241426835634786304" rel="nofollow noreferrer">I can just get what I want from the loader class</a> by setting the third argument telling it to return a string rather than render the view to TRUE. E.g.:</p> <pre><code>$lotsaHtml = $this-&gt;load-&gt;view('fooview', $somearray, TRUE); </code></pre> <p>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 <em>did</em> have a bunch of partials. Those could be concatenated, but yuck.</p> <p>It seems like I should be able to get the output class to not render anything (else, why does <code>get_output()</code> exist?) so I can do something else with everything it knows about. I just can't find a way to make that happen.</p> <p><strong>Edit 2</strong></p> <p>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 <em>wanted</em> to do.</p> <p>Let's say I have a public method <code>genpdf($id)</code> in a controller named <code>invoice</code> using a model named <code>inv</code>:</p> <pre><code>public function genpdf($invoiceId) { $this-&gt;load-&gt;library('dompdflib'); $this-&gt;pagedata['invoice_data'] = $this-&gt;inv-&gt;getInvoice($invoiceId); $html = $this-&gt;load-&gt;view('pdfgen', $this-&gt;pagedata, TRUE); $this-&gt;dompdflib-&gt;sendPdf($html); } </code></pre> <p>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 <code>pdfgen</code> view as a string, which I pass to the function in my dompdf wrapper that sets headers and sends the PDF to the browser.</p> <p>It just seemed like this would be easy to do by just getting the output buffer itself (after setting headers correctly / etc). </p> <p>Or do I just have to call the output class <code>append_output()</code> in succession with every partial I load?</p> <p>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 <code>output-&gt;append_output()</code> to make that happen.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload