Skip to content

Commit 6e624b5

Browse files
authored
Merge pull request #14 from LibreSign/feat/standalone-pdf-preview-export
feat: add standalone PDF preview export
2 parents 55d0193 + 5ea4b93 commit 6e624b5

7 files changed

Lines changed: 1284 additions & 0 deletions

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Use the compiler to generate a reusable XObject result. Consumers that prefer ar
2020
```php
2121
use LibreSign\XObjectTemplate\Dto\CompileRequest;
2222
use LibreSign\XObjectTemplate\Integration\XObjectPayloadAdapter;
23+
use LibreSign\XObjectTemplate\Pdf\SinglePagePdfExporter;
2324
use LibreSign\XObjectTemplate\XObjectTemplateCompiler;
2425

2526
$compiler = new XObjectTemplateCompiler();
@@ -32,15 +33,27 @@ $result = $compiler->compile(new CompileRequest(
3233
));
3334

3435
$payload = (new XObjectPayloadAdapter())->toXObjectPayload($result);
36+
$pdf = (new SinglePagePdfExporter())->export($result);
37+
38+
file_put_contents(__DIR__ . '/build/preview.pdf', $pdf);
3539
```
3640

41+
### Standalone PDF export
42+
43+
`SinglePagePdfExporter` wraps a compiled XObject result into a one-page PDF whose `MediaBox` matches the compiled `bbox` size exactly.
44+
45+
- The page size is derived from `$result->bbox`
46+
- Non-zero bounding boxes are translated back to the page origin automatically
47+
- Local PNG and JPEG image sources are embedded into the standalone PDF during export
48+
3749
### Output contract
3850

3951
- `$result->contentStream`: PDF operators ready for a Form XObject stream
4052
- `$result->resources`: font/image resource dictionary keyed for PDF serialization
4153
- `$result->bbox`: bounding box as `[x1, y1, x2, y2]`
4254
- `$result->metadata`: render diagnostics such as `line_count`, `image_count`, `node_count`, and `render_ms`
4355
- `$payload`: transport-agnostic array with `stream`, `resources`, and `bbox`
56+
- `$pdf`: standalone PDF bytes ready to save, stream, or attach to preview workflows
4457

4558
## Supported HTML/CSS subset
4659

src/Pdf/EmbeddedPdfImage.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
// SPDX-FileCopyrightText: 2026 LibreSign
4+
// SPDX-License-Identifier: AGPL-3.0-or-later
5+
6+
declare(strict_types=1);
7+
8+
namespace LibreSign\XObjectTemplate\Pdf;
9+
10+
final readonly class EmbeddedPdfImage
11+
{
12+
/**
13+
* @param array<string, mixed> $dictionary
14+
*/
15+
public function __construct(
16+
public array $dictionary,
17+
public string $stream,
18+
public ?self $softMask = null,
19+
) {
20+
}
21+
}

0 commit comments

Comments
 (0)