I posted this question ExpressionEngine, php and stash - scope problem, please help and really appreciate the responses and take on board that mixing ExpressionEngine and PHP tags is bad practice, and also that using embeds in a loop is bad for performance...
I was originally trying to use stash and got to a place where the pdf generation would be blank the first time the user generated it and then every subsequent click to generate it, it would be one generation behind ie if you removed a favourite, it would only be reflected after generating the pdf twice more.
I thought tring to remove stash from the equation might help. However I have a situation where I am needing to extend the ExpressionEngine favorites plugin by having more than one entry on a page able to be favourited - in this case a barristers skills that are related to the barrister currently being viewed by a playa relationship. I have had to use PHP on output to sort through the favourited entry_ids and have the barrister skills listed below each barrister and the client is keen to keep the content that is going into the pdf as ExpressionEngine tags so it is reusable in future projects.
What I am finding with the code below is that I can generate the accurate html code in one click and display it on screen, but if I attempt to do anything else with that variable (generate a pdf) using PHP, the pdf is sent but is made of unparsed ExpressionEngine tags.
My question is - is there any hope for this approach? I really can't think of another way to approach this problem and I am rather stuck! I happily use and pay for an ExpressionEngine plugin to do this but there isn't one out there that can sort through the favourited IDs and work out which are playa parents and format things in the right way and be compatible with Playa and Matrix.
Many thanks in advance for any light anyone can shed on this.
The code:
$barrister_expertise_titles = '{exp:stash:get name="barrister_expertise_titles"}';
// SORT BARRISTERS
$barrister_person_ids = '{exp:stash:get name="barrister_person_ids"}';
$barrister_person_ids = rtrim($barrister_person_ids, ',');
$barrister_person_ids = explode(',', $barrister_person_ids);
$barrister_person_ids = array_unique($barrister_person_ids);
$barrister_person_ids = array_keys(array_count_values($barrister_person_ids));
$other_favourites = '{exp:stash:get name="other_favourites"}';
$other_favourites = rtrim($other_favourites, ',');
$other_favourites = explode(',', $other_favourites);
$other_favourites = array_unique($other_favourites);
$other_favourites = array_keys(array_count_values($other_favourites));
$barrister_fave_ids = "{exp:stash:get_list name='barrister_expertise_favourites'}{fave_id}{/exp:stash:get_list}";
$barrister_fave_ids = str_replace("{fave_id}","", $barrister_fave_ids);
$barrister_fave_ids = substr_replace($barrister_fave_ids ,"",-1);
$barrister_fave_ids = str_replace(' ','',$barrister_fave_ids);
$barrister_exploded_fave_ids = explode(',', $barrister_fave_ids);
$barrister_exploded_fave_ids = array_unique($barrister_exploded_fave_ids);
$barrister_exploded_fave_ids= array_keys(array_count_values($barrister_exploded_fave_ids));
$num_barrister_faves = sizeof($barrister_exploded_fave_ids);
$html_generation = "";
// THE LOOP
$i = 0;
$num_barristers = sizeof($barrister_person_ids);
while ($i < $num_barristers)
{
$this_barrister_id = $barrister_person_ids[$i];
$html_generation .= "{embed='includes/pdf_format_barrister' barrister_id='$this_barrister_id'}";
$j=0;
while ($j < $num_barrister_faves) {
$html_generation .= "{embed='includes/pdf_format_expertise' barrister_id='$this_barrister_id' fave_id='$barrister_exploded_fave_ids[$j]'}";
$j++;
}
$i++;
}
$html = $html_generation; // html is in this variable and correct if echoed...
// trying to use php to now generate the pdf (the included file below is on the server)
// but it sends a pdf without the expressionengine tags parsed...
require_once('generate_pdf.php');
PDF_FORMAT_BARRISTER
{exp:channel:entries dynamic="off" entry_id="{embed:barrister_id}" status="open" disable="pagination" limit="1" channel="barristers_jc|barristers_qc"}
<table width="100%" cellspacing="0" cellpadding="1" border="0" width="640">
<tr valign="bottom">
<td align="left" valign="middle" width="484">
<h1 style="display: inline-block;">{title}</h1>
<br />
<p>
{if barristers_year_of_silk}Year of Silk: {barristers_year_of_silk format="%Y"}<br />{/if}
Year of Call: {barristers_year_of_call format="%Y"}<br />
{if barristers_email!="" OR clerks_email!=""}<a href="{if barristers_email}{barristers_email}{if:else}{clerks_email}{/if}">{if barristers_email}{barristers_email}{if:else}{clerks_email}{/if}</a><br />{/if}
{if barristers_direct_dial}Direct Dial: {barristers_direct_dial}{/if}
</p>
{if barristers_featured_quote}
<blockquote class="featured-quote" style="width: 50%; clear: both;">
{barristers_featured_quote}
{quote}
{if author_year}<h4>- {author_year}</h4>{/if}
{/barristers_featured_quote}
</blockquote>
{/if}
</td>
<td align="right" valign="middle" width="156">
{if barristers_profile_image}
<img src="{barristers_profile_image}" alt="{title}" title="{title}" border="0" height="156" width="150">
{/if}
</td>
</tr>
<tr valign="bottom">
<td align="left" colspan="2" width="100%">
<h2 class="header" style="clear: both;">Overview</h2>
{barristers_profile_overview}
{if barristers_other_quotes}<h2 class="header">Quotes</h2>{/if}
{barristers_other_quotes sort="asc"}
<blockquote>
{quote}
{if author_year}<h4>- {author_year}</h4>{/if}
</blockquote>
{/barristers_other_quotes}
</td>
</tr>
</table>
{/exp:channel:entries}
The pdf format expertise include is similar to pdf_format_barrister