fop-cli 0.1.0

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
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
# FOP CLI - Complete Usage Guide

## Quick Start

### Installation

```bash
# Build from source
cargo build --release --package fop-cli

# Install to system
sudo cp target/release/fop /usr/local/bin/

# Or install to user bin
cp target/release/fop ~/.local/bin/
```

### First Conversion

```bash
# Create a simple XSL-FO file
cat > hello.fo <<'EOF'
<?xml version="1.0"?>
<fo:root xmlns:fo="http://www.w3.org/1999/XSL/Format">
  <fo:layout-master-set>
    <fo:simple-page-master master-name="A4" page-width="210mm" page-height="297mm">
      <fo:region-body margin="1in"/>
    </fo:simple-page-master>
  </fo:layout-master-set>
  <fo:page-sequence master-reference="A4">
    <fo:flow flow-name="xsl-region-body">
      <fo:block font-size="18pt">Hello, World!</fo:block>
    </fo:flow>
  </fo:page-sequence>
</fo:root>
EOF

# Convert to PDF
fop hello.fo hello.pdf

# View result
xdg-open hello.pdf
```

## Common Use Cases

### 1. Simple Document Conversion

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

# With progress reporting
fop document.fo document.pdf --verbose

# Quiet mode (for scripts)
fop document.fo document.pdf --quiet
```

### 2. Document Validation

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

# Strict validation
fop document.fo --validate-only --strict
```

### 3. Performance Optimization

```bash
# Enable compression
fop document.fo document.pdf --compress

# Use multiple threads
fop document.fo document.pdf -j 8

# Both
fop document.fo document.pdf --compress -j 8
```

### 4. Metadata and PDF Options

```bash
# Add PDF metadata
fop document.fo document.pdf \
  --title "Annual Report 2024" \
  --author "Acme Corp" \
  --subject "Financial Report" \
  --keywords "finance, annual, 2024"

# Specify PDF version
fop document.fo document.pdf --pdf-version 1.7

# Complete metadata
fop document.fo document.pdf \
  --pdf-version 1.7 \
  --compress \
  --title "Technical Manual" \
  --author "Engineering Team" \
  --subject "Product Documentation" \
  --keywords "manual, technical, v2.0"
```

### 5. Statistics and Monitoring

```bash
# Show text statistics
fop document.fo document.pdf --stats

# JSON output for monitoring
fop document.fo document.pdf --stats --output-format json

# Redirect JSON to file
fop document.fo document.pdf --stats --output-format json > stats.json
```

### 6. Custom Resources

```bash
# Specify images directory
fop document.fo document.pdf --images-dir ./images

# Specify fonts directory
fop document.fo document.pdf --font-dir ./fonts

# Both
fop document.fo document.pdf \
  --images-dir ./assets/images \
  --font-dir ./assets/fonts
```

## Batch Processing

### Simple Batch Script

```bash
#!/bin/bash
# Convert all .fo files in a directory

for fo_file in *.fo; do
  pdf_file="${fo_file%.fo}.pdf"
  echo "Converting $fo_file to $pdf_file..."
  fop "$fo_file" "$pdf_file" --quiet --compress
done

echo "Batch conversion complete!"
```

### Parallel Processing

```bash
#!/bin/bash
# Process multiple files in parallel using GNU parallel

parallel fop {} {.}.pdf --quiet --compress ::: *.fo
```

### With Error Handling

```bash
#!/bin/bash
# Batch processing with error handling

SUCCESS=0
FAILED=0

for fo_file in *.fo; do
  pdf_file="${fo_file%.fo}.pdf"

  if fop "$fo_file" "$pdf_file" --quiet --compress; then
    echo "✓ $fo_file → $pdf_file"
    SUCCESS=$((SUCCESS + 1))
  else
    echo "✗ Failed: $fo_file" >&2
    FAILED=$((FAILED + 1))
  fi
done

echo ""
echo "Results: $SUCCESS success, $FAILED failed"

if [ $FAILED -gt 0 ]; then
  exit 1
fi
```

## CI/CD Integration

### GitHub Actions

```yaml
name: Generate PDF Documentation

on:
  push:
    paths:
      - 'docs/**/*.fo'

jobs:
  generate-pdfs:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v3

      - name: Install Rust
        uses: actions-rs/toolchain@v1
        with:
          toolchain: stable

      - name: Build FOP CLI
        run: cargo build --release --package fop-cli

      - name: Generate PDFs
        run: |
          mkdir -p output
          for fo_file in docs/*.fo; do
            filename=$(basename "$fo_file" .fo)
            ./target/release/fop "$fo_file" "output/${filename}.pdf" \
              --quiet --compress --stats --output-format json > "output/${filename}.json"
          done

      - name: Upload Artifacts
        uses: actions/upload-artifact@v3
        with:
          name: pdf-documentation
          path: output/*.pdf
```

### GitLab CI

```yaml
stages:
  - build
  - generate

build-fop:
  stage: build
  image: rust:latest
  script:
    - cargo build --release --package fop-cli
  artifacts:
    paths:
      - target/release/fop

generate-pdfs:
  stage: generate
  dependencies:
    - build-fop
  script:
    - mkdir -p output
    - |
      for fo_file in docs/*.fo; do
        filename=$(basename "$fo_file" .fo)
        ./target/release/fop "$fo_file" "output/${filename}.pdf" --quiet --compress
      done
  artifacts:
    paths:
      - output/*.pdf
```

### Jenkins Pipeline

```groovy
pipeline {
    agent any

    stages {
        stage('Build') {
            steps {
                sh 'cargo build --release --package fop-cli'
            }
        }

        stage('Generate PDFs') {
            steps {
                sh '''
                    mkdir -p output
                    for fo_file in docs/*.fo; do
                        filename=$(basename "$fo_file" .fo)
                        ./target/release/fop "$fo_file" "output/${filename}.pdf" \
                            --quiet --compress --stats --output-format json > "output/${filename}.json"
                    done
                '''
            }
        }

        stage('Archive') {
            steps {
                archiveArtifacts artifacts: 'output/*.pdf', fingerprint: true
                archiveArtifacts artifacts: 'output/*.json', fingerprint: true
            }
        }
    }
}
```

## Advanced Usage

### Environment Variables

```bash
# Set log level
export RUST_LOG=debug
fop document.fo document.pdf

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

# Disable all logging
export RUST_LOG=off
fop document.fo document.pdf --quiet
```

### Custom Configuration

```bash
# Set resource limits
fop document.fo document.pdf --max-memory 1024  # 1GB limit

# Performance tuning
fop document.fo document.pdf \
  -j 8 \              # 8 threads
  --max-memory 2048   # 2GB memory limit
```

### Integration with Other Tools

```bash
# Convert XSLT + XML to FO to PDF
xsltproc stylesheet.xsl data.xml | fop - output.pdf

# Generate from template
cat template.fo | sed 's/{{title}}/My Document/' | fop - output.pdf

# Validate and convert in pipeline
fop document.fo --validate-only && fop document.fo document.pdf
```

## Troubleshooting

### Common Issues

#### 1. Input file not found
```bash
# Error: Input file does not exist
# Solution: Check path
ls -l document.fo
fop "$(pwd)/document.fo" output.pdf
```

#### 2. Output directory doesn't exist
```bash
# Error: Output directory does not exist
# Solution: Create directory first
mkdir -p output
fop document.fo output/document.pdf
```

#### 3. Permission denied
```bash
# Error: Permission denied
# Solution: Check permissions
chmod 644 document.fo
chmod 755 output/
```

#### 4. Out of memory
```bash
# Error: Out of memory
# Solution: Set memory limit or reduce document size
fop document.fo document.pdf --max-memory 4096
```

### Debug Mode

```bash
# Enable verbose logging
RUST_LOG=debug fop document.fo document.pdf --verbose

# Trace all operations
RUST_LOG=trace fop document.fo document.pdf --verbose
```

### Performance Profiling

```bash
# Time the conversion
time fop document.fo document.pdf

# With statistics
fop document.fo document.pdf --stats --output-format json > profile.json
```

## Best Practices

### 1. Development Workflow

```bash
# 1. Validate first
fop document.fo --validate-only

# 2. If valid, generate PDF
if [ $? -eq 0 ]; then
  fop document.fo document.pdf
fi
```

### 2. Production Deployment

```bash
# Use compression and metadata
fop document.fo document.pdf \
  --compress \
  --pdf-version 1.7 \
  --title "Production Document" \
  --author "Your Organization"
```

### 3. Monitoring

```bash
# Collect metrics in JSON
fop document.fo document.pdf \
  --stats \
  --output-format json \
  --quiet > metrics.json

# Send to monitoring system
curl -X POST http://metrics-server/api/fop \
  -H "Content-Type: application/json" \
  -d @metrics.json
```

### 4. Error Handling

```bash
# Always check exit codes
if ! fop document.fo document.pdf --quiet; then
  echo "Conversion failed" >&2
  exit 1
fi

# Use fail-fast in scripts
fop document.fo document.pdf --quiet --fail-fast || exit 1
```

## Performance Tips

### For Large Documents

1. **Enable compression**: Reduces output file size
2. **Use multiple threads**: Faster processing with `-j N`
3. **Suppress progress**: Small performance gain with `--quiet`
4. **Allocate more memory**: Use `--max-memory` for large documents

```bash
fop large-doc.fo large-doc.pdf \
  --compress \
  -j 8 \
  --max-memory 4096 \
  --quiet
```

### For Batch Processing

1. **Use parallel processing**: Process multiple files simultaneously
2. **Disable progress bars**: Use `--quiet` for scripts
3. **Stream output**: Redirect stats to files
4. **Resource limits**: Set `--max-memory` to prevent OOM

```bash
# Process in parallel with GNU parallel
ls *.fo | parallel -j 4 fop {} {.}.pdf --quiet --compress
```

## Output Examples

### Standard 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 (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 (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
  }
}
```

## Support

For issues, questions, or contributions:

- GitHub Issues: https://github.com/apache/fop-rust/issues
- Documentation: See README.md in the fop-cli crate
- Examples: Check the examples/ directory

## License

Apache License 2.0