Документация для разработчиков
Темная тема

Пример генерации документа

Здесь изложен полностью процесс генерации документа:

<?php

define('BX_SESSION_ID_CHANGE', false);
define('BX_SKIP_POST_UNQUOTE', true);
define('NO_AGENT_CHECK', true);
define("STATISTIC_SKIP_ACTIVITY_CHECK", true);
define('STOP_STATISTICS', true);
define('BX_SECURITY_SHOW_MESSAGE', true);
define('NO_KEEP_STATISTIC', 'Y');
define('NO_AGENT_STATISTIC','Y');
define('DisableEventsCheck', true);

require_once($_SERVER['DOCUMENT_ROOT'].'/bitrix/modules/main/include/prolog_before.php');

use Bitrix\DocumentGenerator\Model\FileTable;
use Bitrix\DocumentGenerator\Model\TemplateTable;
use Bitrix\DocumentGenerator\Template;
use Bitrix\Main\Result;

if(!\Bitrix\Main\Loader::includeModule('documentgenerator'))
{
	die('no documentgenerator module');
}

$filePath = 'rest_template.docx';

class DocumentGeneratorPhpExample
{
	public function saveFile(string $filePath): ?int
	{
		$fileData = \CFile::MakeFileArray($filePath);
		if(!$fileData)
		{
			return null;
		}
		$fileData['isTemplate'] = true;

		$saveResult = FileTable::saveFile($fileData);
		if($saveResult->isSuccess())
		{
			return $saveResult->getId();
		}

		return null;
	}

	public function saveTemplate(string $templateName, int $fileId): ?int
	{
		$fields['NAME'] = $templateName;
		$fields['MODULE_ID'] = 'rest';
		$fields['FILE_ID'] = $fileId;
		$fields['ACTIVE'] = 'Y';
		$fields['BODY_TYPE'] = \Bitrix\DocumentGenerator\Body\Docx::class;

		$result = TemplateTable::add($fields);
		if($result->isSuccess())
		{
			return $result->getId();
		}

		return null;
	}

	public function getTemplateFieldPlaceholders(int $templateId): ?array
	{
		$template = Template::loadById($templateId);
		if($template)
		{
			$body = $template->getBody();
			if($body)
			{
				return $body->getFieldNames();
			}
		}

		return null;
	}

	public function createDocument(Template $template): Result
	{
		$fields = [
			'SomeDate' => ['TYPE' => 'DATE'],
			'SomeName' => ['TYPE' => 'NAME'],
			'Stamp' => ['TYPE' => 'STAMP'],
			'Image' => ['TYPE' => 'IMAGE'],
			'Table' => [
				'PROVIDER' => \Bitrix\DocumentGenerator\DataProvider\ArrayDataProvider::class,
				'OPTIONS' => [
					'ITEM_NAME' => 'Item',
					'ITEM_PROVIDER' => \Bitrix\DocumentGenerator\DataProvider\HashDataProvider::class,
				],
			],
			'TableItemName' => ['VALUE' => 'Table.Item.Name'],
			'TableItemImage' => ['VALUE' => 'Table.Item.Image', 'TYPE' => 'IMAGE'],
			'TableItemPrice' => ['VALUE' => 'Table.Item.Price'],
		];

		$values = [
			'SomeDate' => \Bitrix\Main\Type\DateTime::createFromTimestamp(time()),
			'SomeName' => [
				'NAME' => 'Иван',
				'LAST_NAME' => 'Петров',
				'GENDER' => 'M',
			],
			'Image' => __DIR__.'/image.jpg',
			'Stamp' => __DIR__.'/image.jpg',
			'Table' => [
				[
					'Name' => 'Item name 1',
					'Price' => '$111.23',
					'Image' => __DIR__.'/image.jpg',
				],
				[
					'Name' => 'Item name 2',
					'Price' => '$222.34',
					'Image' => __DIR__.'/image.jpg',
				],
			],
		];

		$template->setSourceType(\Bitrix\DocumentGenerator\DataProvider\Rest::class);
		$document = \Bitrix\DocumentGenerator\Document::createByTemplate($template, 111);

		return $document->setFields($fields)->setValues($values)->getFile();
	}
}

$example = new DocumentGeneratorPhpExample();

$templateId = 12;

if(!$templateId)
{
	$fileId = $example->saveFile($filePath);
	if($fileId > 0)
	{
		$templateId = $example->saveTemplate('php_test', $fileId);
	}
}

if(!$templateId)
{
	echo 'no template';
	die;
}

$fields = $example->getTemplateFieldPlaceholders($templateId);

print_r($fields);

$result = $example->createDocument(Template::loadById($templateId));

if($result->isSuccess())
{
	print_r($result->getData());
}
else
{
	print_r($result->getErrorMessages());
}

© «Битрикс», 2001-2024, «1С-Битрикс», 2024