@@ -12,7 +12,10 @@ import { execProcess } from "./process.ts";
1212import { which } from "./path.ts" ;
1313import { quartoDataDir } from "./appdirs.ts" ;
1414import { 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 - Z a - 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 } ) ;
0 commit comments