roadiz/abstract-pdf-theme

Abstract PDF middleware for your Roadiz theme to generate PDF responses out of your contents.

1.1.2 2022-06-20 15:50 UTC

This package is auto-updated.

Last update: 2024-10-20 20:49:38 UTC


README

Abstract PDF middleware for your Roadiz theme to generate PDF responses out of your contents.

Dependency injection

  • Add PDF services to your project app/AppKernel.php:
# AppKernel.php
/**
 * {@inheritdoc}
 */
public function register(\Pimple\Container $container)
{
    parent::register($container);

    /*
     * Add your own service providers.
     */
    $container->register(new \Themes\AbstractPdfTheme\Services\PdfServiceProvider());
}

or in your app/conf/config.yml:

additionalServiceProviders: 
    - \Themes\AbstractPdfTheme\Services\PdfServiceProvider

PdfControllerTrait

Usage

All you need to do is to create your node-source Controller in your theme and use PdfControllerTrait. You will be able to override any methods to configure your PDF rendering such as:

  • protected function getPdfTemplate(): string (Default: '@AbstractPdfTheme/pdf/base.xml.twig')
  • protected function getPdfStylesheetPath(): string (Default: $this->getResourcesFolder() . '/config/pdf_stylesheet.xml')
  • protected function getPdfFilename(NodesSources $nodeSource = null): string (Default: '@AbstractPdfTheme/pdf/fonts.xml.twig')
<?php
namespace Themes\MyTheme\Controllers;

use Themes\AbstractPdfTheme\Controllers\PdfControllerTrait;
use Themes\MyTheme\MyThemeThemeApp;
use RZ\Roadiz\Core\Entities\Node;
use RZ\Roadiz\Core\Entities\Translation;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class PageController extends MyThemeThemeApp
{
    use PdfControllerTrait;
    
    /**
     * @param Request $request
     * @param Node|null $node
     * @param Translation|null $translation
     * @return Response
     */
    public function indexAction(
        Request $request,
        Node $node = null,
        Translation $translation = null
    ) {
        $this->prepareThemeAssignation($node, $translation);

        if ($request->query->has('_format') && $request->query->get('_format') == 'pdf') {
            return $this->generatePdf($request, $this->nodeSource);
        }

        $response = $this->render('pages/page.html.twig', $this->assignation);

        return $response;
    }

    protected function getPdfTemplate()
    {
        return 'pdf/page.xml.twig';
    }
}

Template examples

Resources/views/ folder contains useful templates for creating your own PDF. Feel free to include them directly in your theme or duplicated them.