PNG
JPG
BMP
TIFF
PDF
如何通过云 PHP SDK 加密 PDF
要加密 PDF,我们将使用 Aspose.PDF Cloud PHP SDK 这个云 SDK 通过 Aspose.PDF REST API 协助 PHP 程序员开发基于云的 PDF 创建、注释、编辑和转换应用程序。只需在 Aspose for Cloud 创建一个帐户并获取您的应用程序信息。一旦您拥有 App SID 和密钥,您就可以使用 Aspose.PDF Cloud PHP SDK。
包管理器控制台命令
composer install
通过云 PHP SDK 加密 PDF 的步骤
Aspose.PDF Cloud 开发人员可以轻松地用几行代码加载和加密 PDF。
- 上传必要的 PDF 文件。
- 使用 AESx256 加密和用户及所有者密码对其进行加密。
- 将受保护的文件下载回本地系统。
使用 PHP 加密 PDF
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";
}
}
加密 PDF
加密 PDF 文档是一种简单而强大的方式来保护敏感内容,确保隐私,并保持对文档使用方式的控制。无论您是分享商业合同、财务报告还是个人记录,加密都有助于保护您的信息免受未经授权的访问、篡改或滥用。 通过应用加密和设置适当的权限,您可以增强文档的安全性,确保符合行业法规,并与您的受众建立信任。在当今的数字世界中,PDF 加密不仅仅是一个功能——它是负责任的数据处理的必需品。 使用 Aspose.PDF Cloud PHP SDK 加密 PDF 文档。
使用我们的 PHP 库,您可以:
- 以文本或图像格式添加 PDF 文档的页眉和页脚。
- 向 PDF 文档添加表格和印章(文字或图像)。
- 将多个 PDF 文档附加到现有文件。
- 处理 PDF 附件、注释和表单字段。
- 对 PDF 文档应用加密或解密并设置密码。
- 从页面或整个 PDF 文档中删除所有印章和表格。
- 按 ID 从 PDF 文档中删除特定印章或表格。
- 替换 PDF 页面或整个文档中的单个或多个文本实例。
- 提供广泛支持将 PDF 文档转换为各种其他文件格式。
- 提取 PDF 文件的各种元素并优化 PDF 文档。
- 您可以尝试我们的免费应用在线测试功能。