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.
This commit is contained in:
Jepp9350
2025-04-09 14:19:06 +02:00
parent d2201448c1
commit 110ac5c28f
5 changed files with 154 additions and 12 deletions
+9 -1
View File
@@ -80,7 +80,15 @@ class pdf_generator implements pdf_generator_i
$this->filename = null;
$this->module_path = WD . '/modules/html2pdf/';
$this->template_path = $this->module_path . 'templates/';
$this->html2pdf = new Html2Pdf();
$this->html2pdf = new Html2Pdf(
'P',
'A4',
'en',
true,
'UTF-8',
array(0, 0, 0, 0)
);
$this->html2pdf->pdf->setDisplayMode('fullpage');
$this->templates = new html2pdf_templates();
//self::create_tmp_dir();
self::require_valid_paths([$this->template_path, $this->module_path]);
@@ -15,6 +15,10 @@ trait html2pdf_template_t
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';
@@ -25,7 +29,19 @@ trait html2pdf_template_t
}
// Generate the HTML for the image
$style = "height: {$height}px; width: {$width}px; text-align: {$align};";
$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}' />";
}
Binary file not shown.

After

Width:  |  Height:  |  Size: 336 KiB

@@ -6,40 +6,145 @@ use html2pdf\helpers\html2pdf_template;
class wash_certificate extends html2pdf_template
{
/**
* @inheritDoc
*/
public function getHtml($data = null): string
{
// self::addImage(self::pathSrc('truckwash-banner-white-compressed.png'), 'Certificate Image', 100, 100, 'center');
$html = '<page backimg="' . self::pathSrc('wash_certificate_template.png') . '" backtop="0mm" backbottom="0mm" backleft="0mm" backright="0mm">
<page_header></page_header>
<page_footer></page_footer>';
/** Add the styles */
$html = self::getDefaultStyles();
$html .= self::getDefaultStyles();
/** Add the header */
$html .= self::getHeader();
//$html .= self::getHeader();
/** Add the issuer company */
//$html .= self::getIssuerCompany();
/** Open the content div */
$html .= '<div style="' . self::getStyle('content') . '">';
/** Add the title */
$html .= self::getTitle('Wash Certificate');
//$html .= self::getTitle('Wash Certificate');
/** Add the seal number */
$html .= self::getSealNumber();
//$html .= self::getSealNumber();
/** Add the registration numbers, time, date and the carried out by */
$html .= self::getDetails();
//$html .= self::getDetails();
/** Add the provider details */
$html .= self::getProviderDetails();
//$html .= self::getProviderDetails();
/** Add the regarding this document */
//$html .= self::getRegardingThisDocument('DK');
/** Add the regarding this document in English */
$html .= self::getRegardingThisDocument('EN');
//$html .= self::getRegardingThisDocument('EN');
/** Add the signature */
$html .= self::getSignature();
//$html .= self::getSignature();
$html .= self::setDocumentFields(
'123',
'123',
'123',
'123',
);
$html .= self::setDocumentRegistrationNumberFields(
'123',
'123',
);
$html .= self::setDocumentCustomerSupplierFields(
'123',
'123',
);
$html .= self::setSignatureField(
'123',
);
/** Close the content div */
$html .= '</div>';
/** Close the page */
$html .= '</page>';
return $html;
}
private function setDocumentFields(string $document_number_column, string $time_and_date_column, string $carried_out_by_column, string $safety_seal_or_plom_number_column): string
{
$column_width_px = 125;
return '
<table style="position: absolute; top: 240px; left: 0; width: 100%; border-collapse: collapse; margin-left: 60px; margin-right: 60px; align-content: space-between;">
<tr>
<td style="width: ' . $column_width_px . 'px; align-content: center; text-align: center; border: 1px solid transparent;">
<strong>' . htmlspecialchars($document_number_column, ENT_QUOTES, 'UTF-8') . '</strong>
</td>
<td style="width: ' . $column_width_px . 'px; align-content: center; text-align: center; border: 1px solid transparent;">
<strong>' . htmlspecialchars($time_and_date_column, ENT_QUOTES, 'UTF-8') . '</strong>
</td>
<td style="width: ' . $column_width_px . 'px; align-content: center; text-align: center; border: 1px solid transparent;">
<strong>' . htmlspecialchars($carried_out_by_column, ENT_QUOTES, 'UTF-8') . '</strong>
</td>
<td style="width: ' . $column_width_px . 'px; align-content: center; text-align: center; border: 1px solid transparent;">
<strong>' . htmlspecialchars($safety_seal_or_plom_number_column, ENT_QUOTES, 'UTF-8') . '</strong>
</td>
</tr>
</table>
';
}
private function setDocumentRegistrationNumberFields(string $truck_registration_number_column, string $trailer_registration_number_column): string
{
$column_width_px = 132;
$spacer_width_px = 66;
return '
<table style="position: absolute; top: 323px; left: 0; width: 100%; border-collapse: collapse; margin-left: 178px; margin-right: 178px; align-content: space-between;">
<tr>
<td style="width: ' . $column_width_px . 'px; align-content: center; text-align: left; border: 1px solid transparent;">
<strong>' . htmlspecialchars($truck_registration_number_column, ENT_QUOTES, 'UTF-8') . '</strong>
</td>
<!-- Spacer -->
<td style="width: ' . $spacer_width_px . 'px; align-content: center; text-align: center; border: 1px solid transparent;">
<strong></strong>
</td>
<td style="width: ' . $column_width_px . 'px; align-content: center; text-align: left; border: 1px solid transparent;">
<strong>' . htmlspecialchars($trailer_registration_number_column, ENT_QUOTES, 'UTF-8') . '</strong>
</td>
</tr>
</table>
';
}
private function setDocumentCustomerSupplierFields(string $customer_details_column, string $supplier_details_column): string
{
$column_width_px = 257;
$spacer_width_px = 32;
$column_2_additional_width_px = 12;
return '
<table style="position: absolute; top: 720px; left: 0; width: 100%; border-collapse: collapse; margin-left: 72px; margin-right: 72px; align-content: space-between;">
<tr>
<td style="width: ' . $column_width_px . 'px; align-content: center; text-align: center; border: 1px solid transparent;">
<strong>' . htmlspecialchars($customer_details_column, ENT_QUOTES, 'UTF-8') . '</strong>
</td>
<!-- Spacer -->
<td style="max-width: ' . $spacer_width_px . 'px; align-content: center; text-align: center; border: 1px solid transparent; min-width: ' . $spacer_width_px . 'px; padding: 0; margin: 0;">
<div style="width: ' . $spacer_width_px . 'px; height: 1px; background-color: transparent;"></div>
</td>
<td style="width: ' . ($column_width_px + $column_2_additional_width_px) . 'px; align-content: center; text-align: center; border: 1px solid transparent;">
<strong>' . htmlspecialchars($supplier_details_column, ENT_QUOTES, 'UTF-8') . '</strong>
</td>
</tr>
</table>
';
}
private function setSignatureField(string $column_1): string
{
$column_width_px = 219;
$margin_left_px = 263;
$margin_right_px = $margin_left_px;
return '
<table style="position: absolute; top: 878px; left: 0; width: 100%; border-collapse: collapse; margin-left: ' . $margin_left_px . 'px; ' . $margin_right_px . 'px; align-content: space-between;">
<tr>
<td style="width: ' . $column_width_px . 'px; align-content: center; text-align: center; border: 1px solid black;">
<strong>' . htmlspecialchars($column_1, ENT_QUOTES, 'UTF-8') . '</strong>
</td>
</tr>
</table>
';
}
/**
* Get the seal number for the wash certificate
* @notation This method is used to generate the seal number for the wash certificate.
@@ -176,4 +281,14 @@ class wash_certificate extends html2pdf_template
</table>
';
}
private function getBackground(): string
{
// add the template pdf as a background
$template = self::pathSrc('wash_certificate_template.png');
$html = '<div style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; z-index: -1;">';
//$html .= self::addImage($template, 'Certificate Image', -1, -1, 'center', 'background');
$html .= '</div>';
return $html;
}
}
@@ -58,7 +58,10 @@ class pdfGeneratorRoute
$pdf_path = $pdf_generator->generate_pdf();
$response->success([
'pdf_path' => $pdf_path,
'message' => 'PDF generated successfully'
'message' => 'PDF generated successfully',
'link' => (new pdf_store())->getPresignedUrl(
$pdf_path
),
]);
} else {
(new logs_o())->add('orderInvoices', 'global', 0, 0, 'LIST_COLLECTED_INVOICES', 'User tried to access the list of collected order invoices without a valid session');