Skip to content

Commit e3fb091

Browse files
claude: Add QUARTO_VERAPDF env var and direct JAR invocation
- Add QUARTO_VERAPDF environment variable support for custom verapdf invocation (Docker, custom Java setups, etc.) - Use direct Java invocation with quarto-installed JAR by default, avoiding .bat file issues on Windows - Fall back to system verapdf on PATH if JAR not installed - Log info message when QUARTO_VERAPDF override is used - Warn if specified QUARTO_VERAPDF path doesn't exist - Add smoke test validating env var override and validation warnings Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
1 parent 9525c1b commit e3fb091

2 files changed

Lines changed: 143 additions & 17 deletions

File tree

‎src/core/verapdf.ts‎

Lines changed: 97 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,10 @@ import { execProcess } from "./process.ts";
1212
import { which } from "./path.ts";
1313
import { quartoDataDir } from "./appdirs.ts";
1414
import { existsSync } from "../deno_ral/fs.ts";
15-
import { isWindows } from "../deno_ral/platform.ts";
15+
import { warnOnce } from "./log.ts";
16+
17+
const kVerapdfEnvVar = "QUARTO_VERAPDF";
18+
const kVerapdfMainClass = "org.verapdf.apps.GreenfieldCliWrapper";
1619

1720
// verapdf flavours (profiles) supported for validation
1821
// Maps quarto pdf-standard values to verapdf --flavour values
@@ -53,24 +56,101 @@ export function getVerapdfFlavour(standard: string): string | undefined {
5356
return kVerapdfFlavours[normalized];
5457
}
5558

