Documentation

The following lines of code will output the current template as a PDF:

use Concrete\Package\PdfDesigner\Src\PDFDesigner;

...

PDFDesigner::getTemplateByName("New Document 1")->outputPDF();

Working with Placeholders

The following sample code explains you, how to fill the template with dynamic content.

Paste following code snippet into your sourcecode:

use Concrete\Package\PdfDesigner\Src\PDFDesigner;

...

// Use createPDF instead of outputPDF, if you want the get the generated PDF data as a string.
PDFDesigner::getTemplateByName("New Document 1")->outputPDF(
    array(
        "receiver" => array(
            "firstname" => "Mister",
            "lastname" => "Smith",
            "street" => "Name of the Street",
            "zip" => "Zip Code",
            "city" => "City"
        )
    )
);

Click on Edit Content of a box and paste the following placeholders into the content area.

{receiver.firstname} {receiver.lastname}
{receiver.street}
{receiver.zip} {receiver.city}

Working with Tables

The following sample code explains you, how to fill a Table Box with dynamic content.

Paste following code snippet into your sourcecode:

PDFDesigner::getTemplateByName("New Document 24")->outputPDF(
    array(
        "myTable" => array(
            // Define the Columns
            "columns" => array(
                array(
                    "align" => "L", // Left Align
                    "text" => "Column 1"
                ),
                array(
                    "align" => "C", // Left Align
                    "text" => "Column 2"
                ),
                array(
                    "align" => "R", // Right Align
                    "text" => "Column 3"
                )
            ),
            
            "rows" => array(
                array(
                    "Data 1",
                    "Data 2",
                    "Data 3"
                ),
                array(
                    "Data 1",
                    "Data 2",
                    "Data 3"
                ),
                array(
                    "Data 1",
                    "Data 2",
                    "Data 3"
                )

                // Add so many rows you want...
            )
        )
    )
);

Click on Edit Content of a Table Box and set the Data Source to {myTable}.

Saving PDF to file

 If you want to save the PDF to a file e.g. to send it as an email attachment you can use the following code snippet:

$fileName = \Concrete\Package\PdfDesigner\Src\Helpers::generateTempFile();
        
$pdfData = \Concrete\Package\PdfDesigner\Src\PDFDesigner::getTemplateByName("My Template")->createPDF(
	array(
		// Array with Placeholders ....
	)
);
        
$app = \Concrete\Core\Support\Facade\Application::getFacadeApplication();
        
$fileHelper = $app->make('helper/file');
        
$fileHelper->append($fileName, $pdfData);

This tutorial is only for advanced programmers. If you need a custom box type feel free to contact me. 

General

To develop your own box type, you must create a controller and a view.

 

The Controller

The controller have to look like this:

namespace Concrete\Package\MySampleExtension\Src\BoxEditor;

use Concrete\Package\PdfDesigner\Src\BoxEditor;
use Concrete\Package\PdfDesigner\Src\IBoxEditor;

defined('C5_EXECUTE') or die("Access Denied.");

class SampleBox extends BoxEditor implements IBoxEditor {

    public function getViewPath() {
        return '../../pdf_designer_barcode_extension/views/dialogs/box_editors/barcode';
    }

    public function renderView() {
        $this->getPage()->set("box", $this);
    }
    
    public function renderPDF(&$pdf, $x, $y, $w, $h) {
        //
    }

}

The Parameter $pdf is a instance of FPDF.

For further informations what you can do with this object take a look at the FPDF API Documentation.

 

The View

The view have to look like this:

<div class="ccm-ui">
    <form method="post" id="ccmEditBoxForm" data-dialog-form="edit-box" action="<?php echo sprintf("%s?templateId=%s&boxId=%s", $this->action('submit'), $box->getTemplateId(), $box->getBoxId()); ?>">

        <!-- Content goes here -->

        <div class="dialog-buttons">
            <button class="btn btn-default pull-left" data-dialog-action="cancel">
                <?php echo t('Cancel')?>
            </button>
            
            <button type="button" data-dialog-action="submit" class="btn btn-primary pull-right">
                <?php echo t('Save')?>
            </button>
        </div>
    </form>
</div>

Save and Load Settings

All post parameters will be stored as settings when submitting the form.

To load a setting, you can use the following code in the view:

echo $box->getAttribute("AttributeName", "Default Value");



Register your Boxtype

After you have created the View and Controller, you have to register your box type to the PDF Designer. The best possibility to do this is the package controllers on_start event.

use Concrete\Package\PdfDesigner\Src\PDFDesigner;

...

PDFDesigner::getInstance()->registerBoxEditor("\Concrete\Package\MySampleExtension\Src\BoxEditor\SampleBox", t("Sample Box"));