Skip to content

Instantly share code, notes, and snippets.

@andrei99
Created April 11, 2019 11:29
Show Gist options
  • Save andrei99/99a8773bab4f71f399e8c178812b222b to your computer and use it in GitHub Desktop.
Save andrei99/99a8773bab4f71f399e8c178812b222b to your computer and use it in GitHub Desktop.
TCPDF
<?
require($_SERVER['DOCUMENT_ROOT'] . '/bitrix/modules/main/include/prolog_before.php');
require_once($_SERVER['DOCUMENT_ROOT'] . "/bitrix/php_interface/libs/tcpdf/tcpdf.php");
use Bitrix\Main\Application;
use Bitrix\Main\Context;
$obRequest = Application::getInstance()->getContext()->getRequest();
$intID = $obRequest->get('id');
$strName = $obRequest->get('name');
$strText = $obRequest->get('textHtml');
if (!empty($intID) && !empty($strName) && !empty($strText)) {
// create new PDF document
$pdf = new TCPDF('P', 'mm', 'A4', true, 'UTF-8');
// set document information
$pdf->SetTitle('Объект ' . $intID);
$pdf->SetTextColor(68, 68, 68);
// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO, PDF_HEADER_LOGO_WIDTH, PDF_HEADER_TITLE, PDF_HEADER_STRING);
//set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);
$pdf->SetFont(PDF_FONT_NAME_MAIN, '', 12, '', true);
//set auto page breaks
$pdf->SetHeaderFont(array(
PDF_FONT_NAME_MAIN, 'B', 10
));
$pdf->SetFooterFont(array(
PDF_FONT_NAME_MAIN, 'B', 10
));
// add a page
$pdf->AddPage();
$strHtml = $strText;
$pdf->WriteHTML($strHtml, true, false, false);
$pdf->Output($_SERVER['DOCUMENT_ROOT'] . 'tcpdf-file/pdf/item_' . $intID . '.pdf', 'F');
if (file_exists($_SERVER['DOCUMENT_ROOT'] . 'tcpdf-file/pdf/item_' . $intID . '.pdf')) {
$arResult['NAME'] = $strName;
$arResult['PATH'] = '/tcpdf-file/pdf/item_' . $intID . '.pdf';
echo json_encode($arResult);
}
}
//get pdf
$('body').on('click', '.link_get_pdf', function () {
var obj = new Object();
var name = $(this).attr('href');
var id = $(this).closest('.object__services').attr('data-id');
var textHtml = $('.wrap_pdf_file').html();
if (name !== '' && id !== '' && textHtml !== '') {
showSpinner();
obj.name = name;
obj.id = id;
obj.textHtml = textHtml;
$.ajax(
{
url: "/tcpdf-file/ajax.php",
dataType: "text",
data: obj,
type: "post",
success: function (ans) {
var result = JSON.parse(ans);
var name = result.NAME;
var path = result.PATH;
// console.log(ans);
// console.log(result.NAME);
setTimeout(function () {
if(name === 'send'){
}else if(name === 'print'){
var file = window.open(path);
file.print();
}else{
window.open(path);
}
hideSpinner();
}, 1000);
}
});
}
return false;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment