I'm creating a module with differents forms. I have some forms which are modal forms using ctools.
On one of these modal forms i made a button to open a pdf on a new tab in my browser but it doesn't work.
I have a AJAX error.
This is my code:

function liste_apprenant_partenaire($js){
	if ($js) {
		ctools_include('ajax');
		ctools_include('modal');
		ctools_add_js('ajax-responder');
	
		$form_state = array(
				'ajax' => TRUE,				
		);
		
		$output = ctools_modal_form_wrapper('liste_apprenant_partenaire_form', $form_state);
		
		if (!empty($form_state['ajax_commands'])) {
				$output = $form_state['ajax_commands'];
		}
		
		print ajax_render($output);
		drupal_exit();
	}
	else {
		return drupal_get_form('liste_apprenant_partenaire_form');
	}
}

function liste_apprenant_partenaire_form($form, &$form_state){


$form['pv-button'] = array(
			'#id' => 'pv-button',
			'#type' => 'submit',
			'#value' => 'PV',
			'#submit'=>array('pv_apprenant_partenaire_button_form_submit'),
			'#attributes' => array('onclick' => 'target="_blank";return true;'),
	);

return $form;	
}


function pv_apprenant_partenaire_button_form_submit(&$form, &$form_state){
require_once("sites/all/modules/print/lib/dompdf/autoload.inc.php");

$node_session=node_load($_SESSION['nid_session']);

$type_formation=$node_session->field_type[LANGUAGE_NONE][0]['value'];

$test=file_get_contents('sites/all/modules/multi_example/test.html');
$test=str_replace('+type_formation',"$type_formation",$test);

$dompdf = new Dompdf();
$dompdf->loadHtml($test);

$dompdf->render();

$dompdf->stream('my.pdf',array('Attachment'=>0));

}

When i put the path of the node in my browser, it's worked fine because the form appears normally not as a modal form.
But i research the same result using a modal form. So i think there is a problem with AJAX but i don't find the real solution.

Comments

nitin.k’s picture


function liste_apprenant_partenaire_form($form, &$form_state){


$form['pv-button'] = array(
			'#id' => 'pv-button',
			'#type' => 'submit',
			'#value' => 'PV',
			'#submit'=>array('pv_apprenant_partenaire_button_form_submit'), // This will work but will not open the pdf in new tab
			'#attributes' => array('onclick' => 'target="_blank";return true;'), // This will not work
	);


$form['#attributes']['target'] = '_blank'; // even this will not work.
return $form;	
}


for achieving this target, follow below steps.

1. Provide a link in the form instead of button.
2. After creating callback and function, show the pdf.
3. Done.

dims971’s picture

Thanks your comment i have found a solution even if (actually) the pdf is just dowloaded when i click on the link i've created.
But i can generate a pdf from my modal form ! It's great !

I follow your step, i have created a new item in my hook_menu like that:

$items['pdf-pv'] = array(
	'title' => 'PV',
	'page callback' => 'pdf_test',
    'page arguments' => array(1),
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
   );

I put in my form['pv-button'] the link type instead of the submit type like that:

$form['pv-button'] = array(
			'#id' => 'pv-link',
			'#type' => 'link',
			'#title' => 'PV',
			'#href' => 'pdf-pv',
	);

And my function callback which generate the pdf looks like that:

function pdf_test(){

require_once("sites/all/modules/print/lib/dompdf/autoload.inc.php");

$node_session=node_load($_SESSION['nid_session']);

$type_formation=$node_session->field_type[LANGUAGE_NONE][0]['value'];

$test=file_get_contents('sites/all/modules/multi_example/test.html');
$test=str_replace('+type_formation',"$type_formation",$test);

$dompdf = new Dompdf();
$dompdf->loadHtml($test);

$dompdf->render();

$dompdf->stream();
}

I will try to find a solution to display the generated pdf on a new tab after (this will be my ideal solution) but if you have some suggestions about that, you are welcom :-)

But essentially it's working !!! Thank you a lot !!!