M3 Security Fixes for pass 28
This commit is contained in:
@@ -7,240 +7,94 @@ use Storage;
|
||||
|
||||
class BillOfLadingService
|
||||
{
|
||||
public function fillBillOfLading($data, $outputPath = null)
|
||||
public function fillBillOfLading(array $data, ?string $outputPath = null): string
|
||||
{
|
||||
$templatePath = Storage::disk('public')->path('ETN-BOL2.pdf'); // storage_path('app/templates/ETN-BOL.pdf');
|
||||
$newfile = 'BOL_' . $data['bol_number'] . '.pdf'; // was using time()
|
||||
$outputPath = $outputPath ?? Storage::disk('public')->path($newfile); // storage_path('app/filled/BOL_' . $data['bol_number'] . '.pdf');
|
||||
$templatePath = Storage::disk('public')->path('ETN-BOL2.pdf');
|
||||
$newFile = 'BOL_' . ($data['bol_number'] ?? 'unknown') . '.pdf';
|
||||
$outputPath ??= Storage::disk('public')->path($newFile);
|
||||
|
||||
$pdf = new Fpdi;
|
||||
$pdf->setSourceFile($templatePath);
|
||||
|
||||
// Set source file
|
||||
$pageCount = $pdf->setSourceFile($templatePath);
|
||||
|
||||
// Import first page (you have 5 pages in your PDF)
|
||||
$tplIdx = $pdf->importPage(1);
|
||||
$pdf->AddPage();
|
||||
$pdf->useTemplate($tplIdx);
|
||||
|
||||
// Set font
|
||||
$pdf->SetFont('Helvetica', '', 10);
|
||||
$pdf->SetTextColor(0, 0, 0);
|
||||
// $pdf->SetLineWidth(50);
|
||||
|
||||
// Fill form fields at specific coordinates
|
||||
// You'll need to adjust X,Y positions based on your PDF
|
||||
|
||||
// Bill of Lading No.
|
||||
if (isset($data['bol_number'])) {
|
||||
$pdf->SetXY(126, 44);
|
||||
$pdf->Write(0, $data['bol_number']);
|
||||
}
|
||||
|
||||
// Date
|
||||
if (isset($data['date'])) {
|
||||
$pdf->SetXY(171, 44);
|
||||
$pdf->Write(0, $data['date']);
|
||||
}
|
||||
|
||||
// Shipper No.
|
||||
if (isset($data['shipper_no'])) {
|
||||
$pdf->SetXY(126, 53);
|
||||
$pdf->Write(0, $data['shipper_no']);
|
||||
}
|
||||
|
||||
// Master Bill No
|
||||
if (isset($data['master_bill_no'])) {
|
||||
$pdf->SetXY(172, 54);
|
||||
$pdf->Write(0, $data['master_bill_no']);
|
||||
}
|
||||
// Master Bill Yes
|
||||
if (isset($data['master_bill_yes'])) {
|
||||
$pdf->SetXY(183, 54);
|
||||
$pdf->Write(0, $data['master_bill_yes']);
|
||||
}
|
||||
// Master Bill Drops
|
||||
if (isset($data['master_bill_drops'])) {
|
||||
$pdf->SetXY(194, 55);
|
||||
$pdf->Write(0, $data['master_bill_drops']);
|
||||
}
|
||||
|
||||
// Consignor (FROM)
|
||||
if (isset($data['consignor_name'])) {
|
||||
$pdf->SetXY(21, 44);
|
||||
$pdf->Write(0, $data['consignor_name']);
|
||||
}
|
||||
if (isset($data['consignor_address1'])) {
|
||||
$pdf->SetXY(21, 48);
|
||||
$pdf->Write(0, $data['consignor_address1']);
|
||||
}
|
||||
if (isset($data['consignor_address2'])) {
|
||||
$pdf->SetXY(21, 52);
|
||||
$pdf->Write(0, $data['consignor_address2']);
|
||||
}
|
||||
if (isset($data['consignor_address3'])) {
|
||||
$pdf->SetXY(21, 56);
|
||||
$pdf->Write(0, $data['consignor_address3']);
|
||||
}
|
||||
|
||||
// Consignee (TO)
|
||||
if (isset($data['consignee_name'])) {
|
||||
$pdf->SetXY(21, 72);
|
||||
$pdf->Write(0, $data['consignee_name']);
|
||||
}
|
||||
if (isset($data['consignee_address1'])) {
|
||||
$pdf->SetXY(21, 76);
|
||||
$pdf->Write(0, $data['consignee_address1']);
|
||||
}
|
||||
if (isset($data['consignee_address2'])) {
|
||||
$pdf->SetXY(21, 80);
|
||||
$pdf->Write(0, $data['consignee_address2']);
|
||||
}
|
||||
if (isset($data['consignee_address3'])) {
|
||||
$pdf->SetXY(21, 84);
|
||||
$pdf->Write(0, $data['consignee_address3']);
|
||||
}
|
||||
|
||||
// Freight Collect
|
||||
if (isset($data['freight_collect'])) {
|
||||
$pdf->SetXY(127, 90);
|
||||
$pdf->Write(0, $data['freight_collect']);
|
||||
}
|
||||
|
||||
// Carrier Name
|
||||
if (isset($data['carrier_name'])) {
|
||||
$pdf->SetXY(10, 100);
|
||||
$pdf->Write(0, $data['carrier_name']);
|
||||
}
|
||||
|
||||
// Pieces
|
||||
if (isset($data['pieces'])) {
|
||||
$pdf->SetXY(6, 142);
|
||||
$pdf->Write(0, $data['pieces']);
|
||||
}
|
||||
|
||||
// Line class
|
||||
if (isset($data['line_class'])) {
|
||||
$pdf->SetXY(145, 142);
|
||||
$pdf->Write(0, $data['line_class']);
|
||||
}
|
||||
|
||||
// Line weight
|
||||
if (isset($data['line_weight'])) {
|
||||
$pdf->SetXY(177, 142);
|
||||
$pdf->Write(0, $data['line_weight']);
|
||||
}
|
||||
|
||||
// Total pieces
|
||||
if (isset($data['total_pieces'])) {
|
||||
$pdf->SetXY(6, 244);
|
||||
$pdf->Write(0, $data['total_pieces']);
|
||||
}
|
||||
|
||||
// Total weight
|
||||
if (isset($data['total_line_weight'])) {
|
||||
$pdf->SetXY(177, 244);
|
||||
$pdf->Write(0, $data['total_line_weight']);
|
||||
}
|
||||
|
||||
// Description of articles
|
||||
if (isset($data['description'])) {
|
||||
$pdf->SetXY(28, 140);
|
||||
$pdf->MultiCell(67, 4.5, $data['description'], 0, 'L');
|
||||
}
|
||||
|
||||
// Add more pages if needed (pages 2-5)
|
||||
// Repeat for other pages as needed
|
||||
$tplIdx = $pdf->importPage(2);
|
||||
$pdf->AddPage();
|
||||
$pdf->useTemplate($tplIdx);
|
||||
|
||||
// Bill of Lading No.
|
||||
if (isset($data['bol_number'])) {
|
||||
$pdf->SetXY(126, 44);
|
||||
$pdf->Write(0, $data['bol_number']);
|
||||
}
|
||||
|
||||
// Date
|
||||
if (isset($data['date'])) {
|
||||
$pdf->SetXY(171, 44);
|
||||
$pdf->Write(0, $data['date']);
|
||||
}
|
||||
|
||||
// Shipper No.
|
||||
if (isset($data['shipper_no'])) {
|
||||
$pdf->SetXY(126, 53);
|
||||
$pdf->Write(0, $data['shipper_no']);
|
||||
}
|
||||
|
||||
// Master Bill No
|
||||
if (isset($data['master_bill_no'])) {
|
||||
$pdf->SetXY(172, 54);
|
||||
$pdf->Write(0, $data['master_bill_no']);
|
||||
}
|
||||
// Master Bill Yes
|
||||
if (isset($data['master_bill_yes'])) {
|
||||
$pdf->SetXY(183, 54);
|
||||
$pdf->Write(0, $data['master_bill_yes']);
|
||||
}
|
||||
// Master Bill Drops
|
||||
if (isset($data['master_bill_drops'])) {
|
||||
$pdf->SetXY(194, 55);
|
||||
$pdf->Write(0, $data['master_bill_drops']);
|
||||
}
|
||||
|
||||
// Consignor (FROM)
|
||||
if (isset($data['consignor_name'])) {
|
||||
$pdf->SetXY(21, 44);
|
||||
$pdf->Write(0, $data['consignor_name']);
|
||||
}
|
||||
if (isset($data['consignor_address1'])) {
|
||||
$pdf->SetXY(21, 48);
|
||||
$pdf->Write(0, $data['consignor_address1']);
|
||||
}
|
||||
if (isset($data['consignor_address2'])) {
|
||||
$pdf->SetXY(21, 52);
|
||||
$pdf->Write(0, $data['consignor_address2']);
|
||||
}
|
||||
if (isset($data['consignor_address3'])) {
|
||||
$pdf->SetXY(21, 56);
|
||||
$pdf->Write(0, $data['consignor_address3']);
|
||||
}
|
||||
|
||||
// Consignee (TO)
|
||||
if (isset($data['consignee_name'])) {
|
||||
$pdf->SetXY(21, 72);
|
||||
$pdf->Write(0, $data['consignee_name']);
|
||||
}
|
||||
if (isset($data['consignee_address1'])) {
|
||||
$pdf->SetXY(21, 76);
|
||||
$pdf->Write(0, $data['consignee_address1']);
|
||||
}
|
||||
if (isset($data['consignee_address2'])) {
|
||||
$pdf->SetXY(21, 80);
|
||||
$pdf->Write(0, $data['consignee_address2']);
|
||||
}
|
||||
if (isset($data['consignee_address3'])) {
|
||||
$pdf->SetXY(21, 84);
|
||||
$pdf->Write(0, $data['consignee_address3']);
|
||||
}
|
||||
|
||||
// Freight Collect
|
||||
if (isset($data['freight_collect'])) {
|
||||
$pdf->SetXY(127, 90);
|
||||
$pdf->Write(0, $data['freight_collect']);
|
||||
}
|
||||
|
||||
// Carrier Name
|
||||
if (isset($data['carrier_name'])) {
|
||||
$pdf->SetXY(10, 100);
|
||||
$pdf->Write(0, $data['carrier_name']);
|
||||
}
|
||||
|
||||
// KD : add fields as you need
|
||||
// Save the PDF
|
||||
$this->renderPage($pdf, $data, 1, true);
|
||||
$this->renderPage($pdf, $data, 2, false);
|
||||
$pdf->Output('F', $outputPath);
|
||||
|
||||
return $outputPath;
|
||||
}
|
||||
|
||||
private function renderPage(Fpdi $pdf, array $data, int $pageNumber, bool $includeLineDetails): void
|
||||
{
|
||||
$template = $pdf->importPage($pageNumber);
|
||||
$pdf->AddPage();
|
||||
$pdf->useTemplate($template);
|
||||
$pdf->SetFont('Helvetica', '', 10);
|
||||
$pdf->SetTextColor(0, 0, 0);
|
||||
|
||||
foreach ($this->headerFields() as $field => [$x, $y]) {
|
||||
$this->writeField($pdf, $data, $field, $x, $y);
|
||||
}
|
||||
|
||||
if ($includeLineDetails) {
|
||||
foreach ($this->lineFields() as $field => [$x, $y]) {
|
||||
$this->writeField($pdf, $data, $field, $x, $y);
|
||||
}
|
||||
$this->writeDescription($pdf, $data);
|
||||
}
|
||||
}
|
||||
|
||||
private function writeField(Fpdi $pdf, array $data, string $field, float $x, float $y): void
|
||||
{
|
||||
if (! array_key_exists($field, $data) || $data[$field] === null || $data[$field] === '') {
|
||||
return;
|
||||
}
|
||||
|
||||
$pdf->SetXY($x, $y);
|
||||
$pdf->Write(0, (string) $data[$field]);
|
||||
}
|
||||
|
||||
private function writeDescription(Fpdi $pdf, array $data): void
|
||||
{
|
||||
$description = $data['description'] ?? null;
|
||||
if ($description === null || $description === '') {
|
||||
return;
|
||||
}
|
||||
|
||||
$pdf->SetXY(28, 140);
|
||||
$pdf->MultiCell(67, 4.5, (string) $description, 0, 'L');
|
||||
}
|
||||
|
||||
private function headerFields(): array
|
||||
{
|
||||
return [
|
||||
'bol_number' => [126, 44],
|
||||
'date' => [171, 44],
|
||||
'shipper_no' => [126, 53],
|
||||
'master_bill_no' => [172, 54],
|
||||
'master_bill_yes' => [183, 54],
|
||||
'master_bill_drops' => [194, 55],
|
||||
'consignor_name' => [21, 44],
|
||||
'consignor_address1' => [21, 48],
|
||||
'consignor_address2' => [21, 52],
|
||||
'consignor_address3' => [21, 56],
|
||||
'consignee_name' => [21, 72],
|
||||
'consignee_address1' => [21, 76],
|
||||
'consignee_address2' => [21, 80],
|
||||
'consignee_address3' => [21, 84],
|
||||
'freight_collect' => [127, 90],
|
||||
'carrier_name' => [10, 100],
|
||||
];
|
||||
}
|
||||
|
||||
private function lineFields(): array
|
||||
{
|
||||
return [
|
||||
'pieces' => [6, 142],
|
||||
'line_class' => [145, 142],
|
||||
'line_weight' => [177, 142],
|
||||
'total_pieces' => [6, 244],
|
||||
'total_line_weight' => [177, 244],
|
||||
];
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user