fop-cli 0.1.1

Command-line interface for Apache FOP - XSL-FO to PDF converter
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
# FOP CLI - Apache FOP Command-Line Interface

[![Crates.io](https://img.shields.io/crates/v/fop-cli.svg)](https://crates.io/crates/fop-cli)
[![Documentation](https://docs.rs/fop-cli/badge.svg)](https://docs.rs/fop-cli)
[![License](https://img.shields.io/crates/l/fop-cli.svg)](LICENSE)

A high-performance command-line tool for converting XSL-FO documents to multiple output formats including PDF, SVG, PostScript, PNG, JPEG, and plain text.

Part of the [COOLJAPAN FOP](https://github.com/cool-japan/fop) ecosystem — an Apache FOP-compatible pure-Rust implementation.

## Features

- **Fast conversion** — Rust-based implementation for optimal performance
- **Multiple output formats** — PDF, SVG, PostScript, PNG, JPEG, and plain text
- **Progress reporting** — Real-time progress bars for large documents
- **Statistics** — Detailed processing metrics and timing (text or JSON)
- **Validation** — Validate XSL-FO documents without generating output
- **PDF encryption** — Owner/user password protection and permission control
- **Flexible options** — Support for custom fonts, images, and PDF metadata
- **Apache FOP-compatible** — Drop-in replacement CLI interface

## Installation

### From Source

```bash
cargo install --path crates/fop-cli
```

### From Workspace

```bash
cargo build --release --package fop-cli
cp target/release/fop /usr/local/bin/
```

## Usage

### Basic Conversion

```bash
fop input.fo output.pdf
```

### Output to Different Formats

```bash
# PDF (default)
fop input.fo output.pdf

# SVG
fop input.fo output.svg

# PostScript
fop input.fo output.ps

# PNG
fop input.fo output.png

# JPEG
fop input.fo output.jpg

# Plain text
fop input.fo output.txt
```

### With Progress and Statistics

```bash
fop input.fo output.pdf --stats
```

### Validation Only

```bash
fop input.fo --validate-only
```

### Verbose Output

```bash
fop input.fo output.pdf --verbose
```

### Custom Resources

```bash
fop input.fo output.pdf \
    --images-dir ./images \
    --font-dir ./fonts
```

### PDF Metadata

```bash
fop input.fo output.pdf \
    --title "My Document" \
    --author "John Doe" \
    --subject "Technical Report" \
    --keywords "fop, pdf, xsl-fo"
```

### PDF Encryption

```bash
# Set owner and user passwords
fop input.fo output.pdf \
    -o "owner-secret" \
    -u "user-secret"

# Restrict permissions
fop input.fo output.pdf \
    -o "owner-secret" \
    --noprint \
    --nocopy \
    --noedit \
    --noannotations
```

### Advanced Options

```bash
fop input.fo output.pdf \
    --compress \
    --pdf-version 1.7 \
    --jobs 4 \
    --strict
```

## Command-Line Options

### Input/Output

- `<INPUT>` — Input XSL-FO file (required)
- `[OUTPUT]` — Output file (optional with `--validate-only`)

### Processing Options

- `-v, --verbose` — Enable verbose logging
- `--stats` — Show detailed statistics after conversion
- `--validate-only` — Only validate the XSL-FO document
- `-q, --quiet` — Suppress progress bars and animations
- `--no-progress` — Disable progress reporting (for scripting)

### Resources

- `--images-dir <DIR>` — Directory containing images
- `--font-dir <DIR>` — Directory containing fonts

### PDF Options

- `--compress` — Enable PDF compression (reduces file size)
- `--pdf-version <VERSION>` — PDF version (1.4, 1.5, 1.6, 1.7, 2.0) [default: 1.4]
- `--author <AUTHOR>` — Set PDF author metadata
- `--title <TITLE>` — Set PDF title metadata
- `--subject <SUBJECT>` — Set PDF subject metadata
- `--keywords <KEYWORDS>` — Set PDF keywords metadata

### Encryption Options

- `-o, --owner-password <PASSWORD>` — Set PDF owner password (full access)
- `-u, --user-password <PASSWORD>` — Set PDF user password (restricted access)
- `--noprint` — Disallow printing
- `--nocopy` — Disallow copying content
- `--noedit` — Disallow editing
- `--noannotations` — Disallow adding annotations

### Performance

- `-j, --jobs <N>` — Number of threads to use (default: auto-detect)
- `--max-memory <MB>` — Maximum memory usage in MB

### Validation

- `--strict` — Enable strict XSL-FO validation
- `--fail-fast` — Stop processing after first error

### Output Format

- `--output-format <FORMAT>` — Output format for validation results (text, json) [default: text]

## Examples

### Basic Document Conversion

```bash
# Simple conversion
fop document.fo document.pdf

# With progress and stats
fop document.fo document.pdf --stats
```

### Large Document Processing

```bash
# Show progress for large documents
fop large-report.fo large-report.pdf --verbose --stats

# Use multiple threads
fop large-report.fo large-report.pdf -j 8
```

### Validation Workflow

```bash
# Validate before generating
fop document.fo --validate-only

# If valid, generate PDF
fop document.fo document.pdf
```

### Scripting Integration

```bash
# Disable progress bars for scripts
fop document.fo document.pdf --quiet --stats --output-format json > stats.json

# Exit code indicates success (0) or failure (non-zero)
if fop document.fo document.pdf --quiet; then
    echo "Success"
else
    echo "Failed"
fi
```

### Production Deployment

```bash
# Compressed PDF with metadata and encryption
fop report.fo report.pdf \
    --compress \
    --pdf-version 1.7 \
    --title "Annual Report 2024" \
    --author "Acme Corporation" \
    --subject "Financial Report" \
    --keywords "finance, annual, 2024" \
    -o "owner-secret" \
    -u "reader-pass" \
    --noprint --nocopy
```

### Multi-Format Output

```bash
# Generate multiple formats from the same source
fop document.fo document.pdf
fop document.fo document.svg
fop document.fo document.ps
fop document.fo document.txt
```

## Output

### Progress Output

```
Apache FOP
Rust Implementation

✓ Read input file: document.fo (5ms)
✓ Parsed FO tree (1243 nodes) (245ms)
✓ Layout complete (3456 areas) (1.2s)
✓ PDF rendered (42 pages) (890ms)
✓ Saved to document.pdf (15ms)

✓ Success!

  Input:   document.fo
  Output:  document.pdf
  Size:    234.5 KB → 1.2 MB
  Pages:   42
  Time:    2.35s
```

### Statistics Output (Text)

```
Processing Statistics:

  ⚙️ Parsing:      245ms
  ⚙️ Layout:       1.20s
  ⚙️ Rendering:    890ms
  ✨ Total:        2.35s

  • FO Nodes:     1243
  • Areas:        3456
  📄 Pages:        42

  Input size:    234.5 KB
  Output size:   1.2 MB
  Size ratio:    5.12x

  Throughput:    17.87 pages/sec
```

### Statistics Output (JSON)

```json
{
  "parsing": {
    "duration_ms": 245,
    "nodes": 1243
  },
  "layout": {
    "duration_ms": 1200,
    "areas": 3456
  },
  "rendering": {
    "duration_ms": 890,
    "pages": 42
  },
  "total": {
    "duration_ms": 2350,
    "input_bytes": 240128,
    "output_bytes": 1258291,
    "warnings": 0,
    "errors": 0
  }
}
```

## Performance Tips

### For Large Documents

1. **Use multiple threads**: `-j 8` (adjust based on CPU cores)
2. **Enable compression**: `--compress` (reduces output size)
3. **Suppress progress**: `--quiet` (slight performance gain)

### For Batch Processing

```bash
#!/bin/bash
for fo_file in *.fo; do
    pdf_file="${fo_file%.fo}.pdf"
    fop "$fo_file" "$pdf_file" --quiet --compress
done
```

### For CI/CD Pipelines

```yaml
# Example GitHub Actions workflow
- name: Generate PDF
  run: |
    fop document.fo document.pdf \
      --quiet \
      --fail-fast \
      --stats \
      --output-format json > stats.json
```

## Error Handling

The CLI returns different exit codes:

- `0` — Success
- `1` — Error (parsing, layout, rendering, or I/O)

Errors are printed to stderr with detailed context:

```
Error: Failed to parse XSL-FO document

Caused by:
  0: Unexpected element: fo:invalid-element
  1: Line 42, column 5
```

## Logging

Set `RUST_LOG` environment variable for fine-grained control:

```bash
# Debug all FOP modules
RUST_LOG=debug fop document.fo document.pdf

# Only show errors
RUST_LOG=error fop document.fo document.pdf

# Module-specific logging
RUST_LOG=fop_core=debug,fop_layout=info fop document.fo document.pdf
```

## Building

```bash
# Debug build
cargo build --package fop-cli

# Release build (optimized)
cargo build --release --package fop-cli

# Run tests
cargo test --package fop-cli

# Run clippy
cargo clippy --package fop-cli -- -D warnings
```

## Dependencies

| Crate | Version | Purpose |
|-------|---------|---------|
| `fop-core` | workspace | FO tree parsing and processing |
| `fop-layout` | workspace | Page layout engine |
| `fop-render` | workspace | Multi-format rendering |
| `fop-types` | workspace | Shared types and data structures |
| `clap` | 4.5 | Command-line argument parsing |
| `anyhow` | 1.0 | Error handling |
| `indicatif` | 0.18 | Progress bars |
| `console` | 0.16 | Terminal styling |
| `humantime` | 2.3 | Human-readable durations |
| `bytesize` | 2.3 | Human-readable byte sizes |
| `serde_json` | 1.0 | JSON statistics output |
| `env_logger` | 0.11 | Logging backend |
| `log` | 0.4 | Logging facade |

## Related Crates

| Crate | Description |
|-------|-------------|
| [`fop-core`]https://crates.io/crates/fop-core | XSL-FO tree parsing and validation |
| [`fop-layout`]https://crates.io/crates/fop-layout | Page layout and area tree generation |
| [`fop-render`]https://crates.io/crates/fop-render | Multi-format rendering (PDF, SVG, PS, PNG, JPEG, Text) |
| [`fop-types`]https://crates.io/crates/fop-types | Shared FO types, properties, and data structures |
| [`fop-pdf-renderer`]https://crates.io/crates/fop-pdf-renderer | Low-level PDF generation with encryption support |
| [`fop-wasm`]https://crates.io/crates/fop-wasm | WebAssembly bindings for browser usage |
| [`fop-python`]https://crates.io/crates/fop-python | Python bindings via PyO3 |

## See Also

- [FOP Repository]https://github.com/cool-japan/fop — Main project repository
- [FOP Core]../fop-core/README.md — FO tree parsing
- [FOP Layout]../fop-layout/README.md — Layout engine
- [FOP Render]../fop-render/README.md — Multi-format rendering
- [FOP Types]../fop-types/README.md — Shared types
- [FOP PDF Renderer]../fop-pdf-renderer/README.md — PDF generation and encryption
- [CLI Usage Guide]USAGE.md — Detailed usage documentation

## License

Apache License 2.0

Copyright 2024-2026 COOLJAPAN OU (Team Kitasan)

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

<http://www.apache.org/licenses/LICENSE-2.0>

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.