starlight93/html-pdf-excel

lightweight and fast library to render sheet-formatted-template to xlsx, pdf, and html easily. Really easy!

v1.0.3 2024-07-26 22:34 UTC

This package is auto-updated.

Last update: 2024-11-27 03:22:17 UTC


README

Thanks to TCPDF and PHPSpreadsheet. This library is a helper to generate html, pdf, and excel from excel-based template and array data.

Example

Installation

composer require starlight93/html-pdf-excel

Usage

//  your array of data, see: /testing/1data.json
$data = [
    'key'=>'value',
    'detail' => [
        [
            'key_detail1'   => 'value1',
            'amount'    => 2000
        ],[
            'key_detail1'   => 'value2',
            'amount'    => 1000
        ]
    ]
];

//  example: function to get string from a text file
public function getFromFile( $path ){
    $file = fopen( $path, "r" );
    $dt =  fread( $file ,filesize($path) ) ;
    fclose($file);
    return $dt;
}

//  your template string, see: /testing/1template.txt
$template = getFromFile(  "your text file path" );

$renderer = new \Starlight93\HtmlPdfExcel\Renderer;
//  if you want to try with /testing data dan template just pass true to Renderer Construct
// $renderer = new \Starlight93\HtmlPdfExcel\Renderer( true );
// with parameter true will take the data and template from /testing dir, so you can focus on config only

//  ======================= example rendering PDF start
$renderer->renderPDF(data: $data, template: $template, config: [
    'title' =>'testing',
    'break' => true,
    'left'  => 10,
    'top'   => 10,
    'right' => 12,
    'orientation'   =>'L',
    'size'  => [210, 297], // can be A4, F4, etc
    'fontSize'  => 10,
    'callback' => function( $pdf ){
        //  see tcpdf documentation for any available property and function
    },
    'header_callback' => function( $hd ){
        //  see tcpdf documentation for any available property and function
    },

    'footer_callback' => function( $ft ){
        //  see tcpdf documentation for any available property and function
    },
]);
//  ======================= end


//  ======================= example rendering XLS start
$renderer->renderXls(data: $data, template: $template, config: [
    'title' => 'testing',
    'break' => true, // to separate into 
    'orientation'   =>'L',
    'size'  => 9, // see https://github.com/PHPOffice/PhpSpreadsheet/blob/master/src/PhpSpreadsheet/Worksheet/PageSetup.php
    'fontSize'  => 10
]);
//  ======================= end

//  ======================= example rendering XLS start
$renderer->renderHtml(data: $data, template: $template, config: [
    'title' =>'testing',
    // 'orientation'   =>'L',
    // 'size'  => 'A4',
    'fontSize'  => 10
]);
//  ======================= end

Basic Syntax

Inside Looping Syntax

Additional Features

You can also use any mathematic formula to get dynamic value as the image above such as summary or etc.

Dynamic Columns (table)

Below shows how to generate dynamic templates. Focus to .dynamic string which is shown in the picture

Example

//  your array of data for dynamic columns
$data = [
    'key'=>'value',
    'details' => [
        [
            'key_detail1'   => 'value1',
            'amount'    => 2000,
            'another1'    => 5000,
            'another2'    => 1000,
        ],[
            'key_detail1'   => 'value2',
            'amount'    => 1000,
            'amount'    => 2000,
            'another1'    => 2000,
            'another2'    => 4000
        ],[
            'key_detail1'   => 'value2',
            'amount'    => 1000,
            'amount'    => 2000,
            'another1'    => 3000,
            'another2'    => 2000
        ]
    ],

    // key below is to generate dynamic columns
    'dynamic' => [
        "Col Dynamic 1"=>"\$details.another1",
        "Col Dynamic 2"=>"\$details.another2",
    ]
];

By using data array above, the .dynamic header in the template will be replaced with the following dynamic key in the data array. So the final template result will be like this picture shown below:

Example