Coding standards
Security in ResourceSpace
Developer reference
Database
Action functions
Admin functions
Ajax functions
Annotation functions
API functions
Collections functions
Comment functions
Config functions
CSV export functions
Dash functions
Debug functions
Encryption functions
Facial recognition functions
File functions
General functions
Language functions
Log functions
Login functions
Message functions
Migration functions
Node functions
PDF functions
Plugin functions
Render functions
Reporting functions
Request functions
Research functions
Slideshow functions
Theme permission functions
User functions
Video functions
Database functions
Metadata functions
Resource functions
Search functions
Map functions
Job functions
Tab functions
Test functions

generate_pdf()

Description

Takes an HTML template suitable for HTML2PDF library and generates a PDF file if successfull

and $save_on_server = true, it will be save on the server
placeholders found in the HTML template

Parameters

ColumnTypeDefaultDescription
$html_template_path string HTML template path
$filename string The file name of the generated PDF file. If this is an actual path,
$bind_placeholders array array A map of all the values that are meant to replace any
$save_on_server boolean false If true, PDF file will be saved to the filename path
$pdf_properties array array Properties of the PDF file (e.g. author, title, font, margins)

Return

boolean

Location

include/pdf_functions.php lines 70 to 190

Definition

 
function generate_pdf($html_template_path$filename, array $bind_placeholders = array(), $save_on_server false, array $pdf_properties = array())
    {
    global 
$applicationname$baseurl$baseurl_short$storagedir$linkedheaderimgsrc$language$contact_sheet_date_include_time$contact_sheet_date_wordy;

    
$html2pdf_path __DIR__ '/../lib/html2pdf/vendor/autoload.php';
    if(!
file_exists($html2pdf_path))
        {
        
trigger_error('html2pdf class file is missing. Please make sure you have it under lib folder!');
        }
    require_once 
$html2pdf_path;

    
// Do we have a physical HTML template
    
if(!file_exists($html_template_path))
        {
        
trigger_error('File "' $html_template_path '" does not exist!');
        }

    
$html file_get_contents($html_template_path);
    if(
false === $html)
        {
        return 
false;
        }

    
// General placeholders available to HTML templates
    
$general_params = array(
        
'applicationname' => $applicationname,
        
'baseurl'         => $baseurl,
        
'baseurl_short'   => $baseurl_short,
        
'filestore'       => $storagedir,
        
'filename'        => (!$save_on_server $filename basename($filename)),
        
'date'            => nicedate(date('Y-m-d H:i:s'), $contact_sheet_date_include_time$contact_sheet_date_wordy),
    );

    if(
'' != $linkedheaderimgsrc)
        {
        
$general_params['linkedheaderimgsrc'] = $linkedheaderimgsrc;
        }

    
$bind_params array_merge($general_params$bind_placeholders);

    foreach(
$bind_params as $param => $param_value)
        {
        
// Bind [%param%] placeholders to their values
        
$html str_replace('[%' $param '%]'escape($param_value), $html);
        
        
// replace \r\n with <br />. This is how they do it at the moment at html2pdf.fr
        
$html str_replace("\r\n"'<br />'$html);
        }

    
$html process_if_statements($html$bind_params);
    
    
// Last resort to clean up PDF templates by searching for all remaining placeholders
    
$html preg_replace('/\[%.*%\]/'''$html);

    
// Setup PDF
    
$pdf_orientation 'P';
    
$pdf_format      'A4';
    
$pdf_language    resolve_pdf_language();
    
$pdf_unicode     true;
    
$pdf_encoding    'UTF-8';
    
$pdf_margins     = array(5558);

    if(
array_key_exists('orientation'$pdf_properties) && '' != trim($pdf_properties['orientation']))
        {
        
$pdf_orientation $pdf_properties['orientation'];
        }

    if(
array_key_exists('format'$pdf_properties) && '' != trim($pdf_properties['format']))
        {
        
$pdf_format $pdf_properties['format'];
        }

    if(
array_key_exists('language'$pdf_properties) && '' != trim($pdf_properties['language']))
        {
        
$pdf_language $pdf_properties['language'];
        }

    if(
array_key_exists('margins'$pdf_properties) && is_array($pdf_properties['margins']) && !== count($pdf_properties['margins']))
        {
        
$pdf_margins $pdf_properties['margins'];
        }

    
$html2pdf = new Spipu\Html2Pdf\Html2Pdf($pdf_orientation$pdf_format$pdf_language$pdf_unicode$pdf_encoding$pdf_margins);

    
// Set PDF title
    
if(array_key_exists('title'$pdf_properties) && '' != trim($pdf_properties['title']))
        {
        
$html2pdf->pdf->SetTitle($pdf_properties['title']);
        }

    
// Set PDF author
    
if(array_key_exists('author'$pdf_properties) && '' != trim($pdf_properties['author']))
        {
        
$html2pdf->pdf->SetAuthor($pdf_properties['author']);
        }

    
// Set PDF subject
    
if(array_key_exists('subject'$pdf_properties) && '' != trim($pdf_properties['subject']))
        {
        
$html2pdf->pdf->SetSubject($pdf_properties['subject']);
        }

    
// Set PDF font family
    
if(array_key_exists('font'$pdf_properties) && '' != trim($pdf_properties['font']))
        {
        
$html2pdf->setDefaultFont($pdf_properties['font']);
        }

    
$html2pdf->WriteHTML($html);

    if(
$save_on_server)
        {
        
$html2pdf->Output($filename'F');
        }
    else
        {
        
$html2pdf->Output($filename);
        }

    return 
true;
    }

This article was last updated 5th December 2024 06:35 Europe/London time based on the source file dated 27th November 2024 15:30 Europe/London time.