Генерация PDF
Вместо того чтобы возвращать HTML-код вашей страницы, вы можете возвращать её PDF‑версию. Для этого можно использовать функциональность PimcoreWebToPrintBundle.
Пожалуйста, убедитесь, что вы правильно настроили и установили PimcoreWebToPrintBundle ("Settings" -> "Web2Print Settings").
Установите и настройте PimcoreWebToPrintBundle через bundles.php, а затем укажите правильные настройки(Tool -> PDFreactor / Chromium / Gotenberg) и соответствующие параметры для выбранного адаптера.
В контроллере нужно вернуть PDF вместо HTML.
Простой пример
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class BlogController extends FrontendController
{
public function indexAction(Request $request): Response
{
//ваш код...
//вернуть pdf
$html = $this->renderView(':Blog:index.html.php', [
'document' => $this->document,
'editmode' => $this->editmode,
]);
return new Response(
\Pimcore\Bundle\WebToPrintBundle\Processor::getInstance()->getPdfFromString($html),
200,
array(
'Content-Type' => 'application/pdf',
)
);
}
Продвинутый пример
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
class BlogController extends FrontendController
{
public function indexAction(Request $request): Response
{
//ваш код...
//вернуть pdf
$params = [
'document' => $this->document,
'editmode' => $this->editmode,
];
$params['testPlaceholder'] = ' :-)';
$html = $this->renderView(':Blog:index.html.php', $params);
$adapter = \Pimcore\Bundle\WebToPrintBundle\Processor::getInstance();
//добавить кастомные параметры конфигурации при необходимости
if ($adapter instanceof \Pimcore\Bundle\WebToPrintBundle\Processor\PdfReactor) {
//Параметры конфигурации -> http://www.pdfreactor.com/product/doc/webservice/php.html#Configuration
$params['adapterConfig'] = [
'author' => 'Max Mustermann',
'title' => 'Custom Title',
'javaScriptMode' => 0,
'addLinks' => true,
'appendLog' => true,
'enableDebugMode' => true
];
} elseif ($adapter instanceof \Pimcore\Bundle\WebToPrintBundle\Processor\Gotenberg) {
$params = Config::getWeb2PrintConfig();
$params = json_decode($params['gotenbergSettings'], true) ?: [];
} elseif ($adapter instanceof \Pimcore\Bundle\WebToPrintBundle\Processor\Chromium) {
$params = Config::getWeb2PrintConfig();
$params = json_decode($params['chromiumSettings'], true) ?: [];
}
return new Response(
$adapter->getPdfFromString($html, $params),
200,
array(
'Content-Type' => 'application/pdf',
// 'Content-Disposition' => 'attachment; filename="custom-pdf.pdf"' //для прямой загрузки
)
);
}
Вы можете предложить улучшение документации или задать вопрос в комментариях.
Если вам нужна полноценная консультация — вы можете заказать её на нашем сайте.