PNG
JPG
BMP
TIFF
PDF
如何通过云 PHP SDK 压缩 PDF
要压缩 PDF,我们将使用 Aspose.PDF Cloud PHP SDK 此云 SDK 协助 PHP 程序员开发基于云的 PDF 创建、注释、编辑和转换应用程序,使用 PHP 编程语言通过 Aspose.PDF REST API。只需在 Aspose for Cloud 创建一个账户并获取您的应用程序信息。一旦您拥有 App SID 和密钥,您就可以开始使用 Aspose.PDF Cloud PHP SDK。此方法利用 Aspose.PDF Cloud SDK for PHP 来有效地优化 PDF 文档。通过自动化上传、优化和下载过程,它简化了开发人员的工作流程。使用各种优化选项确保结果 PDF 在不影响质量的情况下更小。
包管理器控制台命令
composer install
通过云 PHP 压缩 PDF 的步骤
Aspose.PDF Cloud 开发人员可以轻松地在几行代码中加载和优化 PDF。
- 上传 PDF 文档
- 压缩 PDF 文档
- 下载优化后的 PDF 文档
使用 PHP 压缩 PDF
require __DIR__.'vendor\autoload.php';
use Aspose\PDF\Configuration;
use Aspose\PDF\Api\PdfApi;
$credentials = json_decode(file_get_contents(__DIR__ . 'credentials.json'), true);
$configParams = [
"LOCAL_FOLDER" => "C:\\Samples\\",
"PDF_DOCUMENT_NAME" => "sample.pdf",
"TEMP_FOLDER" => "TempPdfCloud",
"LOCAL_RESULT_DOCUMENT_NAME" => "output_sample.pdf",
];
class PdfCompress
{
private $pdfApi;
private $configParams;
private function _create_rest_api() {
$credentials = json_decode(file_get_contents("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"] . DIRECTORY_SEPARATOR . $this->configParams["PDF_DOCUMENT_NAME"];
$fileData = file_get_contents($filePath);
$storagePath = $this->configParams["TEMP_FOLDER"] . DIRECTORY_SEPARATOR . $this->configParams["PDF_DOCUMENT_NAME"];
$this->pdfApi->uploadFile($storagePath, $fileData);
echo "File: '{$this->configParams["PDF_DOCUMENT_NAME"]}' successfully uploaded." . PHP_EOL;
}
public function downloadResult()
{
$fileName = $this->configParams["TEMP_FOLDER"] . DIRECTORY_SEPARATOR . $this->configParams["PDF_DOCUMENT_NAME"];
$downloaded = $this->pdfApi->downloadFile($fileName);
$filePath = $this->configParams["LOCAL_FOLDER"] . DIRECTORY_SEPARATOR . $this->configParams["LOCAL_RESULT_DOCUMENT_NAME"];
file_put_contents($filePath, $downloaded);
echo "Downloaded: {$filePath}" . PHP_EOL;
}
public function compressPdfDocument()
{
$options = new \Aspose\PDF\Model\OptimizeOptions();
$options->setAllowReusePageContent(true);
$options->setCompressImages(true);
$options->setImageQuality(100);
$options->setLinkDuplcateStreams(true);
$options->setRemoveUnusedObjects(true);
$options->setRemoveUnusedStreams(true);
$options->setUnembedFonts(true);
$response = $this->pdfApi->postOptimizeDocument(
$this->configParams["PDF_DOCUMENT_NAME"],
$options,
null,
$this->configParams["TEMP_FOLDER"]
);
if ($response->getCode() != 200) {
echo "compressPdfDocument(): Failed to compress the PDF document!" . PHP_EOL;
} else {
echo "compressPdfDocument(): Successfully compressed the PDF document '{$this->configParams["PDF_DOCUMENT_NAME"]}' !" . PHP_EOL;
}
}
}
try {
$compressor = new PdfCompress($pdfApi, $configParams);
$compressor->uploadDocument();
$compressor->compressPdfDocument();
$compressor->downloadResult();
} catch (Exception $e) {
echo "Error: " . $e->getMessage() . PHP_EOL;
}
使用我们的 PHP 库您可以:
使用 Aspose.PDF Cloud PHP SDK 压缩 PDF 文档。
- 以文本或图像格式添加 PDF 文档的页眉和页脚。
- 向 PDF 文档添加表格和印章(文本或图像)。
- 将多个 PDF 文档附加到现有文件中。
- 处理 PDF 附件、注释和表单字段。
- 对 PDF 文档应用加密或解密并设置密码。
- 从页面或整个 PDF 文档中删除所有印章和表格。
- 按 ID 从 PDF 文档中删除特定印章或表格。
- 替换 PDF 页面或整个文档中的单个或多个文本实例。
- 广泛支持将 PDF 文档转换为各种其他文件格式。
- 提取 PDF 文件的各种元素并优化 PDF 文档。
- 您可以尝试我们的 免费应用 在线测试功能。