Files
api/services/nginx/app/modules/html2pdf/helpers/html2pdf_template_styles.php
T
Jepp9350 45328a6b72 Add PDF generation module with HTML2PDF integration.
This commit introduces a new PDF generation module leveraging the HTML2PDF library. The module generates PDFs from HTML templates, supports customization through styles and templates, and integrates with Minio for storage and retrieval. A test route is added for generating and serving PDFs dynamically.
2025-03-14 14:56:55 +01:00

106 lines
3.3 KiB
PHP

<?php
namespace html2pdf\helpers;
class html2pdf_template_styles
{
/**
* @var array Styles for different elements
*/
private array $styles = [
'title' => 'font-size: 48px; font-weight: bold; color: #000; text-align: center;',
'subtitle' => 'font-size: 24px; font-weight: normal; color: #555; text-align: center;',
'header' => 'font-size: 36px; font-weight: bold; color: #17375e;',
'footer' => 'font-size: 12px; font-weight: normal; color: #777;',
// Add more styles as needed
'default' => 'font-size: 12px; font-weight: normal; color: #000;',
'content' => 'padding-left: 1rem; padding-right: 1rem; padding-top: 0.5rem; padding-bottom: 0.5rem;',
];
/**
* Set the style for an element
* @param string $element Element name / identifier (E.g. 'title', 'subtitle', 'header', etc.)
* @param string $style CSS styles
*/
public function setStyle(string $element, string $style): void
{
$this->styles[$element] = $style;
}
/**
* Get the title for the template
* @param string|null $title Title
* @param string|null $subtitle Subtitle
* @return string HTML title
*/
public function getTitle(string $title = null, string $subtitle = null): string
{
$html = '';
if (!empty($title)) {
$title = htmlspecialchars($title, ENT_QUOTES, 'UTF-8');
$html .= '<h1 style="' . self::getStyle('title') . '">' . $title . '</h1>';
}
if (!empty($subtitle)) {
$subtitle = htmlspecialchars($subtitle, ENT_QUOTES, 'UTF-8');
$html .= '<h2 style="' . self::getStyle('subtitle') . '">' . $subtitle . '</h2>';
}
return $html;
}
/**
* Get the style for an element
* @param string $element Element name / identifier (E.g. 'title', 'subtitle', 'header', etc.)
* @return string CSS styles
*/
public function getStyle(string $element): string
{
return $this->styles[$element] ?? $this->styles['default'];
}
/**
* Get the header title for the template
* @param string|null $title Title
* @return string HTML header title
*/
public function getHeaderTitle(string $title = null): string
{
$html = '';
if (!empty($title)) {
$title = htmlspecialchars($title, ENT_QUOTES, 'UTF-8');
$html .= '<span style="' . self::getStyle('header') . '">' . $title . '</span>';
}
return $html;
}
/**
* Get the CSS (default) styles for the template
* @return string CSS styles
*/
public function getDefaultStyles(): string
{
return '
<style>
body {
font-family: Arial, sans-serif;
font-size: 12px;
color: #333;
}
h1 {
font-size: 24px;
margin-bottom: 10px;
}
p {
margin: 5px 0;
}
table {
width: 100%;
border-collapse: collapse;
}
td {
padding: 10px;
vertical-align: top;
}
</style>
';
}
}