Question

I am having some trouble making a pdf attachment from dompdf successfully attach to an email. The pdf document just says its malformed, not giving me any kind of error, however when i go directly to the link, the pdf downloads and loads successfully.

The email sends fine so the following line is all i can see that can possibly cause an issue.

$mailer->addStringAttachment(file_get_contents(JRoute::_('index.php?option=com_helloworld&view=finish&format=pdf&tmpl=component&data='.$id.'&Itemid='.$Itemid)),'Assessment.pdf');

The item id and id are loading from the following code

$app = JFactory::getApplication();
        $menu = $app->getMenu();
        $menuItem = $menu->getItems( 'link', 'index.php?option=com_helloworld&view=core', true );
        $Itemid = $menuItem->id;
$id = JRequest::getVar('assessmentId',null,'get');

This is all for the actual email creation, it does send an email with an attached pdf, however the pdf is malformed.

The view.pdf.php file runs as well the only difference between them is that the id loads the data rather then a session.

$data = JRequest::getVar('data',false,'get');
                if($data!==false){
                        //$data = urldecode($data);
                        //$data = json_decode($data);
                        $this->curData = array('id'=>$data,'data'=>$model->getAssessment($data));
                }else{
                        $this->curData = $session->get('core_value_assessment');
                }
Was it helpful?

Solution 3

The solution ended up being an "error" or rather then error, strange behavior from JRoute. It changes & to & automatically, witch being system parameters was causing it to try to route to the default view rather then the one I needed. The solution was to only use JRoute to get the menu itemid JRoute::_('index.php?Itemid=%%%'). This way it would get the proper path and you can add any extra parameters after.

The reason this was hard to diagnose is blind trust in JRoute as I never ran into this anywhere else. So because of that I never considered that such a straight forward route would not even go to the right view.

So to anyone else who has this problem or something similar, the trick is that since it is being generated for an email, pull that portion of the code out and print it in a view to see how its generating the link.

OTHER TIPS

domPDF throws exception in the event of error. Have you tried wrapping your code that produces the PDF with try catch?

e.g.

<?php

require_once("dompdf_config.inc.php");

try
{
    $dompdf = new DOMPDF();
    $dompdf->load_html('YOUR PDF HTML GOES HERE');
    $dompdf->set_paper("a4", "portrait");
    $dompdf->render();

    // $dompdf->stream("dompdf_out.pdf", array("Attachment" => false));
    $fp = fopen('test.pdf', 'w');
    fwrite($fp, $dompdf->output());
    fclose($fp);
}
catch (Exception $e)
{
    // log $e->getMessage() to file to see if any error(s) are occuring
}

?>

This may sound stupid, but you never know what can be the heart of the matter.

Check if the script index.php?option=com_helloworld&view=finish&format=pdf... does not generate any PHP errors int it's output.

Open the produced file in a text editor, and make sure it begins with sort of a binary header, not a few lines like <b>Notice:</b> . . . or such.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top