Advertisement
Trigub_Ilia

Формирование PDF документа

Apr 20th, 2021
1,098
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.66 KB | None | 0 0
  1. <?
  2. error_reporting(E_ALL);
  3. ini_set('display_errors', TRUE);
  4. ini_set('display_startup_errors', TRUE);
  5.  
  6. define("SM_SAFE_MODE", true);
  7. define("NOT_CHECK_PERMISSIONS",true);
  8. define("NO_AGENT_CHECK", true);
  9. define("NO_AGENT_STATISTIC", true);
  10. define("STOP_STATISTICS", true);
  11.  
  12. require($_SERVER["DOCUMENT_ROOT"]."/bitrix/modules/main/include/prolog_before.php");
  13. require_once $_SERVER["DOCUMENT_ROOT"] . '/local/lib/dompdf/autoload.inc.php';
  14.  
  15. use Dompdf\Dompdf;
  16. use Dompdf\Options;
  17.  
  18. global $USER;
  19. $USER->GetID();
  20.  
  21. if($_GET["user_id"] == $USER->GetID()) {
  22.  
  23.     CModule::IncludeModule("iblock");
  24.  
  25.     $arSelect = array("ID", "IBLOCK_ID", "NAME", "PROPERTY_OPERATIONS", "PROPERTY_TOTAL_ACCRUED", "PROPERTY_TOTAL_SUM", "PROPERTY_TOTAL_WITHHELD", "PROPERTY_TOTAL_COMPLETED");
  26.     $arFilter = array("IBLOCK_ID" => \Bitrix\Main\Config\Option::get('main', 'IBLOCK_PAY_LIST'), "ID" => $_GET["element_id"], "ACTIVE" => "Y");
  27.     $res = CIBlockElement::GetList(array(), $arFilter, false, array("nPageSize" => 1), $arSelect);
  28.     while ($ob = $res->GetNextElement()) {
  29.         $arFields = $ob->GetFields();
  30.         $ddd = json_decode($arFields["~PROPERTY_OPERATIONS_VALUE"]);
  31.         $docName = $arFields["NAME"];
  32.         $prop1 = $arFields["PROPERTY_TOTAL_ACCRUED_VALUE"];
  33.         $prop2 = $arFields["PROPERTY_TOTAL_SUM_VALUE"];
  34.         $prop3 = $arFields["PROPERTY_TOTAL_WITHHELD_VALUE"];
  35.         $prop4 = $arFields["PROPERTY_TOTAL_COMPLETED_VALUE"];
  36.     }
  37.  
  38.     $arAllData = array();
  39.     foreach ((array)$ddd as $data):
  40.         $data = (array)$data;
  41.         $date = new DateTime($data["StartDate"]);
  42.         $StartDate = $date->format('d.m.Y');
  43.  
  44.         $date = new DateTime($data["RegistrationData"]);
  45.         $EndDate = $date->format('d.m.Y');
  46.  
  47.         $arAllData[$data["Group"]] .= "<tr><td>" . $data["Type"] . " (" . $StartDate . "-" . $EndDate . ") - " . $data["Amount"] . "</td></tr>";
  48.  
  49.     endforeach;
  50.  
  51.     define("DOMPDF_ENABLE_REMOTE", true);
  52.  
  53.     $pdf_table_style = '<style>
  54.    body {
  55.        font-family: DejaVu Sans
  56.    }
  57.    table.products{
  58.        border-collapse: collapse;
  59.        width: 100%;
  60.        margin: 0 auto;
  61.    }
  62.    table.products td{
  63.        border: 1px solid;
  64.        font-size: 12px;
  65.        padding: 3px;
  66.    }
  67.    table.products td b{
  68.        font-size: 15px;
  69.    }
  70.    table.products td span{
  71.        font-size: 14px;
  72.    }
  73.    table.products th{
  74.        padding: 5px;
  75.        border: 1px solid;
  76.        font-weight: bold;
  77.        font-size: 12px;
  78.    }
  79.    h1{
  80.        text-align: center;
  81.    }
  82.    </style>';
  83.  
  84.     $pdf_table = '<!DOCTYPE html>
  85.                    <html>
  86.                        <head>
  87.                            <meta http-equiv = "Content-Type" content = "text/html; charset = utf-8" />
  88.                            ' . $pdf_table_style . '
  89.                            <title></title>
  90.                        </head>
  91.                        <body>
  92.                            <img src="http://' . $_SERVER['HTTP_HOST'] . '/local/download.jpg">
  93.                            <h1>' . $docName . '</h1>
  94.                            <table cellpadding="0" cellspacing="0" class="products">
  95.                            <colgroup><col></colgroup>
  96.                            <tbody>
  97.                                <tr>
  98.                                    <td>
  99.                                        <b>1. Начислено:</b>
  100.                                    </td>
  101.                                </tr>
  102.                                ' . $arAllData["Начислено"] . '
  103.                                <tr>
  104.                                    <td>
  105.                                         <span>ИТОГО НАЧИСЛЕНО: ' . $prop1 . '</span>
  106.                                    </td>
  107.                                </tr>
  108.                                <tr>
  109.                                    <td>
  110.                                        <b>2. Доходы в натуральной Форме:</b>
  111.                                    </td>
  112.                                </tr>
  113.                                ' . $arAllData["Справочно"] . '
  114.                                <tr>
  115.                                    <td>
  116.                                         <span>ИТОГО ДОХОДОВ В НАТУРАЛЬНОЙ ФОРМЕ: ' . $prop2 . '</span>
  117.                                    </td>
  118.                                </tr>
  119.                                <tr>
  120.                                    <td>
  121.                                        <b>3. Удержано:</b>
  122.                                    </td>
  123.                                </tr>
  124.                                ' . $arAllData["Удержано"] . '
  125.                                <tr>
  126.                                    <td>
  127.                                         <span>ИТОГО Удержано: ' . $prop3 . '</span>
  128.                                    </td>
  129.                                </tr>
  130.                                <tr>
  131.                                    <td>
  132.                                         <b>4. Выплачено:</b>
  133.                                    </td>
  134.                                </tr>
  135.                                ' . $arAllData["Выплачено"] . '
  136.                                <tr>
  137.                                    <td>
  138.                                         <span>ИТОГО Выплачено: ' . $prop4 . '</span>
  139.                                    </td>
  140.                                </tr>
  141.                            </tbody>
  142.                            </table>';
  143.     $pdf_table .= '</body></html></table>';
  144.  
  145.     //echo $pdf_table;
  146.     //exit();
  147.  
  148.     $options = new Options();
  149.     $options->set('isRemoteEnabled', true);
  150.  
  151.     $dompdf = new Dompdf($options);
  152.  
  153.     $dompdf->set_option('isHtml5ParserEnabled', true);
  154.     $dompdf->set_option('isRemoteEnabled', true);
  155.  
  156.  
  157.     $dompdf->loadHtml($pdf_table);
  158.     $dompdf->render();
  159.  
  160.     $file = $dompdf->output();
  161.  
  162.     $file_to_save = $_SERVER["DOCUMENT_ROOT"] . '/upload/file.pdf';
  163.     //save the pdf file on the server
  164.     file_put_contents($file_to_save, $file);
  165.     //print the pdf file to the screen for saving
  166.     header('Content-type: application/pdf');
  167.     header('Content-Disposition: inline; filename="file.pdf"');
  168.     header('Content-Transfer-Encoding: binary');
  169.     header('Content-Length: ' . filesize($file_to_save));
  170.     header('Accept-Ranges: bytes');
  171.     readfile($file_to_save);
  172.  
  173.     $temp = tmpfile();
  174.     fwrite($temp, $file);
  175.     fseek($temp, 0);
  176.  
  177.     $metaDatas = stream_get_meta_data($temp);
  178.  
  179.     echo "<pre>";
  180.     print_r($metaDatas);
  181.     echo "</pre>";
  182. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement