Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
H
hsmcrypto
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Mohammad Izzat Johari
hsmcrypto
Commits
ce951cf3
Commit
ce951cf3
authored
Jan 26, 2026
by
Mohammad Izzat Johari
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
no message
parent
0db18a05
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
106 additions
and
9 deletions
+106
-9
app/Helpers/Pdfhelper.php
+66
-0
composer.json
+4
-1
src/Pkcs11Command.php
+36
-8
No files found.
app/Helpers/Pdfhelper.php
0 → 100644
View file @
ce951cf3
<?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
;
}
}
composer.json
View file @
ce951cf3
...
@@ -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"
,
...
...
src/Pkcs11Command.php
View file @
ce951cf3
...
@@ -383,16 +383,44 @@ class Pkcs11Command
...
@@ -383,16 +383,44 @@ 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
);
// Write decrypted bytes to a memory stream
$uniqueId
=
uniqid
();
// $pdfStream = fopen('php://memory', 'r+');
$inputPath
=
$tempDir
.
"/in_
{
$uniqueId
}
.pdf"
;
// fwrite($pdfStream, $decryptedPdf);
$outputPath
=
$tempDir
.
"/out_
{
$uniqueId
}
.pdf"
;
// rewind($pdfStream);
// Count pages
try
{
$pdfStream
=
StreamReader
::
createByString
(
$decryptedPdf
);
file_put_contents
(
$inputPath
,
$decryptedPdf
);
$pageCount
=
$fpdi
->
setSourceFile
(
$pdfStream
);
$decryptedPdf
=
convertPdfVersion
(
$inputPath
,
$outputPath
);
$cleanPdfContent
=
file_get_contents
(
$outputPath
);
$fpdi
=
new
MyPdf
();
$pdfStream
=
StreamReader
::
createByString
(
$cleanPdfContent
);
$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
++
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment