Files
api/services/nginx/app/modules/html2pdf/helpers/html2pdf_template_t.php
T
Jepp9350 110ac5c28f Enhance PDF generation and wash certificate functionality
Added support for presigned URLs in PDF responses and improved image handling by dynamically setting dimensions. Refactored and expanded wash certificate templates with additional fields and styles, along with a new background template. Updated HTML2PDF configuration for better rendering and full-page display mode.
2025-04-09 14:19:06 +02:00

54 lines
1.6 KiB
PHP

<?php
namespace html2pdf\helpers;
trait html2pdf_template_t
{
/**
* @inheritDoc
*/
public function addImage(string $imagePath, string $altText = '', int|null $height = 100, int|null $width = 100, string $align = 'center'): string
{
// Validate the image path (Local, then URL)
if (!file_exists($imagePath)) {
if (filter_var($imagePath, FILTER_VALIDATE_URL) === false) {
throw new \InvalidArgumentException("Invalid image path: $imagePath");
}
}
// If the image is above 0, set the height and width to the image size
if ($height > 0 && $width > 0) {
list($width, $height) = getimagesize($imagePath);
}
// Validate the height and width
if (empty($height)) {
$height = 'auto';
}
if (empty($width)) {
$width = 'auto';
}
// Generate the HTML for the image
$style = '';
if ($height !== 'auto') {
$style .= "max-height: {$height}px; ";
}
if ($width !== 'auto') {
$style .= "max-width: {$width}px; ";
}
if ($align !== 'center') {
$style .= "text-align: {$align}; ";
}
// Set the style for the image
$style = rtrim($style, '; ');
// Return the HTML for the image
return "<img src='{$imagePath}' alt='{$altText}' style='max-height: {$height}px; max-width: {$width}px; {$style}' />";
}
public function pathSrc(string $path): string
{
// Assuming the path is relative to the module directory
return WD . '/modules/html2pdf/src/' . $path;
}
}