Skip to content

Commit 6d468ce

Browse files
authored
Merge pull request #57 from aspose-pdf-cloud/php
Add PHP SDK documentation for compressing PDF
2 parents 9a3e3d8 + 2ec769b commit 6d468ce

File tree

1 file changed

+213
-0
lines changed

1 file changed

+213
-0
lines changed

pdf/en/php/compress/_index.md

Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
---
2+
title: Compress PDF via Cloud PHP SDK
3+
url: /php/compress/
4+
description: Aspose.PDF Cloud allows you to optimize PDF Document. Check the PHP source code to compress PDF file.
5+
lastmod: "2025-05-19"
6+
---
7+
8+
{{< blocks/products/pf/main-wrap-class isAutogenPage="true">}}
9+
{{< blocks/products/pf/upper-banner h1="Compress PDF in PHP SDK" h2="Optimize PDF Document using Cloud PHP SDK" logoImageSrc="/sdk/aspose_pdf-for-php.svg" sourceAdditionalConversionTag="" additionalConversionTag="" pfName="Aspose.PDF" subTitlepfName="for PHP" downloadUrl="" fileiconsmall1="PNG" fileiconsmall2="JPG" fileiconsmall3="BMP" fileiconsmall4="TIFF" fileiconsmall5="PDF" >}}
10+
11+
{{< blocks/products/pf/main-container pfName="Aspose.PDF Cloud " subTitlepfName="SDK for PHP" >}}
12+
{{< blocks/products/pf/sub-menu autoGeneratedVersion="true" logoImageSrc="/sdk/aspose_pdf-for-php.svg" apiHomeLink="" codeSamplesLink="https://github.com/aspose-pdf-cloud" liveDemosLink="https://products.aspose.cloud/pdf/" docsLink="https://docs.aspose.cloud/pdf/" installationsDocsLink="https://docs.aspose.cloud/pdf/" nugetLink="https://www.nuget.org/packages/Aspose.Pdf-Cloud" nugetPackageName="" downloadAsLink="https://releases.aspose.cloud/pdf/php/" learnAsLink="https://docs.aspose.cloud/pdf/" apiReference="https://reference.aspose.cloud/pdf/" mavenRepoLink="" >}}
13+
14+
{{% blocks/products/pf/agp/content h2="How to crompress PDF via Cloud PHP SDK" %}}
15+
16+
To crompress PDF, we'll use
17+
[Aspose.PDF Cloud PHP SDK](https://products.aspose.cloud/pdf/php/)
18+
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](https://dashboard.aspose.cloud/#/apps) and get your application information. Once you have the App SID & key, you are ready to give the Aspose.PDF Cloud PHP SDK. This method leverages the Aspose.PDF Cloud SDK for PHP to optimize PDF documents efficiently. By automating the upload, optimization, and download processes, it simplifies the workflow for developers. The use of various optimization options ensures that the resulting PDF is smaller in size without compromising quality.
19+
20+
{{% blocks/products/pf/agp/code-block title="Package Manager Console Command" offSpacer="true" %}}
21+
22+
```bash
23+
 
24+
composer install
25+
26+
```
27+
28+
{{% /blocks/products/pf/agp/code-block %}}
29+
30+
{{% /blocks/products/pf/agp/content %}}
31+
32+
{{< blocks/products/pf/agp/feature-section isGrey="true" >}}
33+
34+
{{% blocks/products/pf/agp/feature-section-col title="Steps to compress PDF via Cloud PHP" %}}
35+
36+
{{% blocks/products/pf/agp/text %}}
37+
38+
Aspose.PDF Cloud developers can easily load & optimize PDF in just a few lines of code.
39+
40+
{{% /blocks/products/pf/agp/text %}}
41+
42+
1. Upload a PDF Document
43+
1. Compress the PDF Document
44+
1. Download the Optimized PDF Document
45+
46+
{{% /blocks/products/pf/agp/feature-section-col %}}
47+
48+
{{% blocks/products/pf/agp/code-block title="Compress PDF using PHP" offSpacer="" %}}
49+
50+
```php
51+
52+
require __DIR__.'vendor\autoload.php';
53+
54+
use Aspose\PDF\Configuration;
55+
use Aspose\PDF\Api\PdfApi;
56+
57+
$credentials = json_decode(file_get_contents(__DIR__ . 'credentials.json'), true);
58+
59+
$configParams = [
60+
"LOCAL_FOLDER" => "C:\\Samples\\",
61+
"PDF_DOCUMENT_NAME" => "sample.pdf",
62+
"TEMP_FOLDER" => "TempPdfCloud",
63+
"LOCAL_RESULT_DOCUMENT_NAME" => "output_sample.pdf",
64+
];
65+
66+
class PdfCompress
67+
{
68+
private $pdfApi;
69+
private $configParams;
70+
71+
private function _create_rest_api() {
72+
$credentials = json_decode(file_get_contents("credentials.json"), true);
73+
74+
$configAuth = new Configuration();
75+
$configAuth->setAppKey($credentials['key']);
76+
$configAuth->setAppSid($credentials['id']);
77+
78+
$this->pdfApi = new PdfApi(null, $configAuth);
79+
}
80+
81+
public function __construct($config) {
82+
$this->configParams = $config;
83+
$this->_create_rest_api();
84+
}
85+
86+
public function uploadDocument()
87+
{
88+
$filePath = $this->configParams["LOCAL_FOLDER"] . DIRECTORY_SEPARATOR . $this->configParams["PDF_DOCUMENT_NAME"];
89+
$fileData = file_get_contents($filePath);
90+
91+
$storagePath = $this->configParams["TEMP_FOLDER"] . DIRECTORY_SEPARATOR . $this->configParams["PDF_DOCUMENT_NAME"];
92+
93+
$this->pdfApi->uploadFile($storagePath, $fileData);
94+
echo "File: '{$this->configParams["PDF_DOCUMENT_NAME"]}' successfully uploaded." . PHP_EOL;
95+
}
96+
97+
public function downloadResult()
98+
{
99+
$fileName = $this->configParams["TEMP_FOLDER"] . DIRECTORY_SEPARATOR . $this->configParams["PDF_DOCUMENT_NAME"];
100+
$downloaded = $this->pdfApi->downloadFile($fileName);
101+
102+
$filePath = $this->configParams["LOCAL_FOLDER"] . DIRECTORY_SEPARATOR . $this->configParams["LOCAL_RESULT_DOCUMENT_NAME"];
103+
file_put_contents($filePath, $downloaded);
104+
echo "Downloaded: {$filePath}" . PHP_EOL;
105+
}
106+
107+
public function compressPdfDocument()
108+
{
109+
$options = new \Aspose\PDF\Model\OptimizeOptions();
110+
$options->setAllowReusePageContent(true);
111+
$options->setCompressImages(true);
112+
$options->setImageQuality(100);
113+
$options->setLinkDuplcateStreams(true);
114+
$options->setRemoveUnusedObjects(true);
115+
$options->setRemoveUnusedStreams(true);
116+
$options->setUnembedFonts(true);
117+
118+
$response = $this->pdfApi->postOptimizeDocument(
119+
$this->configParams["PDF_DOCUMENT_NAME"],
120+
$options,
121+
null,
122+
$this->configParams["TEMP_FOLDER"]
123+
);
124+
125+
if ($response->getCode() != 200) {
126+
echo "compressPdfDocument(): Failed to compress the PDF document!" . PHP_EOL;
127+
} else {
128+
echo "compressPdfDocument(): Successfully compressed the PDF document '{$this->configParams["PDF_DOCUMENT_NAME"]}' !" . PHP_EOL;
129+
}
130+
}
131+
}
132+
133+
try {
134+
$compressor = new PdfCompress($pdfApi, $configParams);
135+
$compressor->uploadDocument();
136+
$compressor->compressPdfDocument();
137+
$compressor->downloadResult();
138+
} catch (Exception $e) {
139+
echo "Error: " . $e->getMessage() . PHP_EOL;
140+
}
141+
```
142+
143+
{{% /blocks/products/pf/agp/code-block %}}
144+
145+
{{% blocks/products/pf/agp/content h2="With our PHP library you can:" %}}
146+
147+
Compress PDF documents with [Aspose.PDF Cloud PHP SDK](https://products.aspose.cloud/pdf/php/).
148+
149+
+ Add PDF document's header & footer in text or image format.
150+
+ Add tables & stamps (text or image) to PDF documents.
151+
+ Append multiple PDF documents to an existing file.
152+
+ Work with PDF attachments, annotations, & form fields.
153+
+ Apply encryption or decryption to PDF documents & set a password.
154+
+ Delete all stamps & tables from a page or entire PDF document.
155+
+ Delete a specific stamp or table from the PDF document by its ID.
156+
+ Replace single or multiple instances of text on a PDF page or from the entire document.
157+
+ Extensive support for converting PDF documents to various other file formats.
158+
+ Extract various elements of PDF files & make PDF documents optimized.
159+
+ You can try out our [free App](https://products.aspose.app/pdf/family) to test the functionality online.
160+
161+
{{% /blocks/products/pf/agp/content %}}
162+
163+
{{< /blocks/products/pf/agp/feature-section >}}
164+
165+
{{< blocks/products/pf/support-learning-resources >}}
166+
{{< blocks/products/pf/slr-tab tabTitle="Learning Resources" tabId="resources" >}}
167+
{{< blocks/products/pf/slr-element name="Documentation" href="https://docs.aspose.cloud/pdf" >}}
168+
{{< blocks/products/pf/slr-element name="Source Code" href="https://github.com/aspose-pdf-cloud/aspose-pdf-cloud-php" >}}
169+
{{< blocks/products/pf/slr-element name="API References" href="https://reference.aspose.cloud/pdf/" >}}
170+
{{< /blocks/products/pf/slr-tab >}}
171+
172+
{{< blocks/products/pf/slr-tab tabTitle="Product Support" tabId="support" >}}
173+
{{< blocks/products/pf/slr-element name="Free Support" href="https://forum.aspose.cloud/c/pdf/13" >}}
174+
{{< blocks/products/pf/slr-element name="Paid Support" href="https://helpdesk.aspose.cloud" >}}
175+
{{< blocks/products/pf/slr-element name="Blog" href="https://blog.aspose.cloud/categories/aspose.pdf-cloud-product-family/" >}}
176+
{{< /blocks/products/pf/slr-tab >}}
177+
178+
{{< blocks/products/pf/slr-tab tabTitle="Why Aspose.PDF Cloud for PHP?" tabId="success-stories" >}}
179+
{{< blocks/products/pf/slr-element name="Customers List" href="https://company.aspose.cloud/customers" >}}
180+
{{< blocks/products/pf/slr-element name="Security" href="https://company.aspose.cloud/legal/security" >}}
181+
{{< /blocks/products/pf/slr-tab >}}
182+
183+
{{< /blocks/products/pf/support-learning-resources >}}
184+
185+
{{< blocks/products/pf/offers-section pfName="Aspose.PDF" >}}
186+
187+
{{< blocks/products/pf/offers-section-item link="/pdf/curl/" imgSrc="/sdk/aspose_pdf-for-curl.svg" platform="cURL" >}}
188+
189+
{{< blocks/products/pf/offers-section-item link="/pdf/net/" imgSrc="/sdk/aspose_pdf-for-net.svg" platform=".NET" >}}
190+
191+
{{< blocks/products/pf/offers-section-item link="/pdf/java/" imgSrc="/sdk/aspose_pdf-for-java.svg" platform="Java" >}}
192+
193+
{{< blocks/products/pf/offers-section-item link="/pdf/php/" imgSrc="/sdk/aspose_pdf-for-php.svg" platform="PHP" >}}
194+
195+
{{< blocks/products/pf/offers-section-item link="/pdf/android/" imgSrc="/sdk/aspose_pdf-for-android.svg" platform="Android" >}}
196+
197+
{{< blocks/products/pf/offers-section-item link="/pdf/python/" imgSrc="/sdk/aspose_pdf-for-python.svg" platform="Python" >}}
198+
199+
{{< blocks/products/pf/offers-section-item link="/pdf/ruby/" imgSrc="/sdk/aspose_pdf-for-ruby.svg" platform="Ruby" >}}
200+
201+
{{< blocks/products/pf/offers-section-item link="/pdf/nodejs/" imgSrc="/sdk/aspose_pdf-for-node.svg" platform="Node.js" >}}
202+
203+
{{< blocks/products/pf/offers-section-item link="/pdf/swift/" imgSrc="/sdk/aspose_pdf-for-swift.svg" platform="Swift" >}}
204+
205+
{{< blocks/products/pf/offers-section-item link="/pdf/go/" imgSrc="/sdk/aspose_pdf-for-go.svg" platform="Go" >}}
206+
207+
{{< /blocks/products/pf/offers-section >}}
208+
209+
<!-- aboutfile Ends -->
210+
211+
{{< /blocks/products/pf/main-container >}}
212+
213+
{{< /blocks/products/pf/main-wrap-class >}}

0 commit comments

Comments
 (0)