Skip to content

Commit ecdb3a9

Browse files
claude: Add pdf-standard option for PDF/A, PDF/UA, and version control
New `pdf-standard` option for LaTeX and Typst PDF output that supports: - PDF versions: 1.4, 1.5, 1.6, 1.7, 2.0 - PDF/A standards: a-1b, a-2a, a-2b, a-2u, a-3a, a-3b, a-3u, a-4, a-4f - PDF/UA standards: ua-1, ua-2 (LaTeX only) - PDF/X standards: x-4, x-4p, x-5g, x-5n, x-5pg, x-6, x-6n, x-6p (LaTeX only) Standards can be combined, e.g., `pdf-standard: [a-2b, ua-1]` for accessible archival PDF. Uses LaTeX's \DocumentMetadata API with `tagging=on` for tagged PDF support. Auto-installs required LaTeX packages (latex-lab, colorprofiles) when missing. Closes #4426 Closes #13782 Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 8bd7018 commit ecdb3a9

23 files changed

Lines changed: 477 additions & 4 deletions

‎news/changelog-1.9.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ All changes included in 1.9:
4343

4444
### `pdf`
4545

46+
- ([#4426](https://github.com/quarto-dev/quarto-cli/issues/4426)): Add `pdf-standard` option for PDF/A, PDF/UA, and PDF version control. Supports standards like `a-2b`, `ua-1`, and versions `1.7`, `2.0`. Works with both LaTeX and Typst formats.
4647
- ([#10291](https://github.com/quarto-dev/quarto-cli/issues/10291)): Fix detection of babel hyphenation warnings with straight-quote format instead of backtick-quote format.
4748
- ([#13661](https://github.com/quarto-dev/quarto-cli/issues/13661)): Fix LaTeX compilation errors when using `mermaid-format: svg` with PDF/LaTeX output. SVG diagrams are now written directly without HTML script tags. Note: `mermaid-format: png` is recommended for best compatibility. SVG format requires `rsvg-convert` (or Inkscape with `use-rsvg-convert: false`) in PATH for conversion to PDF, and may experience text clipping in diagrams with multi-line labels.
4849
- ([rstudio/tinytex-releases#49](https://github.com/rstudio/tinytex-releases/issues/49)): Fix detection of LuaTeX-ja missing file errors by matching both "File" and "file" in error messages.

‎src/command/render/latexmk/parse-error.ts‎

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,6 +107,53 @@ const resolvingMatchers = [
107107
},
108108
];
109109

110+
// Finds PDF/UA accessibility warnings from tagpdf and DocumentMetadata
111+
export interface PdfAccessibilityWarnings {
112+
missingAltText: string[]; // filenames of images missing alt text
113+
missingLanguage: boolean; // document language not set
114+
otherWarnings: string[]; // other tagpdf warnings
115+
}
116+
117+
export function findPdfAccessibilityWarnings(
118+
logText: string,
119+
): PdfAccessibilityWarnings {
120+
const result: PdfAccessibilityWarnings = {
121+
missingAltText: [],
122+
missingLanguage: false,
123+
otherWarnings: [],
124+
};
125+
126+
// Match: Package tagpdf Warning: Alternative text for graphic is missing.
127+
// (tagpdf) Using 'filename' instead.
128+
const altTextRegex =
129+
/Package tagpdf Warning: Alternative text for graphic is missing\.\s*\n\(tagpdf\)\s*Using ['`]([^'`]+)['`] instead\./g;
130+
let match;
131+
while ((match = altTextRegex.exec(logText)) !== null) {
132+
result.missingAltText.push(match[1]);
133+
}
134+
135+
// Match: LaTeX DocumentMetadata Warning: The language has not been set in
136+
if (
137+
/LaTeX DocumentMetadata Warning: The language has not been set in/.test(
138+
logText,
139+
)
140+
) {
141+
result.missingLanguage = true;
142+
}
143+
144+
// Capture any other tagpdf warnings we haven't specifically handled
145+
const otherTagpdfRegex = /Package tagpdf Warning: ([^\n]+)/g;
146+
while ((match = otherTagpdfRegex.exec(logText)) !== null) {
147+
const warning = match[1];
148+
// Skip the alt text warning we already handle specifically
149+
if (!warning.startsWith("Alternative text for graphic is missing")) {
150+
result.otherWarnings.push(warning);
151+
}
152+
}
153+
154+
return result;
155+
}
156+
110157
// Finds missing hyphenation files (these appear as warnings in the log file)
111158
export function findMissingHyphenationFiles(logText: string) {
112159
//ngerman gets special cased
@@ -273,6 +320,19 @@ const packageMatchers = [
273320
return "colorprofiles.sty";
274321
},
275322
},
323+
{
324+
regex: /.*No support files for \\DocumentMetadata found.*/g,
325+
filter: (_match: string, _text: string) => {
326+
return "latex-lab";
327+
},
328+
},
329+
{
330+
// PDF/A requires embedded color profiles - pdfmanagement-testphase needs colorprofiles
331+
regex: /.*\(pdf backend\): cannot open file for embedding.*/g,
332+
filter: (_match: string, _text: string) => {
333+
return "colorprofiles";
334+
},
335+
},
276336
{
277337
regex: /.*No file ([^`'. ]+[.]fd)[.].*/g,
278338
filter: (match: string, _text: string) => {
@@ -297,7 +357,7 @@ const packageMatchers = [
297357
];
298358

299359
function fontSearchTerm(font: string): string {
300-
const fontPattern = font.replace(/\s+/g, '\\s*');
360+
const fontPattern = font.replace(/\s+/g, "\\s*");
301361
return `${fontPattern}(-(Bold|Italic|Regular).*)?[.](tfm|afm|mf|otf|ttf)`;
302362
}
303363

‎src/command/render/latexmk/pdf.ts‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
findLatexError,
2222
findMissingFontsAndPackages,
2323
findMissingHyphenationFiles,
24+
findPdfAccessibilityWarnings,
2425
kMissingFontLog,
2526
needsRecompilation,
2627
} from "./parse-error.ts";
@@ -197,6 +198,25 @@ async function initialCompileLatex(
197198
`Possibly missing hyphenation file: '${missingHyphenationFile}'. See more in logfile (by setting 'latex-clean: false').\n`,
198199
);
199200
}
201+
202+
// Check for accessibility warnings (e.g., missing alt text, language with PDF/UA)
203+
const accessibilityWarnings = findPdfAccessibilityWarnings(logText);
204+
if (accessibilityWarnings.missingAltText.length > 0) {
205+
const fileList = accessibilityWarnings.missingAltText.join(", ");
206+
warning(
207+
`PDF accessibility: Missing alt text for image(s): ${fileList}. Add alt text using ![alt text](image.png) syntax for PDF/UA compliance.\n`,
208+
);
209+
}
210+
if (accessibilityWarnings.missingLanguage) {
211+
warning(
212+
`PDF accessibility: Document language not set. Add 'lang: en' (or appropriate language) to document metadata for PDF/UA compliance.\n`,
213+
);
214+
}
215+
if (accessibilityWarnings.otherWarnings.length > 0) {
216+
for (const warn of accessibilityWarnings.otherWarnings) {
217+
warning(`PDF accessibility: ${warn}\n`);
218+
}
219+
}
200220
} else if (pkgMgr.autoInstall) {
201221
// try autoinstalling
202222
// First be sure all packages are up to date

‎src/command/render/latexmk/texlive.ts‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,10 +86,11 @@ export async function findPackages(
8686
`finding package for ${searchTerm}`,
8787
);
8888
}
89-
// Special case for a known package
89+
// Special cases for known packages where tlmgr file search doesn't work
9090
// https://github.com/rstudio/tinytex/blob/33cbe601ff671fae47c594250de1d22bbf293b27/R/latex.R#L470
91-
if (searchTerm === "fandol") {
92-
results.push("fandol");
91+
const knownPackages = ["fandol", "latex-lab", "colorprofiles"];
92+
if (knownPackages.includes(searchTerm)) {
93+
results.push(searchTerm);
9394
} else {
9495
const result = await tlmgrCommand(
9596
"search",

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ import {
1212
kKeepTyp,
1313
kOutputExt,
1414
kOutputFile,
15+
kPdfStandard,
1516
kVariant,
1617
} from "../../config/constants.ts";
18+
import { warning } from "../../deno_ral/log.ts";
1719
import { Format } from "../../config/types.ts";
1820
import { writeFileToStdout } from "../../core/console.ts";
1921
import { dirAndStem, expandPath } from "../../core/path.ts";
@@ -66,6 +68,11 @@ export function typstPdfOutputRecipe(
6668
const typstOptions: TypstCompileOptions = {
6769
quiet: options.flags?.quiet,
6870
fontPaths: asArray(format.metadata?.[kFontPaths]) as string[],
71+
pdfStandard: normalizePdfStandardForTypst(
72+
asArray(
73+
format.render?.[kPdfStandard] ?? format.metadata?.[kPdfStandard],
74+
),
75+
),
6976
};
7077
if (project?.dir) {
7178
typstOptions.rootDir = project.dir;
@@ -140,3 +147,41 @@ export function typstPdfOutputRecipe(
140147

141148
return recipe;
142149
}
150+
151+
// Typst-supported PDF standards
152+
const kTypstSupportedStandards = new Set([
153+
"1.4",
154+
"1.5",
155+
"1.6",
156+
"1.7",
157+
"2.0",
158+
"a-1b",
159+
"a-1a",
160+
"a-2b",
161+
"a-2u",
162+
"a-2a",
163+
"a-3b",
164+
"a-3u",
165+
"a-3a",
166+
"a-4",
167+
"a-4f",
168+
"a-4e",
169+
"ua-1",
170+
]);
171+
172+
function normalizePdfStandardForTypst(standards: unknown[]): string[] {
173+
const result: string[] = [];
174+
for (const s of standards) {
175+
if (typeof s !== "string") continue;
176+
// Normalize: lowercase, remove any "pdf" prefix
177+
const normalized = s.toLowerCase().replace(/^pdf[/-]?/, "");
178+
if (kTypstSupportedStandards.has(normalized)) {
179+
result.push(normalized);
180+
} else {
181+
warning(
182+
`PDF standard '${s}' is not supported by Typst and will be ignored`,
183+
);
184+
}
185+
}
186+
return result;
187+
}

‎src/config/constants.ts‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ export const kShortcodes = "shortcodes";
8686
export const kKeepMd = "keep-md";
8787
export const kKeepTex = "keep-tex";
8888
export const kKeepTyp = "keep-typ";
89+
export const kPdfStandard = "pdf-standard";
8990
export const kKeepIpynb = "keep-ipynb";
9091
export const kKeepSource = "keep-source";
9192
export const kVariant = "variant";
@@ -219,6 +220,7 @@ export const kRenderDefaultsKeys = [
219220
kLatexTlmgrOpts,
220221
kLatexOutputDir,
221222
kLatexTinyTex,
223+
kPdfStandard,
222224
kLinkExternalIcon,
223225
kLinkExternalNewwindow,
224226
kLinkExternalFilter,
@@ -686,6 +688,7 @@ export const kPandocDefaultsKeys = [
686688
kPdfEngine,
687689
kPdfEngineOpts,
688690
kPdfEngineOpt,
691+
kPdfStandard,
689692
kWrap,
690693
kColumns,
691694
"dpi",

‎src/config/types.ts‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,7 @@ import {
176176
kPdfEngine,
177177
kPdfEngineOpt,
178178
kPdfEngineOpts,
179+
kPdfStandard,
179180
kPlotlyConnected,
180181
kPreferHtml,
181182
kPreserveYaml,
@@ -492,6 +493,7 @@ export interface FormatRender {
492493
[kLatexMinRuns]?: number;
493494
[kLatexMaxRuns]?: number;
494495
[kLatexClean]?: boolean;
496+
[kPdfStandard]?: string | string[];
495497
[kLatexInputPaths]?: string[];
496498
[kLatexMakeIndex]?: string;
497499
[kLatexMakeIndexOpts]?: string[];

‎src/core/typst.ts‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export type TypstCompileOptions = {
3939
quiet?: boolean;
4040
fontPaths?: string[];
4141
rootDir?: string;
42+
pdfStandard?: string[];
4243
};
4344

4445
export async function typstCompile(
@@ -58,6 +59,9 @@ export async function typstCompile(
5859
if (options.rootDir) {
5960
cmd.push("--root", options.rootDir);
6061
}
62+
if (options.pdfStandard && options.pdfStandard.length > 0) {
63+
cmd.push("--pdf-standard", options.pdfStandard.join(","));
64+
}
6165
cmd.push(
6266
input,
6367
...fontPathsArgs(fontPaths),

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

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,15 @@ import {
3131
kNumberSections,
3232
kPaperSize,
3333
kPdfEngine,
34+
kPdfStandard,
3435
kReferenceLocation,
3536
kShiftHeadingLevelBy,
3637
kTblCapLoc,
3738
kTopLevelDivision,
3839
kWarning,
3940
} from "../../config/constants.ts";
41+
import { warning } from "../../deno_ral/log.ts";
42+
import { asArray } from "../../core/array.ts";
4043
import { Format, FormatExtras, PandocFlags } from "../../config/types.ts";
4144

4245
import { createFormat } from "../formats-shared.ts";
@@ -254,6 +257,7 @@ function createPdfFormat(
254257
const partialNamesPandoc: string[] = [
255258
"after-header-includes",
256259
"common",
260+
"document-metadata",
257261
"font-settings",
258262
"fonts",
259263
"hypersetup",
@@ -313,6 +317,27 @@ function createPdfFormat(
313317
extras.pandoc[kNumberSections] = true;
314318
}
315319

320+
// Handle pdf-standard option for PDF/A, PDF/UA, PDF/X conformance
321+
const pdfStandard = asArray(
322+
format.render?.[kPdfStandard] ?? format.metadata?.[kPdfStandard],
323+
);
324+
if (pdfStandard.length > 0) {
325+
const { version, standards } = normalizePdfStandardForLatex(
326+
pdfStandard,
327+
);
328+
extras.pandoc.variables = extras.pandoc.variables || {};
329+
extras.pandoc.variables["pdf-standard"] = true; // Enable the partial
330+
if (version) {
331+
extras.pandoc.variables["pdf-version"] = version;
332+
}
333+
if (standards.length > 0) {
334+
// Pass as array - Pandoc template will iterate with $for()$
335+
extras.pandoc.variables["pdf-standards"] = standards;
336+
}
337+
// Always enable tagging for pdf-standard (stable in LaTeX 2025+)
338+
extras.pandoc.variables["pdf-tagging"] = true;
339+
}
340+
316341
return extras;
317342
},
318343
},
@@ -1236,3 +1261,54 @@ const kbeginLongTablesideCap = `{
12361261
\\makeatother`;
12371262

12381263
const kEndLongTableSideCap = "}";
1264+
1265+
// LaTeX-supported PDF standards
1266+
const kLatexSupportedStandards = new Set([
1267+
"a-1b",
1268+
"a-2a",
1269+
"a-2b",
1270+
"a-2u",
1271+
"a-3a",
1272+
"a-3b",
1273+
"a-3u",
1274+
"a-4",
1275+
"a-4f",
1276+
"x-4",
1277+
"x-4p",
1278+
"x-5g",
1279+
"x-5n",
1280+
"x-5pg",
1281+
"x-6",
1282+
"x-6n",
1283+
"x-6p",
1284+
"ua-1",
1285+
"ua-2",
1286+
]);
1287+
1288+
const kVersionPattern = /^(1\.[4-7]|2\.0)$/;
1289+
1290+
function normalizePdfStandardForLatex(
1291+
standards: unknown[],
1292+
): { version?: string; standards: string[] } {
1293+
let version: string | undefined;
1294+
const result: string[] = [];
1295+
1296+
for (const s of standards) {
1297+
if (typeof s !== "string") continue;
1298+
// Normalize: lowercase, remove any "pdf" prefix
1299+
const normalized = s.toLowerCase().replace(/^pdf[/-]?/, "");
1300+
1301+
if (kVersionPattern.test(normalized)) {
1302+
version = normalized;
1303+
} else if (kLatexSupportedStandards.has(normalized)) {
1304+
// LaTeX is case-insensitive, pass through lowercase
1305+
result.push(normalized);
1306+
} else {
1307+
warning(
1308+
`PDF standard '${s}' is not supported by LaTeX and will be ignored`,
1309+
);
1310+
}
1311+
}
1312+
1313+
return { version, standards: result };
1314+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
$if(pdf-standard)$
2+
\DocumentMetadata{
3+
$if(pdf-version)$
4+
pdfversion=$pdf-version$,
5+
$endif$
6+
$if(pdf-standards)$
7+
pdfstandard={$for(pdf-standards)$$it$$sep$,$endfor$},
8+
$endif$
9+
$if(pdf-tagging)$
10+
tagging=on,
11+
$endif$
12+
}
13+
$endif$

0 commit comments

Comments
 (0)