custom button for pdf from template

jscott

New member
Is there an easy way in DaDaBIK 9.2 Monterosso to create a custom button to generate a pdf based on one of the custom templates?
 

eugenio

Administrator
Staff member
Interesting, I've never thought about that; do you mean in the details page or in the results page?

For the details page, the button has to redirect to an URL like this (change tablename, where_field and where_value according to yours):

index.php?tablename=customers&function=details&export_to_pdf=1&where_field=id_customer&where_value=301

the problem is that the function export_to_pdf expects in POST the name of the template:

$_POST["pdf_template"]

so you should, using some javascript in your function, post the value to the URL.

I might add to the next version the possibility to specify the template via GET, which would make the whole think easier.

A possible (not tested) "hack" is to pass it anyway via GET

index.php?tablename=customers&function=details&export_to_pdf=1&where_field=id_customer&where_value=301&pdf_template=mytemplate

and then, in your custom_startup_function (see custom_functions.php), add something like:

if (isset($_GET['pdf_template'])){
$_POST['pdf_template'] = $_GET['pdf_template'];
}

be aware that if you use "pdf_template" as a field name somewhere in your db, you can have unexpected results.


Best,
 

jscott

New member
Thanks Eugenio, I was thinking the details page, but the results grid would also be useful.

The reason this would be useful is that I have a few views that share a common id with some common tables, and I would like to create a button that could be located on a table detail page that would generate a pdf based on the view with the same id (rather than go to the view and use the existing pdf generator).

I'm very happy with the additional features of Monterosso by the way!

js
 

jscott

New member
Make sure custom buttons are enable by setting $enable_custom_button_functions = 1 in include/config.php, then you need to set up a custom button following the instructions in include/custom_functions.php. In the example below, I passed the table name, $where_value, template name etc into the html header.

$custom_buttons['Distribution_view'][$cnt]['type'] = 'php_standard';
$custom_buttons['Distribution_view'][$cnt]['callback_function'] = 'dadabik_distribution_sheet';
$custom_buttons['Distribution_view'][$cnt]['permission_needed'] = 'edit';
$custom_buttons['Distribution_view'][$cnt]['show_in'][] = 'edit_form';
$custom_buttons['Distribution_view'][$cnt]['show_in'][] = 'details_page';
$custom_buttons['Distribution_view'][$cnt]['position_form'] = 'top';
$custom_buttons['Distribution_view'][$cnt]['label_type'] = 'fixed';
$custom_buttons['Distribution_view'][$cnt]['label'] = 'PRINT Distribution sheet';
$custom_buttons['Distribution_view'][$cnt]['style'] = 'background:#2e4472;width:200px';
$cnt++;

function dadabik_distribution_sheet($table_name, $where_field, $where_value)
{
header('Location:index.php?tablename=Distribution_view&function=details&export_to_pdf=1&where_field=id&where_value='.$where_value.'&pdf_template=DISTRIBUTION-Summary');
exit();
}
 

drashrafsabry

Well-known member
Hello
can you tell me what code did you use to create the button in details or results page to print using a specfic pdf template
 

drashrafsabry

Well-known member
when i use it , it sends that


https://drashrafsabry.com/clinic/index.php?function=execute_custom_function&tablename=Visits&where_field=id_visits&where_value=426&custom_function=dadabik_print_treatment&name_button=1
 

drashrafsabry

Well-known member
ok my mistake i added the function in the .js , so i added it now to the .php file it works but it is not reading the correct template , it is reading the default one
 

drashrafsabry

Well-known member
here is the code i used


$cnt = 1;
$custom_buttons['Visits'][$cnt]['type'] = 'php_standard';
$custom_buttons['Visits'][$cnt]['callback_function'] = 'dadabik_print_treatment';
$custom_buttons['Visits'][$cnt]['permission_needed'] = 'edit';
$custom_buttons['Visits'][$cnt]['show_in'][] = 'edit_form';
$custom_buttons['Visits'][$cnt]['show_in'][] = 'details_page';
$custom_buttons['Visits'][$cnt]['position_form'] = 'top';
$custom_buttons['Visits'][$cnt]['label_type'] = 'fixed';
$custom_buttons['Visits'][$cnt]['label'] = 'PRINT Treatment';
$custom_buttons['Visits'][$cnt]['style'] = 'background:#2e4472;width:200px';
$cnt++;



function dadabik_print_treatment($table_name, $where_field, $where_value)
{
header('Location:index.php?tablename=Visits&function=details&export_to_pdf=1&where_field=id_visits&where_value='.$where_value.'&pdf_template=visitstreatment');
exit();
}


my template name is visitstreatment
the file is visitstreatment.html in templates directory


here is what is output to the browser

https://drashrafsabry.com/clinic/index.php?tablename=Visits&function=details&export_to_pdf=1&where_field=id_visits&where_value=432&pdf_template=visitstreatment

but it does not load the template , it loads the default one
 

eugenio

Administrator
Staff member
Hello,
you should add some code in the startup function as explained in the post:

https://dadabik.com/forum/read.php?1,20784,20786#msg-20786

I haven't tested this solution but jscott said it worked.
 

drashrafsabry

Well-known member
i am using this function to print a pdf using a specific template

function dadabik_print_request($table_name, $where_field, $where_value)
{
header('Location:index.php?tablename=Requests&function=details&export_to_pdf=1&where_field=id_requests&where_value='.$where_value.'&pdf_template=RequestsPrint');
exit();
}


i need help in two things

1- how can i make this function open the pdf generated in a new window not in the same window

2- how can i make this function print directly to printer , and not pass through generating a pdf file window first then i have to click print
 

eugenio

Administrator
Staff member
Hello,

1) Instead of a PHP function, you should use a javascript function; the function should execute something like:

window.open('index.php?tablename=....&function=search&export_to_pdf=1&pdf_template=......', '_blank');

2) I don't think it is possible

Best,
 
Top