59+
export interface VerapdfInvocation {
60+
cmd: string;
61+
args: string[];
62+
}
63+
5664
/**
57-
* Find verapdf binary path - checks PATH and quarto install location
65+
* Build the verapdf command to execute.
66+
* Priority:
67+
* 1. QUARTO_VERAPDF env var (user override)
68+
* 2. Direct Java invocation with quarto-installed JAR
69+
* 3. System verapdf on PATH (fallback)
5870
*/
59-
export async function findVerapdfPath(): Promise<string | undefined> {
60-
// First check PATH
61-
const pathResult = await which("verapdf");
62-
if (pathResult) {
63-
return pathResult;
71+
export async function buildVerapdfCommand(
72+
flavour: string,
73+
pdfPath: string,
74+
): Promise<VerapdfInvocation | undefined> {
75+
// Priority 1: QUARTO_VERAPDF env var
76+
const envOverride = Deno.env.get(kVerapdfEnvVar);
77+
if (envOverride) {
78+
// Check if it looks like a file path and doesn't exist
79+
const looksLikePath = envOverride.startsWith("/") ||
80+
envOverride.startsWith("./") ||
81+
envOverride.startsWith("~") ||
82+
/^[A-Za-z]:[\\/]/.test(envOverride);
83+
84+
if (looksLikePath && !existsSync(envOverride)) {
85+
warnOnce(
86+
`Specified ${kVerapdfEnvVar} does not exist, using built-in verapdf`,
87+
);
88+
} else {
89+
info(`Using ${kVerapdfEnvVar}: ${envOverride}`);
90+
return {
91+
cmd: envOverride,
92+
args: ["-f", flavour, pdfPath],
93+
};
94+
}
6495
}
6596

66-
// Check quarto install location
97+
// Priority 2: Direct Java invocation with quarto-installed JAR
6798
const quartoVerapdfDir = quartoDataDir("verapdf");
68-
const quartoVerapdfBin = isWindows
69-
? join(quartoVerapdfDir, "verapdf.bat")
70-
: join(quartoVerapdfDir, "verapdf");
99+
const binDir = join(quartoVerapdfDir, "bin");
71100

72-
if (existsSync(quartoVerapdfBin)) {
73-
return quartoVerapdfBin;
101+
if (existsSync(binDir)) {
102+
// Use direct Java invocation to avoid .bat issues on Windows
103+
const javaPath = await which("java");
104+
if (javaPath) {
105+
return {
106+
cmd: javaPath,
107+
args: [
108+
"-classpath",
109+
join(binDir, "*"),
110+
"-Dfile.encoding=UTF8",
111+
"-XX:+IgnoreUnrecognizedVMOptions",
112+
`-Dbasedir=${quartoVerapdfDir}`,
113+
"--add-exports=java.base/sun.security.pkcs=ALL-UNNAMED",
114+
kVerapdfMainClass,
115+
"-f",
116+
flavour,
117+
pdfPath,
118+
],
119+
};
120+
}
121+
}
122+
123+
// Priority 3: System verapdf on PATH
124+
const systemVerapdf = await which("verapdf");
125+
if (systemVerapdf) {
126+
return {
127+
cmd: systemVerapdf,
128+
args: ["-f", flavour, pdfPath],
129+
};
130+
}
131+
132+
return undefined;
133+
}
134+
135+
/**
136+
* Find verapdf binary path - checks quarto install location and PATH
137+
* @deprecated Use buildVerapdfCommand instead
138+
*/
139+
export async function findVerapdfPath(): Promise<string | undefined> {
140+
// Check quarto install location (bin directory for direct Java invocation)
141+
const quartoVerapdfDir = quartoDataDir("verapdf");
142+
const binDir = join(quartoVerapdfDir, "bin");
143+
144+
if (existsSync(binDir)) {
145+
// Return the directory to indicate verapdf is installed
146+
// (actual invocation will use buildVerapdfCommand)
147+
return binDir;
148+
}
149+
150+
// Fall back to system PATH
151+
const pathResult = await which("verapdf");
152+
if (pathResult) {
153+
return pathResult;
74154
}
75155

76156
return undefined;
@@ -91,8 +171,8 @@ export async function validatePdf(
91171
flavour: string,
92172
quiet = false,
93173
): Promise<VerapdfValidationResult> {
94-
const verapdfPath = await findVerapdfPath();
95-
if (!verapdfPath) {
174+
const invocation = await buildVerapdfCommand(flavour, pdfPath);
175+
if (!invocation) {
96176
throw new Error("verapdf not found");
97177
}
98178

@@ -106,8 +186,8 @@ export async function validatePdf(
106186
}
107187

108188
const result = await execProcess({
109-
cmd: verapdfPath,
110-
args: ["-f", flavour, pdfPath],
189+
cmd: invocation.cmd,
190+
args: invocation.args,
111191
stdout: "piped",
112192
stderr: "piped",
113193
});
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* render-verapdf.test.ts
3+
*
4+
* Copyright (C) 2020-2022 Posit Software, PBC
5+
*
6+
* Tests for QUARTO_VERAPDF environment variable override functionality.
7+
*/
8+
9+
import { existsSync } from "../../../src/deno_ral/fs.ts";
10+
import { join } from "../../../src/deno_ral/path.ts";
11+
import { quartoDataDir } from "../../../src/core/appdirs.ts";
12+
import { isWindows } from "../../../src/deno_ral/platform.ts";
13+
import { docs } from "../../utils.ts";
14+
import { printsMessage } from "../../verify.ts";
15+
import { testRender } from "./render.ts";
16+
17+
// Test that QUARTO_VERAPDF override is logged and validation runs correctly
18+
// Uses ua-missing-title.qmd which should produce a validation warning
19+
const input = docs("smoke-all/pdf-standard/ua-missing-title.qmd");
20+
21+
// Point QUARTO_VERAPDF to the installed script to test the override path
22+
const verapdfDir = quartoDataDir("verapdf");
23+
const verapdfScript = isWindows
24+
? join(verapdfDir, "verapdf.bat")
25+
: join(verapdfDir, "verapdf");
26+
27+
testRender(input, "pdf", true, [
28+
// Verify the QUARTO_VERAPDF override message is logged
29+
printsMessage({
30+
level: "INFO",
31+
regex: /Using QUARTO_VERAPDF:.*verapdf/,
32+
}),
33+
// Verify that validation actually ran and caught the missing title
34+
printsMessage({
35+
level: "WARN",
36+
regex: /PDF validation failed for ua-2/,
37+
}),
38+
], {
39+
prereq: async () => {
40+
// Skip if verapdf not installed
41+
return existsSync(verapdfScript);
42+
},
43+
env: {
44+
QUARTO_VERAPDF: verapdfScript,
45+
},
46+
});

0 commit comments

Comments
 (0)