PNG JPG BMP TIFF PDF
Aspose.PDF  for PHP

Password Protect PDF in PHP SDK

API for encrypting PDF documents using Cloud PHP SDK.

Get Started

How to encrypt PDF via Cloud PHP SDK

To encrypt PDF, we’ll use Aspose.PDF Cloud PHP SDK This Cloud SDK assists PHP programmers in developing cloud-based PDF creator, annotator, editor, and converter apps using PHP programming language via Aspose.PDF REST API. Simply create an account at Aspose for Cloud and get your application information. Once you have the App SID & key, you are ready to give the Aspose.PDF Cloud PHP SDK.

Package Manager Console Command

     
    composer install

Steps to encrypt PDF via Cloud PHP SDK

Aspose.PDF Cloud developers can easily load & encrypt PDF in just a few lines of code.

  1. Upload Necessary PDF Files.
  2. Encrypting it with a user and owner password using AESx256 encryption.
  3. Downloading the protected file back to the local system.
 

Encrypt PDF using PHP


    require_once 'vendor/autoload.php';

    use Aspose\PDF\Configuration;
    use Aspose\PDF\Model\CryptoAlgorithm;
    use Aspose\PDF\Api\PdfApi;

    $credentials = json_decode(file_get_contents(__DIR__ . "/../../../Credentials/credentials.json"), true);

    $configParams = [
        'LOCAL_FOLDER' => 'C:\\Samples\\',
        'PDF_DOCUMENT_NAME' => 'sample.pdf',
        'LOCAL_RESULT_DOCUMENT_NAME' => 'output_sample.pdf',
        'ENCRYPT_ALGORITHM' => CryptoAlgorithm::AE_SX256,
        'USER_PASSWORD' => 'User-Password',
        'OWNER_PASSWORD' => 'Owner-Password',
    ];

    class PdfEncoder {
        private $pdfApi;
        private $configParams;

        private function _create_rest_api() {
            $credentials = json_decode(file_get_contents("./Credentials/credentials.json"), true);

            $configAuth = new Configuration();
            $configAuth->setAppKey($credentials['key']);
            $configAuth->setAppSid($credentials['id']);

            $this->pdfApi = new PdfApi(null, $configAuth);
        }

        public function __construct($config) {
            $this->configParams = $config;
            $this->_create_rest_api();
        }

        public function uploadDocument() {
            $filePath = $this->configParams['LOCAL_FOLDER'] . $this->configParams['PDF_DOCUMENT_NAME'];
            $fileData = file_get_contents($filePath);

            $this->pdfApi->uploadFile($this->configParams['PDF_DOCUMENT_NAME'], $fileData);
            echo "File: '{$this->configParams['PDF_DOCUMENT_NAME']}' successfully uploaded.\n";
        }

        public function downloadResult() {
            $result = $this->pdfApi->downloadFile($this->configParams['PDF_DOCUMENT_NAME']);
            $outputPath = $this->configParams['LOCAL_FOLDER'] . $this->configParams['LOCAL_RESULT_DOCUMENT_NAME'];
            file_put_contents($outputPath, $result['body']);
            echo "Downloaded: {$outputPath}\n";
        }

        public function encryptDocument() {
            $userPassword = base64_encode($this->configParams['USER_PASSWORD']);
            $ownerPassword = base64_encode($this->configParams['OWNER_PASSWORD']);

            $response = $this->pdfApi->postEncryptDocumentInStorage(
                $this->configParams['PDF_DOCUMENT_NAME'],
                $userPassword,
                $ownerPassword,
                $this->configParams['ENCRYPT_ALGORITHM']
            );

            if ($response['body']['code'] == 200) {
                echo "encryptDocument(): Document '{$this->configParams['PDF_DOCUMENT_NAME']}' successfully encrypted.\n";
            } else {
                throw new Exception("encryptDocument(): Failed to encrypt document '{$this->configParams['PDF_DOCUMENT_NAME']}'. Response code: {$response['code']}");
            }
        }
    }

    function main() {
        global $configParams;

        $encoder = new PdfEncoder($configParams);

        try {
            $encoder->uploadDocument();
            $encoder->encryptDocument();
            $encoder->downloadResult();
        } catch (Exception $e) {
            echo "Error: " . $e->getMessage() . "\n";
        }
    }
 

Encrypt PDF

Encrypting a PDF document is a simple yet powerful way to protect sensitive content, ensure privacy, and maintain control over how the document is used. Whether you’re sharing business contracts, financial reports, or personal records, encryption helps safeguard your information from unauthorized access, tampering, or misuse. By applying encryption and setting appropriate permissions, you enhance the document’s security, ensure compliance with industry regulations, and build trust with your audience. In today’s digital world, PDF encryption is not just a feature—it’s a necessity for responsible data handling. Encrypt PDF documents with Aspose.PDF Cloud PHP SDK.

With our PHP library you can:

  • Add PDF document’s header & footer in text or image format.
  • Add tables & stamps (text or image) to PDF documents.
  • Append multiple PDF documents to an existing file.
  • Work with PDF attachments, annotations, & form fields.
  • Apply encryption or decryption to PDF documents & set a password.
  • Delete all stamps & tables from a page or entire PDF document.
  • Delete a specific stamp or table from the PDF document by its ID.
  • Replace single or multiple instances of text on a PDF page or from the entire document.
  • Extensive support for converting PDF documents to various other file formats.
  • Extract various elements of PDF files & make PDF documents optimized.
  • You can try out our free Apps to test the functionality online.