Created
February 9, 2025 09:59
-
-
Save waqas-mehmood-pk/b2ff8f8eacb5607d3e730113efc41a86 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App\Http\Controllers; | |
use Illuminate\Http\Request; | |
use Mpdf\Mpdf; | |
use App\Utils\Paths; | |
class PdfController extends Controller | |
{ | |
public function generatePdf() | |
{ | |
$xmlFilePath = Paths::clearedInvoice('invoice_57035_79394.xml', 'local'); | |
if (!file_exists($xmlFilePath)) { | |
abort(404, 'XML file not found.'); | |
} | |
// Crucial: Set format to A3 *before* creating the mPDF instance if you want A3. | |
$format = 'A4'; // Or 'A4', etc. Make this consistent. | |
$mpdf = new Mpdf([ | |
'format' => $format, // Use the $format variable | |
'mode' => 'utf-8', // Explicitly set UTF-8 mode (important for special characters) | |
'PDFA' => true, | |
'PDFAauto' => true, | |
// 'pdf_version' => '1.7', | |
'debug' => true, // Enable debugging to see if any errors are reported. | |
// Below Zeeshan shared file | |
'default_font_size' => 0, | |
'default_font' => 'sans serif', | |
'orientation' => 'P', | |
'tempDir' => public_path(), | |
]); | |
$mpdf->SetTitle('PDF/A-3 Compliant Document'); | |
$mpdf->SetAuthor('Saudi Brokers'); | |
$mpdf->SetCreator('Saudi Brokers'); | |
$mpdf->SetSubject('Invoice Document'); | |
$mpdf->SetKeywords('Invoice, PDF/A-3, Compliance'); | |
$mpdf->showImageErrors = true; | |
$mpdf->pdf_version = '1.7'; | |
$mpdf->defaultPagebreakStyle = 'sRGB IEC61966-2.1'; | |
$mpdf->SetDefaultImageAlpha(1); // Removes transparency from images | |
$mpdf->showOCG = false; // Disable optional content groups (OCG) | |
$mpdf->AddPage(); | |
$html = '<h1 style="text-align: center;">PDF/A-3 Compliant Document</h1>'; | |
$html .= '<p>This document is generated using mPDF and is PDF/A-3 compliant with an attached XML file.</p>'; | |
$mpdf->WriteHTML($html); | |
$xmlData = file_get_contents($xmlFilePath); | |
$mpdf->SetAssociatedFiles([ | |
// [ | |
// 'name' => basename($xmlFilePath), // Use basename for the filename | |
// 'filename' => basename($xmlFilePath), | |
// 'description' => 'XML Invoice Attachment', | |
// 'mime' => 'application/xml', | |
// 'relationship' => 'Data', // or 'Alternative' if you're also embedding the XML | |
// 'AFRelationship' => 'Alternative', // Only needed if the relationship is 'Alternative'. | |
// 'data' => $xmlData, | |
// // 'path' => $xmlFilePath, // Remove the path. It might be causing issues. | |
// ] | |
[ | |
'name' => basename($xmlFilePath), | |
'description' => 'XML Invoice Attachment', | |
'mime' => 'application/xml', | |
'AFRelationship'=> 'Alternative', | |
'content' => $xmlData, | |
] | |
]); | |
$rdf = '<rdf:Description rdf:about="" xmlns:zf="urn:ferd:pdfa:CrossIndustryDocument:invoice:1p0#">'."\n"; | |
$rdf .= ' <zf:DocumentType>INVOICE</zf:DocumentType>'."\n"; | |
$rdf .= ' <zf:DocumentFileName>ZUGFeRD-invoice.xml</zf:DocumentFileName>'."\n"; | |
$rdf .= ' <zf:Version>1.0</zf:Version>'."\n"; | |
$rdf .= ' <zf:ConformanceLevel>BASIC</zf:ConformanceLevel>'."\n"; | |
$rdf .= '</rdf:Description>'."\n"; | |
$mpdf->SetAdditionalXmpRdf($rdf); | |
// Output and return | |
$pdfContent = $mpdf->Output('invoice.pdf', 'S'); // Give a filename for download | |
return response($pdfContent, 200) | |
->header('Content-Type', 'application/pdf') | |
->header('Content-Disposition', 'attachment; filename="invoice.pdf"'); // Force download | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment