Skip to content

Commit 312c8bb

Browse files
authored
Compress and uncompress operations (#4)
1 parent e7d7288 commit 312c8bb

4 files changed

Lines changed: 91 additions & 7 deletions

File tree

‎src/Pdftk.php‎

Lines changed: 61 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ public function __construct(
2727
*/
2828
public function fillForm(string $pdfFilePath, string $formDataFilePath, bool $flatten = false): string
2929
{
30-
$executablePath = $this->executablePath ?? $this->findExecutablePath();
30+
$executablePath = $this->getExecutablePath();
3131

3232
$command = [$executablePath, $pdfFilePath, 'fill_form', $formDataFilePath, 'output', '-'];
3333

@@ -55,7 +55,7 @@ public function fillForm(string $pdfFilePath, string $formDataFilePath, bool $fl
5555
*/
5656
public function dumpDataFields(string $pdfFilePath, bool $utf8 = true): array
5757
{
58-
$executablePath = $this->executablePath ?? $this->findExecutablePath();
58+
$executablePath = $this->getExecutablePath();
5959

6060
$operation = $utf8 ? 'dump_data_fields_utf8' : 'dump_data_fields';
6161

@@ -143,7 +143,7 @@ public function dumpDataFields(string $pdfFilePath, bool $utf8 = true): array
143143
*/
144144
public function cat(string ...$pdfFilePaths): string
145145
{
146-
$executablePath = $this->executablePath ?? $this->findExecutablePath();
146+
$executablePath = $this->getExecutablePath();
147147

148148
$command = [$executablePath];
149149

@@ -175,7 +175,7 @@ public function cat(string ...$pdfFilePaths): string
175175
*/
176176
public function dumpData(string $pdfFilePath, bool $utf8 = true): Report
177177
{
178-
$executablePath = $this->executablePath ?? $this->findExecutablePath();
178+
$executablePath = $this->getExecutablePath();
179179

180180
$operation = $utf8 ? 'dump_data_utf8' : 'dump_data';
181181

@@ -317,7 +317,7 @@ public function dumpData(string $pdfFilePath, bool $utf8 = true): Report
317317
*/
318318
public function generateFdf(string $pdfFilePath): string
319319
{
320-
$executablePath = $this->executablePath ?? $this->findExecutablePath();
320+
$executablePath = $this->getExecutablePath();
321321

322322
$command = [$executablePath, $pdfFilePath, 'generate_fdf', 'output', '-'];
323323

@@ -342,7 +342,7 @@ public function generateFdf(string $pdfFilePath): string
342342
*/
343343
public function burst(string $pdfFilePath, string $outputDir = null, string $pageNamePrefixOutput = 'page_'): void
344344
{
345-
$executablePath = $this->executablePath ?? $this->findExecutablePath();
345+
$executablePath = $this->getExecutablePath();
346346

347347
$outputDir ??= sys_get_temp_dir();
348348

@@ -356,8 +356,62 @@ public function burst(string $pdfFilePath, string $outputDir = null, string $pag
356356
}
357357
}
358358

359-
private function findExecutablePath(): string
359+
/**
360+
* Removes compression from a PDF file to create an uncompressed version.
361+
*
362+
* @param string $pdfFilePath Filepath to the input PDF file to be uncompressed.
363+
*
364+
* @return string Uncompressed PDF content.
365+
*/
366+
public function uncompress(string $pdfFilePath): string
360367
{
368+
$executablePath = $this->getExecutablePath();
369+
370+
$command = [$executablePath, $pdfFilePath, 'output', '-', 'uncompress'];
371+
;
372+
373+
$process = new Process($command);
374+
$process->run();
375+
376+
if (false === $process->isSuccessful()) {
377+
throw new ProcessFailedException($process);
378+
}
379+
380+
return $process->getOutput();
381+
}
382+
383+
/**
384+
* Compresses the specified PDF file and returns the compressed output.
385+
*
386+
* @param string $pdfFilePath The file path of the PDF to be compressed.
387+
*
388+
* @return string The compressed content of the PDF file.
389+
*
390+
* @throws ProcessFailedException If the compression process fails.
391+
*/
392+
public function compress(string $pdfFilePath): string
393+
{
394+
$executablePath = $this->getExecutablePath();
395+
396+
$command = [$executablePath, $pdfFilePath, 'output', '-', 'compress'];
397+
;
398+
399+
$process = new Process($command);
400+
$process->run();
401+
402+
if (false === $process->isSuccessful()) {
403+
throw new ProcessFailedException($process);
404+
}
405+
406+
return $process->getOutput();
407+
}
408+
409+
private function getExecutablePath(): string
410+
{
411+
if (null !== $this->executablePath) {
412+
return $this->executablePath;
413+
}
414+
361415
$executableFinder = new ExecutableFinder();
362416
$executablePath = $executableFinder->find('pdftk');
363417

‎tests/PdftkTest.php‎

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,4 +121,34 @@ public function testBurst(): void
121121
$this->assertFileExists('build/page_02.pdf');
122122
$this->assertFileExists('build/page_03.pdf');
123123
}
124+
125+
public function testUncompress(): void
126+
{
127+
// Arrange
128+
$pdftk = new Pdftk();
129+
130+
// Act
131+
$uncompressed = $pdftk->uncompress(__DIR__ . '/data/sample.pdf');
132+
133+
// Assert
134+
$this->assertStringEqualsFile(
135+
__DIR__ . '/data/sample_uncompressed.pdf',
136+
$uncompressed,
137+
);
138+
}
139+
140+
public function testCompress(): void
141+
{
142+
// Arrange
143+
$pdftk = new Pdftk();
144+
145+
// Act
146+
$compressed = $pdftk->compress(__DIR__ . '/data/sample.pdf');
147+
148+
// Assert
149+
$this->assertStringEqualsFile(
150+
__DIR__ . '/data/sample_compressed.pdf',
151+
$compressed,
152+
);
153+
}
124154
}

‎tests/data/sample_compressed.pdf‎

12.9 KB
Binary file not shown.

‎tests/data/sample_uncompressed.pdf‎

25.2 KB
Binary file not shown.

0 commit comments

Comments
 (0)