Skip to content

Commit 26aca9e

Browse files
committed
Add simple profile script that parses a pdf file without using PHPUnit and Composer
1 parent 3f87cc7 commit 26aca9e

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

CONTRIBUTING.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ It contains php-spx for easier profiling of memory/cpu usage, which can be enabl
1313

1414
To profile how a specific PDF is being parsed, you can run the parser with SPX_ENABLED=1 for a specific sample from the Samples test suite. For example, to debug the file in `tests/Samples/files/gdocs-image-simple/file.pdf`, you can run the following command:
1515
```bash
16-
docker compose exec -e SPX_ENABLED=1 php vendor/bin/phpunit tests/Samples/ --filter "gdocs-image-simple"
16+
docker compose exec -e SPX_ENABLED=1 php php tests/Samples/profile.php gdocs-image-simple getText
1717
```
1818

1919
If you want a more detailed profile, you can run the following command:
2020

2121
```bash
22-
docker compose exec -e SPX_ENABLED=1 -e SPX_REPORT=full php vendor/bin/phpunit tests/Samples/ --filter "gdocs-image-simple"
22+
docker compose exec -e SPX_ENABLED=1 -e SPX_REPORT=full php php tests/Samples/profile.php gdocs-image-simple getText
2323
```
2424

2525
you can then head to http://localhost, scroll down to the last run and click on it to view the full profile.

tests/Samples/profile.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php declare(strict_types=1);
2+
3+
require_once dirname(__DIR__, 2) . '/.al-custom.php';
4+
5+
$sampleName = $argv[1] ?? throw new InvalidArgumentException('Missing sample name');
6+
if (preg_match('/^[a-zA-Z0-9_-]+$/', $sampleName) !== 1) {
7+
throw new InvalidArgumentException('Invalid sample name. Only alphanumeric characters, hyphens, and underscores are allowed.');
8+
}
9+
10+
$document = (new \PrinsFrank\PdfParser\PdfParser())
11+
->parseFile(__DIR__ . '/files/' . $sampleName . '/file.pdf');
12+
13+
match ($argv[2] ?? throw new InvalidArgumentException('Missing profile type')) {
14+
'getText' => $document->getText(),
15+
'getImages' => array_map(fn(\PrinsFrank\PdfParser\Document\Object\Decorator\XObject $image) => $image->getStream()->toString(), $document->getImages()),
16+
default => throw new InvalidArgumentException('Please provide a profile type'),
17+
};

0 commit comments

Comments
 (0)