Attach PDF from other list to confirmation email

zebrafilm

Member
Setup:
Customer fills in form and at the end he gets a confirmation by the form plugin with a thank you message.
The input is stored and I had to make another list (with linked elements) to format the result properly and hide all calculations. That second 'view' list also creates a nice PDF but I want to send this one to the client after he fills the first form. Input is locked to the userID

How to do this?
 
mmmm might be an option. Email template = no problem , Guess I could add the details listing in there too but not sure if I will find the right way to couple it to the right person/ record/ detail form.
Will have look for that option.
 
The email template can use PHP so can pretty much pick up whatever data you need and lay it out however you need as well. So picking up the logged in user, the current record values and \ or any joined values are all possible.
 
OK clear. No problem to get the ID of the current forminput but then the question is how to generate a PDF attachement the proper way in the PHP template.
 
It should be possible to generate the PDF view from first principles, by instantiating a form model for the second list, loading the row data, and rendering the PDF view into a buffer capture. Basically, replicating the basics of the code that would kick off if you did that manually from a browser.

But that's not really something we can just whip up as an example bit of code, it would have to be done as a chunk of custom work.

Unless ... hmmm ... might be possible to do as a CURL call, using the URL that would kick off a PDF display in the browser. That should return the PDF, which you could then write out to a file, and attach. That would be a lot simpler. The code would live in the "Attachments (eval)" of your first form.

Are you familiar with how to do a CURL call in PHP?

-- hugh
 
Hi Hugh,

I found the way to create links from the current input to show an overview in other layout / form. I created a PHP template for the customer and one for admin where I put the direct links in:


PHP:
defined('_JEXEC') or die('Restricted access');
 
$key=$this->data['zuid_inv_aanvragen___userid'];
$itemID=$this->data['zuid_inv_aanvragen___id'];
$linkPDF='<a href="http://offerte.zuidwijkcarrosserieen.nl/index.php?option=com_fabrik&view=details&formid=86&rowid='.$itemID.'&format=pdf">Offerte PDF</a>';
$linkID='<a href="http://offerte.zuidwijkcarrosserieen.nl/index.php?option=com_fabrik&view=details&formid=86&rowid='.$itemID">Offerte</a>';
?>
 
 
 
<p>The quote can be checked by clicking this link</p>
<? echo $linkID; ?>
 
 
<p>The quote as PDF </p>
<? echo $linkPDF; ?>

And no I am already struggling with PHP (but improving) so CURL nope.
What I noticed that creating a PDF is time/resource consuming so even using the link takes quite a bit of time.
When I let the Fabrik email/ receipt plugin handle the PDF (from the just filled in form which I don't want) it seems to go pretty smooth and is properly attached.
 
Yes, creating PDF views takes a while, as it's a "double dip" ... first we have to create the standard HTML view of the form, then we have to feed that HTML view in to DOMPDF, which converts it to PDF.

It probably "feels" quicker when doing it through email as a lot of the work involved when you open the specific PDF page has already been done during the form submission / email creation. We've already loaded up all the form and display models, etc., so the added overhead of then rendering the view as PDF is smaller. Whereas when you hit that PDF link, it's having to do everything from scratch.

I'm testing some code for you, I'll post it in a little while.

-- hugh
 
OK, this might work:

PHP:
$ch = curl_init();
$url = 'http://offerte.zuidwijkcarrosserieen.nl/index.php?option=com_fabrik&view=details&formid=86&rowid=' . $data['zuid_inv_aanvragen___id'] . '&format=pdf';
 
if($ch)
{
    $config = JFactory::getConfig();
    $pdf_file = tempnam($config->get('tmp_path'), 'email-') . '.pdf';
 
    $fp = fopen($pdf_file, "w");
    if($fp)
    {
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FILE, $fp);
        curl_setopt($ch, CURLOPT_HEADER, 0);
 
        if( !curl_exec($ch) )
        {
            curl_close($ch);
                  fclose($fp);
            throw new RuntimeException('Could not generate PDF file: curl_exec() failed');
        }
 
        curl_close($ch);
              fclose($fp);
    }
    else
    {
        throw new RuntimeException('Could not generate PDF file: fopen(' . $pdf_file . ') failed');
    }
}
else
{
    throw new RuntimeException('Could not generate PDF file: curl_init() failed');
}
 
return array($pdf_file);


This code goes in the "Attachments (eval)" box, under the Attachments tab in the email plugin.

May need some tweaking, like I'm not 100% sure if you need $data or $this->data to get the id from, or you may need to get the _raw of the id. But in principle, this should work, by reading the response from a CURL fetch (basically just a progrmatically driven way of getting a web page) of the PDF view and storing it in a temp file.

FYI, the email plugin should handle automagically deleting the temp file when the mail has been sent.

This is really just a "cheap" way of building the PDF by hand. If you want to see how that would be done, it would be a slight variation of the pdfAttachment() function, starting around line 339 in ./plugins/fabrik_form/email/emai.php.

-- hugh
 
Hi,

I'm following this thread as it is exactly what I need to do : sending an email using a form plugin with a pdf attachment generated using another form (actually details view) id.

The attached pdf is "blank" (ie it's a correct dompdf output with a title and dates but nothing within). I've var_dumped the generated url and used it "by hand" on the browser. It works very well as the browser throw a popup asking me whether I want to open it in the right application or just save it. In that case the pdf is as expected.

It seems to be curl related ? Did someone use this trick with success ?
 
Actually the pdf detail view is allowed only to registred users. The curl start a new session as "public" and the result is "you have not access to this ressource". Which is fine indeed from a security point of view.

However, it does not help solve the problem of attaching a pdf from detail view id X with in a mail sent on submitting a formid Y
 
I've not tried but perhaps you could use the Fabrik content plugin to render things?

So your custom email template would contain:

Code:
{fabrik view=details id=X rowid=Y}

My only doubt is whether the content plugin is parsed in this case.
 
Hi Rob,

Indeed, I'm able to display the detail view within the message using the content plugin inside the article used as message source ({fabrik view=details formid=14 rowid=[rowid] show-title=0 format=pdf} ). As you can see I've tried the "format=pdf" thing but it does not work.

However this possibility is interesting but, I'd like to attach the pdf file to the mail. The "Attach as PDF" yes/no option of the email form plugin does the thing using the current formid. Would you think it may be possible to modify the plugin to add the option to specify the form/detail view to use to render the pdf ?

BTW the {content} placeholder inside the "message text" field does not render the article as the tooltip says.
 
digging the web, I've finally made it working. Here is my modifiyed script :
Code:
$ch = curl_init();
if($ch)
{
    $proforma_number = $data['f_orders___proforma_number_raw'];
// var_dump($data,$this->data);exit; // some "$data" data are corrupted... included rowid ?!
    $order_id = '{rowid}';

    $url = JURI::base() . 'index.php?option=com_fabrik&view=details&formid=14&rowid=' . $order_id . '&format=pdf';
    $config = JFactory::getConfig();
    $pdf_file = JPATH_BASE . substr($config->get('tmp_path') , 1) . '/devis-' . $proforma_number . '.pdf';

    $fp = fopen($pdf_file, "w");
    if($fp)
    {
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FILE, $fp);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
        if (isset($_COOKIE[session_name()])) {
          curl_setopt($ch, CURLOPT_COOKIE, session_name().'='.$_COOKIE[session_name()].'; path=/');
          session_write_close();      
        }
        $reponse = curl_exec($ch);//get curl response
        if( $reponse !== true )
        {
            curl_close($ch);
            fclose($fp);
            throw new RuntimeException('Could not generate PDF file: curl_exec() failed');
        }
        curl_close($ch);
              fclose($fp);
    }
    else
    {
        throw new RuntimeException('Could not generate PDF file: fopen(' . $pdf_file . ') failed');
    }
}
else
{
    throw new RuntimeException('Could not generate PDF file: curl_init() failed');
}
return array($pdf_file);

The pdf file is generated "by hand" as I want its name "friendly" and it stays in the /tmp directory without being deleted.

However not sure to play right with the session id ? Should I include a
session_start();
at the end of the script ?

Joomla nor Fabrik does not seem affected by the "session_write_close()".
 
  • Like
Reactions: rob
Sorry I don't really know about sessions with respect to CURL.
However, I doubt that you should be doing anything in the script with the session apart from passing its cookie to the curl request
 
Actually I'm experiencing a trouble with redirection.
In the form in which I use the email plugin with the "famous" attach script, I need to do a redirect to a specific form view.
The standard redirect plugin doest the redirection but I haven't been able to make it display a message as per the tool tip example. So, I added a php plugin OnAfterProcess doing this :
PHP:
$rowid = '{rowid}';
$url = './index.php?option=com_fabrik&view=form&Itemid=321&formid=14&rowid=' . $rowid . '&listid=13';
$msg = "some message";
$app = JFactory::getApplication();
$app->redirect($url, $msg);

without the attach pdf to mail script, it's perfectly working. With it, the message never shows up... Could it be related to the session close ?
 
We are in need of some funding.
More details.

Thank you.

Members online

Back
Top