Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save rajvanshipradeep15/7f293b0d2c8f85d7d46d000d22619b43 to your computer and use it in GitHub Desktop.
Save rajvanshipradeep15/7f293b0d2c8f85d7d46d000d22619b43 to your computer and use it in GitHub Desktop.
validate pdf password protection
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
require_once './vendor/setasign/fpdi/pdf_parser.php';
if( 1 == $_GET['file'] ) {
//file which we need to open in browser.
$filename="pdf_sample.pdf";
$header="Content-Disposition: inline; filename=$filename;"; // Send File Name
} else {
//password protected file
$filename="pdf_sample_protected.pdf";
$header="Content-Disposition: attachment; filename=$filename;"; // Send File Name
}
$file="./user_pdfs/$filename";
// using FPDI library
// https://github.com/Setasign/FPDI
// check if pdf is password protected or not.
try
{
$pdf = new pdf_parser($file);
}
catch (Exception $e)
{
if ( "File is encrypted!" == $e->getMessage() ) {
echo "file is encrypted";
} else {
echo "File is not encrypted";
}
exit;
}
$len = filesize($file); // Calculate File Size
ob_clean();
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type:application/pdf"); // Send type of file
header($header );
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".$len); // Send File Size
@readfile($file);
exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment