Skip to content

Commit 860b2bf

Browse files
claude: Auto-infer PDF version from PDF/A standard requirements
PDF/A standards have maximum PDF version requirements: - PDF/A-1 (a-1a, a-1b) -> PDF 1.4 (required by spec) - PDF/A-2 (a-2a, a-2b, a-2u) -> PDF 1.7 (max version) - PDF/A-3 (a-3a, a-3b, a-3u) -> PDF 1.7 (max version) LaTeX defaults to PDF 2.0 with \DocumentMetadata, which caused verapdf validation failures for PDF/A-2b. Now we automatically set the correct pdfversion based on the requested standard. Also fixes handling of unquoted numeric PDF versions in YAML (e.g., `pdf-standard: 2.0` which YAML parses as integer 2) in all three files: - src/core/verapdf.ts - src/format/pdf/format-pdf.ts - src/command/render/output-typst.ts Adds tests and .gitignore files for generated test outputs. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent bf84cc8 commit 860b2bf

9 files changed

Lines changed: 132 additions & 13 deletions

File tree

‎src/command/render/output-typst.ts‎

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,18 @@ const kTypstSupportedStandards = new Set([
187187
function normalizePdfStandardForTypst(standards: unknown[]): string[] {
188188
const result: string[] = [];
189189
for (const s of standards) {
190-
if (typeof s !== "string") continue;
190+
// Convert to string - YAML may parse versions like 2.0 as integer 2
191+
let str: string;
192+
if (typeof s === "number") {
193+
// Handle YAML numeric parsing: integer 2 -> "2.0", float 1.4 -> "1.4"
194+
str = Number.isInteger(s) ? `${s}.0` : String(s);
195+
} else if (typeof s === "string") {
196+
str = s;
197+
} else {
198+
continue;
199+
}
191200
// Normalize: lowercase, remove any "pdf" prefix
192-
const normalized = s.toLowerCase().replace(/^pdf[/-]?/, "");
201+
const normalized = str.toLowerCase().replace(/^pdf[/-]?/, "");
193202
if (kTypstSupportedStandards.has(normalized)) {
194203
result.push(normalized);
195204
} else {

‎src/core/verapdf.ts‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,9 @@ export async function validatePdfStandards(
206206
const nonValidatableStandards: string[] = [];
207207

208208
for (const standard of standards) {
209-
const normalized = standard.toLowerCase().replace(/^pdf[/-]?/, "");
209+
// Convert to string in case YAML parsed a version number (e.g., 2.0) as a number
210+
const standardStr = String(standard);
211+
const normalized = standardStr.toLowerCase().replace(/^pdf[/-]?/, "");
210212
// Skip version numbers (1.4, 1.5, etc.)
211213
if (/^\d+\.\d+$/.test(normalized)) {
212214
continue;
@@ -215,7 +217,7 @@ export async function validatePdfStandards(
215217
if (flavour) {
216218
validatableStandards.push({ standard: normalized, flavour });
217219
} else {
218-
nonValidatableStandards.push(standard);
220+
nonValidatableStandards.push(standardStr);
219221
}
220222
}
221223

‎src/format/pdf/format-pdf.ts‎

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,6 +1310,23 @@ const kTaggingRequiredStandards = new Set([
13101310

13111311
const kVersionPattern = /^(1\.[4-7]|2\.0)$/;
13121312

1313+
// PDF version required by each standard (maximum version limits)
1314+
// LaTeX defaults to PDF 2.0 with \DocumentMetadata, but some standards
1315+
// have maximum version requirements that are incompatible with 2.0
1316+
const kStandardRequiredVersion: Record<string, string> = {
1317+
// PDF/A-1 requires exactly PDF 1.4
1318+
"a-1a": "1.4",
1319+
"a-1b": "1.4",
1320+
// PDF/A-2 and PDF/A-3 have maximum version of 1.7
1321+
"a-2a": "1.7",
1322+
"a-2b": "1.7",
1323+
"a-2u": "1.7",
1324+
"a-3a": "1.7",
1325+
"a-3b": "1.7",
1326+
"a-3u": "1.7",
1327+
// PDF/A-4, PDF/UA-1, PDF/UA-2 all work with PDF 2.0 (the default)
1328+
};
1329+
13131330
function normalizePdfStandardForLatex(
13141331
standards: unknown[],
13151332
): { version?: string; standards: string[]; needsTagging: boolean } {
@@ -1318,9 +1335,18 @@ function normalizePdfStandardForLatex(
13181335
let needsTagging = false;
13191336

13201337
for (const s of standards) {
1321-
if (typeof s !== "string") continue;
1338+
// Convert to string - YAML may parse versions like 2.0 as integer 2
1339+
let str: string;
1340+
if (typeof s === "number") {
1341+
// Handle YAML numeric parsing: integer 2 -> "2.0", float 1.4 -> "1.4"
1342+
str = Number.isInteger(s) ? `${s}.0` : String(s);
1343+
} else if (typeof s === "string") {
1344+
str = s;
1345+
} else {
1346+
continue;
1347+
}
13221348
// Normalize: lowercase, remove any "pdf" prefix
1323-
const normalized = s.toLowerCase().replace(/^pdf[/-]?/, "");
1349+
const normalized = str.toLowerCase().replace(/^pdf[/-]?/, "");
13241350

13251351
if (kVersionPattern.test(normalized)) {
13261352
version = normalized;
@@ -1331,6 +1357,10 @@ function normalizePdfStandardForLatex(
13311357
if (kTaggingRequiredStandards.has(normalized)) {
13321358
needsTagging = true;
13331359
}
1360+
// Infer required PDF version from standard (if not explicitly set)
1361+
if (!version && kStandardRequiredVersion[normalized]) {
1362+
version = kStandardRequiredVersion[normalized];
1363+
}
13341364
} else {
13351365
warning(
13361366
`PDF standard '${s}' is not supported by LaTeX and will be ignored`,
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# Generated test outputs (from keep-tex, keep-typ options)
2+
*.typ
3+
*.tex
4+
*.pdf
5+
*-luamml-mathml.html
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
title: "PDF/A-1b compliance test"
3+
lang: en
4+
pdf-standard: a-1b
5+
keep-tex: true
6+
_quarto:
7+
tests:
8+
pdf:
9+
# PDF/A-1b requires PDF 1.4 - this is automatically set for LaTeX
10+
noErrors: default
11+
ensureLatexFileRegexMatches:
12+
- ['\\DocumentMetadata\{', 'pdfversion=1\.4', 'pdfstandard=\{a-1b\}']
13+
- []
14+
typst:
15+
# Typst automatically uses PDF 1.4 for PDF/A-1b
16+
noErrors: default
17+
---
18+
19+
# PDF/A-1b Test
20+
21+
PDF/A-1b is based on ISO 19005-1 and requires PDF 1.4.
22+
23+
Both LaTeX and Typst automatically use PDF 1.4 when `pdf-standard: a-1b`
24+
is specified, since PDF/A-1 is not compatible with higher PDF versions.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
title: "PDF/A-3b compliance test"
3+
lang: en
4+
pdf-standard: a-3b
5+
keep-tex: true
6+
keep-typ: true
7+
_quarto:
8+
tests:
9+
pdf:
10+
# PDF/A-3b requires PDF 1.7 - this is automatically set for LaTeX
11+
noErrors: default
12+
ensureLatexFileRegexMatches:
13+
- ['\\DocumentMetadata\{', 'pdfversion=1\.7', 'pdfstandard=\{a-3b\}']
14+
- []
15+
typst:
16+
noErrors: default
17+
---
18+
19+
# PDF/A-3b Test
20+
21+
PDF/A-3b is based on ISO 19005-3 which extends PDF/A-2 with embedded file support.
22+
23+
Like PDF/A-2, PDF/A-3 has a maximum PDF version requirement of 1.7.
24+
Both LaTeX and Typst automatically set PDF version 1.7 when `pdf-standard: a-3b` is specified.
Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
---
22
title: "PDF/A-2b standard"
3+
lang: en
34
pdf-standard: a-2b
45
keep-tex: true
56
keep-typ: true
@@ -9,12 +10,9 @@ _quarto:
910
noErrors: default
1011
ensureLatexFileRegexMatches:
1112
# a-2b is "basic" level - does NOT require tagging
12-
- ['\\DocumentMetadata\{', 'pdfstandard=\{a-2b\}']
13+
# PDF/A-2 requires maximum PDF version 1.7
14+
- ['\\DocumentMetadata\{', 'pdfversion=1\.7', 'pdfstandard=\{a-2b\}']
1315
- ['tagging=on']
14-
printsMessage:
15-
# LaTeX PDF/A-2b currently fails verapdf validation
16-
level: WARN
17-
regex: "PDF validation failed for a-2b"
1816
typst:
1917
noErrors: default
2018
---
@@ -23,5 +21,5 @@ _quarto:
2321

2422
This tests PDF/A-2b output with both LaTeX and Typst.
2523

26-
- LaTeX: generates correct `\DocumentMetadata` but fails verapdf validation (header format issue)
27-
- Typst: passes verapdf validation
24+
PDF/A-2 is based on ISO 32000-1 (PDF 1.7) and has a maximum PDF version of 1.7.
25+
Both LaTeX and Typst automatically use PDF 1.7 when `pdf-standard: a-2b` is specified.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
---
2+
title: "PDF version (numeric YAML)"
3+
lang: en
4+
pdf-standard: 2.0
5+
keep-tex: true
6+
keep-typ: true
7+
_quarto:
8+
tests:
9+
pdf:
10+
# Test that YAML-parsed numeric version (2.0 -> 2) is handled correctly
11+
noErrors: default
12+
ensureLatexFileRegexMatches:
13+
- ['\\DocumentMetadata\{', 'pdfversion=2\.0']
14+
- []
15+
typst:
16+
# Test that YAML-parsed numeric version (2.0 -> 2) is handled correctly
17+
# pdf-version is passed to typst CLI as --pdf-standard, not in .typ file
18+
noErrors: default
19+
---
20+
21+
# Test Document
22+
23+
This tests that numeric PDF versions in YAML (like `2.0` which YAML parses as integer `2`)
24+
are correctly handled and converted to the proper version string for both LaTeX and Typst.
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Generated test outputs (from keep-typ option)
2+
*.typ
3+
*.pdf

0 commit comments

Comments
 (0)