Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

Add PDF/X-4 validation command (#200)

Vikram Bhaskaran3de58750cd0a
diff --git a/apps/designcraft-cli/src/main.rs b/apps/designcraft-cli/src/main.rs
index 11514f709..b1819c357 100644
--- a/apps/designcraft-cli/src/main.rs
+++ b/apps/designcraft-cli/src/main.rs
@@ -12,6 +12,7 @@
//! designcraft-cli mcp [--connect PORT] [--sample] # MCP server over stdio (docs/mcp.md)
//! designcraft-cli perf [--pages N] [--frames N] [--chars N] [--images N] [--runs N] [--strict] # budgets on a synthetic stress document
//! designcraft-cli bench FILE [--runs N] # the same measurements on one document
+//! designcraft-cli validate-pdf FILE.pdf # run the built-in PDF/X-4 conformance checks
//! designcraft-cli links # Discord, website, app page and GitHub links
//! designcraft-cli --version # print the version
//! ```
@@ -84,6 +85,7 @@ fn main() -> ExitCode {
Some("mcp") => report(mcp(&args[1..])),
Some("perf") => report(perf::perf(&args[1..])),
Some("bench") => report(perf::bench(&args[1..])),
+ Some("validate-pdf") => report(validate_pdf(args.get(1).map(String::as_str))),
Some("links") => {
use designcraft_engine::links::*;
outln!("Discord {DISCORD}\nWebsite {WEBSITE}\nApp page {APP_PAGE}\nGitHub {GITHUB}\nIssues {ISSUES}");
@@ -114,6 +116,16 @@ fn report(r: Result<(), String>) -> ExitCode {
}
}
+fn validate_pdf(path: Option<&str>) -> Result<(), String> {
+ let path = path.ok_or("usage: designcraft-cli validate-pdf FILE.pdf")?;
+ let bytes = std::fs::read(path).map_err(|e| format!("{path}: {e}"))?;
+ let issues = designcraft_pdf::check_pdfx4(&bytes);
+ let valid = issues.is_empty();
+ let report = json!({"path": path, "standard": "PDF/X-4", "valid": valid, "issues": issues});
+ outln!("{}", serde_json::to_string_pretty(&report).map_err(|e| e.to_string())?);
+ if valid { Ok(()) } else { Err(format!("{path} failed PDF/X-4 validation")) }
+}
+
/// `mcp` (headless, in-process engine) or `mcp --connect PORT|HOST:PORT` (drive a running app
/// started with `designcraft --control PORT`). JSON-RPC on stdin/stdout; logs on stderr.
fn mcp(args: &[String]) -> Result<(), String> {
diff --git a/crates/pdf/src/lib.rs b/crates/pdf/src/lib.rs
index 080bb5981..d2fbba7ba 100644
--- a/crates/pdf/src/lib.rs
+++ b/crates/pdf/src/lib.rs
@@ -20,9 +20,8 @@
//! Unicode mapping taken from the story text (ligatures, page numbers and inserted hyphens get the
//! right characters), so text is selectable and searchable.
//! - **Marks:** crop marks, bleed marks and a page-information line, drawn in `[Registration]`.
-//! - **Standards:** PDF/A-2b through krilla's validator. PDF/X-4 writes PDF 1.6 with trim/bleed
-//! boxes, but the PDF/X output intent and identification are not written yet (krilla 0.8 has no
-//! PDF/X support) — the export report says so.
+//! - **Standards:** PDF/A-2b through krilla's validator and PDF/X-4 output intent/identification
+//! with built-in conformance checks. `designcraft-cli validate-pdf` exposes those checks for CI.
//!
//! Not yet: tagged PDF, bookmarks, hyperlinks, overprint, layers as optional content.
#![deny(clippy::unwrap_used, clippy::expect_used, clippy::panic, clippy::unimplemented, clippy::todo, clippy::unreachable)]
@@ -46,8 +45,7 @@ pub enum Standard {
/// Plain PDF 1.7.
#[default]
None,
- /// PDF/X-4 (PDF 1.6, trim/bleed boxes). TODO: OutputIntent + GTS_PDFXVersion once krilla
- /// supports PDF/X.
+ /// PDF/X-4 (PDF 1.6, trim/bleed boxes, output intent and identification).
PdfX4,
/// PDF/A-2b (archival; validated by krilla).
PdfA2b,