Commit ce951cf3 by Mohammad Izzat Johari

no message

parent 0db18a05
<?php
use Illuminate\Support\Facades\Log;
if (!function_exists('convertPdfVersion')) {
/**
* Identifies the correct Ghostscript binary based on OS.
*/
function getGsPath()
{
$path = (PHP_OS_FAMILY === 'Windows') ? 'gswin64c' : 'gs';
// Check if the binary is actually executable/accessible
$checkCmd = (PHP_OS_FAMILY === 'Windows') ? "where $path" : "which $path";
$exists = shell_exec($checkCmd);
if (!$exists) {
throw new \Exception("Ghostscript binary ($path) not found. Please install it on this " . PHP_OS_FAMILY . " environment.");
}
return $path;
}
}
if (!function_exists('convertPdfVersion')) {
/**
* Converts a PDF to version 1.4 using Ghostscript to ensure compatibility with FPDI.
*
* @param string $path Full path to the source PDF file.
* @return string Full path to the converted PDF file.
*/
function convertPdfVersion($inputPath,$outputPath)
{
// Basic check to ensure the file exists before processing
if (!file_exists($inputPath)) {
Log::error("Source PDF for conversion not found at: {$path}");
return $inputPath;
}
// 2. Prepare the Ghostscript command
// escapeshellarg() is used for security and to handle spaces in file paths
//$gsPath = "gs";
// On Windows/Laragon, use gswin64c instead of gs
// $gsPath = 'gswin64c';
$gsPath = getGsPath();
$command = "gswin64c -sDEVICE=pdfwrite -dCompatibilityLevel=1.4 -dNOPAUSE -dQUIET -dBATCH -sOutputFile=" . escapeshellarg($outputPath) . " " . escapeshellarg($inputPath);
// 3. Execute the command via the system shell
$output = shell_exec($command);
// 4. Verify if the conversion was successful
if (!file_exists($outputPath)) {
Log::error("Ghostscript failed to convert PDF. Output: {$output}");
// Return original path as a fallback so the app doesn't crash,
// though FPDI might still throw the compression error later.
return $outputPath;
}
return $outputPath;
}
}
...@@ -12,7 +12,10 @@ ...@@ -12,7 +12,10 @@
"autoload": { "autoload": {
"psr-4": { "psr-4": {
"LaravelHsm\\HsmCrypto\\": "src/" "LaravelHsm\\HsmCrypto\\": "src/"
} },
"files": [
"app/Helpers/PdfHelper.php"
]
}, },
"require": { "require": {
"php": "^8.1", "php": "^8.1",
......
...@@ -383,17 +383,45 @@ class Pkcs11Command ...@@ -383,17 +383,45 @@ class Pkcs11Command
throw new \Exception('Failed to decrypt PDF content.'); throw new \Exception('Failed to decrypt PDF content.');
} }
$fpdi = new MyPdf(); $tempDir = storage_path('app/temp_pdf');
if (!file_exists($tempDir)) mkdir($tempDir, 0777, true);
$uniqueId = uniqid();
$inputPath = $tempDir . "/in_{$uniqueId}.pdf";
$outputPath = $tempDir . "/out_{$uniqueId}.pdf";
try {
file_put_contents($inputPath, $decryptedPdf);
// Write decrypted bytes to a memory stream $decryptedPdf = convertPdfVersion($inputPath,$outputPath);
// $pdfStream = fopen('php://memory', 'r+');
// fwrite($pdfStream, $decryptedPdf);
// rewind($pdfStream);
// Count pages $cleanPdfContent = file_get_contents($outputPath);
$pdfStream = StreamReader::createByString($decryptedPdf);
$fpdi = new MyPdf();
$pdfStream = StreamReader::createByString($cleanPdfContent);
$pageCount = $fpdi->setSourceFile($pdfStream); $pageCount = $fpdi->setSourceFile($pdfStream);
} catch (\Exception $e) {
Log::error('PDF Processing Error: ' . $e->getMessage());
throw $e;
} finally {
// 7. Cleanup - Never leave temp files on a production server
if (file_exists($inputPath)) @unlink($inputPath);
if (file_exists($outputPath)) @unlink($outputPath);
}
// $fpdi = new MyPdf();
// // Write decrypted bytes to a memory stream
// // $pdfStream = fopen('php://memory', 'r+');
// // fwrite($pdfStream, $decryptedPdf);
// // rewind($pdfStream);
// // Count pages
// $pdfStream = StreamReader::createByString($decryptedPdf);
// $pageCount = $fpdi->setSourceFile($pdfStream);
// 3️⃣ Add watermark & rebuild PDF in memory // 3️⃣ Add watermark & rebuild PDF in memory
for ($i = 1; $i <= $pageCount; $i++) { for ($i = 1; $i <= $pageCount; $i++) {
$tplIdx = $fpdi->importPage($i); $tplIdx = $fpdi->importPage($i);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment