normordis-pdf 2.5.1

Institutional PDF generation for Portuguese public administration
Documentation
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
use base64::Engine as _;
use regex::Regex;
use serde_json::Value;

use super::data::NdtData;
use super::resolver;
use crate::ndf::{
    audit::{Actor, AuditEvent, EventType, NdfAudit},
    integrity::{canonical_hash, NdfIntegrity},
    NdfDocument, NdfEmbeddedFont, NdfMeta, NdfOrigin, NDF_VERSION,
};
use crate::{NormaxisPdfError, Result};

// ── CompileOptions ────────────────────────────────────────────────────────────

/// Options controlling how `compile_ndt()` builds an `NdfDocument`.
#[derive(Debug, Clone)]
pub struct CompileOptions {
    /// Unique document identifier.
    /// If `None`, a UUID v4 is generated automatically.
    pub document_id: Option<String>,

    /// Actor responsible for this generation (stored in audit chain).
    pub generated_by: Actor,

    /// NDT template identifier for origin traceability.
    pub ndt_template_id: Option<String>,

    /// SHA-256 hash of the NDT template file.
    /// If `None`, computed automatically from the `ndt` input.
    pub ndt_template_hash: Option<String>,

    /// If `true` (default), error when any `{{placeholder}}` remains unresolved.
    pub validate_resolved: bool,
}

impl Default for CompileOptions {
    fn default() -> Self {
        Self {
            document_id: None,
            generated_by: Actor::System {
                id: "normordis-pdf".into(),
                version: Some(env!("CARGO_PKG_VERSION").into()),
                instance_id: None,
            },
            ndt_template_id: None,
            ndt_template_hash: None,
            validate_resolved: true,
        }
    }
}

// ── compile_ndt ───────────────────────────────────────────────────────────────

/// Compiles an NDT template + data into a fully resolved `NdfDocument`.
///
/// Pipeline:
/// 1. Parse NDT (JSON or TOML)
/// 2. Validate required placeholders
/// 3. Deep-substitute `{{placeholders}}` in all body string fields
/// 4. Check no unresolved placeholders remain (`validate_resolved`)
/// 5. Compute integrity hashes (RFC 8785 / JCS)
/// 6. Build and return `NdfDocument`
///
/// After calling this, use [`NdfDocument::embed_font`] for any custom fonts
/// used in the template before persisting the NDF.
pub fn compile_ndt(ndt: &str, data: &NdtData, options: CompileOptions) -> Result<NdfDocument> {
    let doc = super::parse_ndt(ndt)
        .map_err(|e| NormaxisPdfError::NdfCompileError(e.to_string()))?;

    if let Some(ref placeholders) = doc.placeholders {
        super::validator::validate(placeholders, data)
            .map_err(|e| NormaxisPdfError::NdfCompileError(e.to_string()))?;
    }

    // Serialize and resolve body + style
    let body_val = serde_json::to_value(&doc.body)
        .map_err(|e| NormaxisPdfError::SerdeError(e.to_string()))?;
    let resolved_content = resolve_value_placeholders(body_val, data);

    let styles_val = serde_json::to_value(&doc.style)
        .map_err(|e| NormaxisPdfError::SerdeError(e.to_string()))?;

    if options.validate_resolved {
        let content_str = serde_json::to_string(&resolved_content)
            .map_err(|e| NormaxisPdfError::SerdeError(e.to_string()))?;
        let re = Regex::new(r"\{\{[a-zA-Z0-9_.]+\}\}").expect("static regex");
        if let Some(m) = re.find(&content_str) {
            return Err(NormaxisPdfError::NdfCompileError(format!(
                "unresolved placeholder '{}' in content after substitution",
                m.as_str()
            )));
        }
    }

    let now = chrono::Utc::now().to_rfc3339();

    let meta_title = doc
        .meta
        .as_ref()
        .and_then(|m| m.title.clone())
        .unwrap_or_default();
    let meta_compat = doc.meta.as_ref().and_then(|m| m.compat_mode);
    let meta = NdfMeta {
        title: meta_title,
        entity: String::new(),
        entity_id: None,
        lang: "pt-PT".into(),
        document_ref: None,
        document_type: None,
        classification: "public".into(),
        subject: None,
        keywords: None,
        created_at: now.clone(),
        valid_from: None,
        valid_until: None,
        supersedes: None,
        compat_mode: meta_compat,
        numbering: None,
    };
    let meta_val = serde_json::to_value(&meta)
        .map_err(|e| NormaxisPdfError::SerdeError(e.to_string()))?;

    let integrity = NdfIntegrity::compute(&resolved_content, &styles_val, &meta_val)?;
    let document_id = options
        .document_id
        .unwrap_or_else(|| uuid::Uuid::new_v4().to_string());

    let ndt_template_hash = options.ndt_template_hash.unwrap_or_else(|| {
        let v = serde_json::from_str::<Value>(ndt).unwrap_or(Value::Null);
        canonical_hash(&v)
    });

    let first_event = AuditEvent {
        seq: 1,
        event_type: EventType::DocumentGenerated,
        timestamp: now.clone(),
        actor: options.generated_by.clone(),
        content_hash: Some(integrity.content_hash.clone()),
        note: None,
        extra: Default::default(),
    };

    Ok(NdfDocument {
        ndf: NDF_VERSION.into(),
        origin: NdfOrigin {
            ndt_template_id: options.ndt_template_id,
            ndt_version: None,
            ndt_template_hash: Some(ndt_template_hash),
            ndt_data_hash: None,
            engine_version: env!("CARGO_PKG_VERSION").into(),
            engine_backend: "normordis-pdf".into(),
            generated_at: now,
            generated_by: options.generated_by,
        },
        revision: None,
        meta,
        output: serde_json::to_value(&doc.output).ok(),
        styles: styles_val,
        content: resolved_content,
        page: serde_json::to_value(&doc.page).ok(),
        embedded_fonts: vec![],
        integrity,
        audit: NdfAudit {
            document_id,
            events: vec![first_event],
        },
        outputs: vec![],
        signatures: vec![],
    })
}

// ── parse_ndf / verify_ndf ────────────────────────────────────────────────────

/// Parses an NDF document from JSON (canonical or pretty-printed).
pub fn parse_ndf(json: &str) -> Result<NdfDocument> {
    serde_json::from_str(json).map_err(|e| NormaxisPdfError::SerdeError(e.to_string()))
}

/// Verifies the integrity hashes of an NDF document.
pub fn verify_ndf(json: &str) -> Result<crate::ndf::integrity::IntegrityReport> {
    let ndf = parse_ndf(json)?;
    ndf.verify_integrity()
}

// ── render_ndf ────────────────────────────────────────────────────────────────

/// Renders an NDF document to PDF bytes.
///
/// Fonts embedded in `ndf.embedded_fonts` are loaded automatically.
/// For additional or override fonts, use [`render_ndf_with_fonts`].
pub fn render_ndf(ndf_json: &str) -> Result<Vec<u8>> {
    render_ndf_inner(ndf_json, None)
}

/// Renders an NDF document to PDF bytes, supplementing with an external font registry.
///
/// Fonts in `extra` take precedence over fonts embedded in the NDF.
/// Use this when the NDF was archived without embedding font bytes and the
/// original fonts are available at render time.
pub fn render_ndf_with_fonts(
    ndf_json: &str,
    extra: &crate::fonts::FontRegistry,
) -> Result<Vec<u8>> {
    render_ndf_inner(ndf_json, Some(extra))
}

fn render_ndf_inner(
    ndf_json: &str,
    extra_fonts: Option<&crate::fonts::FontRegistry>,
) -> Result<Vec<u8>> {
    let ndf = parse_ndf(ndf_json)?;
    let (ndt_doc, fonts) = rebuild_ndt_doc_and_fonts(&ndf, extra_fonts)?;
    let (standard, compression, accessibility) = parse_output_options(ndf.output.as_ref());

    let style = crate::styles::DocumentStyle::default();
    let empty_data = empty_ndt_data();
    let elements = super::renderer::render_template(&ndt_doc, &empty_data, &style)
        .map_err(|e| NormaxisPdfError::Template(e.to_string()))?;

    crate::document::Document {
        title: ndf.meta.title,
        style,
        fonts,
        header: None,
        sectioned_header: None,
        footer: None,
        sectioned_footer: None,
        watermark: None,
        elements,
        footnotes: vec![],
        toc_entries: None,
        compression,
        standard,
        signature: None,
        traceability: None,
        accessibility,
    }
    .render_to_bytes()
}

// ── render_ndf_prepared_for_signing ──────────────────────────────────────────

/// Renders an NDF document to a [`PreparedPdf`] ready for external PKCS#7 signing.
pub fn render_ndf_prepared_for_signing(
    ndf_json: &str,
    opts: crate::signing::SignatureOptions,
) -> Result<crate::signing::PreparedPdf> {
    render_ndf_prepared_for_signing_inner(ndf_json, opts, None)
}

/// Renders an NDF document to a [`PreparedPdf`], supplementing with an external font registry.
pub fn render_ndf_prepared_for_signing_with_fonts(
    ndf_json: &str,
    opts: crate::signing::SignatureOptions,
    extra: &crate::fonts::FontRegistry,
) -> Result<crate::signing::PreparedPdf> {
    render_ndf_prepared_for_signing_inner(ndf_json, opts, Some(extra))
}

fn render_ndf_prepared_for_signing_inner(
    ndf_json: &str,
    opts: crate::signing::SignatureOptions,
    extra_fonts: Option<&crate::fonts::FontRegistry>,
) -> Result<crate::signing::PreparedPdf> {
    let ndf = parse_ndf(ndf_json)?;
    let (ndt_doc, fonts) = rebuild_ndt_doc_and_fonts(&ndf, extra_fonts)?;
    let (standard, compression, accessibility) = parse_output_options(ndf.output.as_ref());

    let style = crate::styles::DocumentStyle::default();
    let empty_data = empty_ndt_data();
    let elements = super::renderer::render_template(&ndt_doc, &empty_data, &style)
        .map_err(|e| NormaxisPdfError::Template(e.to_string()))?;

    crate::document::Document {
        title: ndf.meta.title,
        style,
        fonts,
        header: None,
        sectioned_header: None,
        footer: None,
        sectioned_footer: None,
        watermark: None,
        elements,
        footnotes: vec![],
        toc_entries: None,
        compression,
        standard,
        signature: None,
        traceability: None,
        accessibility,
    }
    .render_prepared_for_signing(opts)
}

// ── Shared helpers ────────────────────────────────────────────────────────────

/// Reconstruct a renderable `NdtDocument` and a `FontRegistry` from a parsed NDF.
fn rebuild_ndt_doc_and_fonts(
    ndf: &NdfDocument,
    extra_fonts: Option<&crate::fonts::FontRegistry>,
) -> Result<(super::model::NdtDocument, crate::fonts::FontRegistry)> {
    let body: Vec<super::model::BodyElement> =
        serde_json::from_value(ndf.content.clone())
            .map_err(|e| NormaxisPdfError::SerdeError(e.to_string()))?;

    let page: Option<super::model::NdtPage> =
        ndf.page.as_ref().and_then(|v| serde_json::from_value(v.clone()).ok());

    let ndt_doc = super::model::NdtDocument {
        ndt: "2.1.0".into(),
        id: None,
        meta: Some(super::model::NdtMeta {
            title: Some(ndf.meta.title.clone()),
            compat_mode: ndf.meta.compat_mode,
            ..Default::default()
        }),
        style: serde_json::from_value(ndf.styles.clone()).ok(),
        fonts: None,
        page,
        output: None,
        signature: None,
        placeholders: None,
        zones: None,
        body,
    };

    // Build font registry: defaults + embedded + extra (extra wins)
    let mut fonts = crate::fonts::FontRegistry::default();
    for ef in &ndf.embedded_fonts {
        decode_and_register_font(ef, &mut fonts)?;
    }
    if let Some(extra) = extra_fonts {
        for (_name, fam) in extra.families() {
            fonts.register(fam.clone());
        }
    }

    Ok((ndt_doc, fonts))
}

/// Decode a base64-encoded [`NdfEmbeddedFont`] and register it in the registry.
fn decode_and_register_font(
    ef: &NdfEmbeddedFont,
    fonts: &mut crate::fonts::FontRegistry,
) -> Result<()> {
    let dec = base64::engine::general_purpose::STANDARD;
    let regular = dec
        .decode(&ef.regular)
        .map_err(|e| NormaxisPdfError::FontLoadError(e.to_string()))?;
    let bold = ef
        .bold
        .as_deref()
        .map(|s| dec.decode(s).map_err(|e| NormaxisPdfError::FontLoadError(e.to_string())))
        .transpose()?;
    let italic = ef
        .italic
        .as_deref()
        .map(|s| dec.decode(s).map_err(|e| NormaxisPdfError::FontLoadError(e.to_string())))
        .transpose()?;
    let bold_italic = ef
        .bold_italic
        .as_deref()
        .map(|s| dec.decode(s).map_err(|e| NormaxisPdfError::FontLoadError(e.to_string())))
        .transpose()?;
    fonts.register_bytes(
        &ef.family,
        &regular,
        bold.as_deref(),
        italic.as_deref(),
        bold_italic.as_deref(),
    )
}

/// Parse PDF output options from the NDF `output` field.
fn parse_output_options(
    output: Option<&Value>,
) -> (
    crate::document::PdfStandard,
    crate::document::CompressionLevel,
    crate::compliance::ua::AccessibilityConfig,
) {
    let ndt_output: Option<super::model::NdtOutput> =
        output.and_then(|v| serde_json::from_value(v.clone()).ok());

    let standard = match ndt_output.as_ref().and_then(|o| o.standard.as_deref()) {
        Some("pdf_a_1b") | Some("pdf_a1b") => crate::document::PdfStandard::PdfA1b,
        Some("pdf_a_2b") | Some("pdf_a2b") => crate::document::PdfStandard::PdfA2b,
        Some("pdf_ua2") | Some("pdf_ua_2") => crate::document::PdfStandard::PdfUa2,
        _ => crate::document::PdfStandard::Pdf17,
    };

    let compression = match ndt_output.as_ref().and_then(|o| o.compression.as_deref()) {
        Some("none") => crate::document::CompressionLevel::None,
        Some("fast") => crate::document::CompressionLevel::Fast,
        Some("best") => crate::document::CompressionLevel::Best,
        _ => crate::document::CompressionLevel::Default,
    };

    let accessibility = ndt_output
        .as_ref()
        .and_then(|o| o.accessibility.clone())
        .unwrap_or_default();

    (standard, compression, accessibility)
}

fn empty_ndt_data() -> NdtData {
    NdtData {
        ndt_data: "1.0.0".into(),
        template_id: None,
        template_version: None,
        data: Default::default(),
    }
}

// ── Helpers ───────────────────────────────────────────────────────────────────

/// Recursively substitutes `{{placeholder}}` patterns in all string nodes of a JSON value.
fn resolve_value_placeholders(value: Value, data: &NdtData) -> Value {
    match value {
        Value::String(s) => Value::String(resolver::resolve_string(&s, data)),
        Value::Array(arr) => {
            Value::Array(arr.into_iter().map(|v| resolve_value_placeholders(v, data)).collect())
        }
        Value::Object(map) => {
            let mut new_map = serde_json::Map::new();
            for (k, v) in map {
                new_map.insert(k, resolve_value_placeholders(v, data));
            }
            Value::Object(new_map)
        }
        other => other,
    }
}