{*
 Шаблон вычисления "Сохранить шаблон печати в файл"
*}

{if $type_template == 'fixed'}
$formId = {$print_tpl};
$build_line = $line;
$build_table = $table_id;
{elseif $type_template == 'dynamic'}
$formId = $line['{$field_id_template}'];
{if $tabel_for_tpl == 'current'}
$build_line = $line;
$build_table = $table_id;
{elseif $tabel_for_tpl == 'parent'}
$parent_table = sql_select_array(ADDIT_TABLES_TABLE, 'link_table_id = ' . get_table_id($table_id));
$build_line = sql_select_array(DATA_TABLE.$parent_table['table_id'], 'id = ' . $line['f'.$parent_table['link_field_id']]);
$build_table = $parent_table['table_id'];
{/if}
{/if}
$form = sql_select_array(FORMS_TABLE, 'id = ' . get_form_id($formId));
if(!empty($form) && $form['table_id'] == $build_table) {
    $form['doc_name'] = form_template($form, $build_line, 'doc_name');
    if ($form['type_form'] == 2 && $form['pdf_import'] == 1) {
        $mpdf = new \Mpdf\Mpdf([
            'tempDir' => $config['site_path'] . '/temp',
            'format' => 'A4' . $form['orientation'],
            'margin_left' => $form['margin_left'],
            'margin_right' => $form['margin_right'],
            'margin_top' => $form['margin_top'],
            'margin_bottom' => $form['margin_bottom']
        ]);     
        $mpdf->SetDisplayMode('fullwidth');
        $form['head_page'] = str_replace(['[[', ']]'], ['{ldelim}ldelim{rdelim}', '{ldelim}rdelim{rdelim}'], $form['head_page']);
        $form['foot_page'] = str_replace(['[[', ']]'], ['{ldelim}ldelim{rdelim}', '{ldelim}rdelim{rdelim}'], $form['foot_page']);
        $mpdf->SetHTMLHeader(form_template($form, $build_line, 'head_page'));
        $mpdf->SetHTMLFooter(form_template($form, $build_line, 'foot_page'));
        if ($form['head_form'] != '') {
            $mpdf->WriteHTML(form_template($form, $build_line, 'head_form'));
        }
        $mpdf->WriteHTML(form_template($form, $build_line, 'body_form'));
        if ($form['foot_form'] != '') {
            $mpdf->WriteHTML(form_template($form, $build_line, 'foot_form'));
        }
        $file_name = $form['doc_name'] . '.pdf';
        $form_text = $mpdf->Output($file_name, 'S');
    } elseif ($form['type_form'] == 3) {
        $file_name = $form['doc_name'] . '.rtf';
        $form_text = form_template($form, $build_line, 'body_form');
    } elseif ($form['type_form'] == 4) {
        $file_name = $form['doc_name'] . '.odt';
        $form_text = form_template($form, $build_line, 'body_form');
    } elseif ($form['type_form'] == 5) {
        $file_name = $form['doc_name'] . '.docx';
        $form_text = form_template($form, $build_line, 'body_form');
    } elseif ($form['type_form'] == 6) {
        $file_name = $form['doc_name'] . '.xls';
        $form_text = form_template($form, [$build_line], 'body_form');
    } elseif ($form['type_form'] == 7) {
        $file_name = $form['doc_name'] . '.xlsx';
        $form_text = form_template($form, [$build_line], 'body_form');
    }
    $save_link = substr('{$field}', 1);
    save_data_file($save_link, $ID, $file_name, $form_text);
    $line['{$field}'] = $file_name;
}