import which from "which" ; import { join , parse } from "path" ; const { execute_code } = require ( "@cocalc/backend/misc_node" ) ; import { callback_opts } from "@cocalc/util/async-utils" ; export default async function htmlToPDF ( path , timeout = 30 ) { const { dir , name } = parse ( path ) ; const outfile = join ( dir , name + ".pdf" ) ; const command = await getCommand ( ) ; const args = [ "--headless" , "--disable-gpu" , "--no-sandbox" , `--print-to-pdf=${ outfile }` , "--run-all-compositor-stages-before-draw" , `--virtual-time-budget=${ timeout * 1000 }` , path , ] ; const output = await callback_opts ( execute_code ) ( { command , args , err_on_exit : false , timeout , ulimit_timeout : true , bash : true , } ) ; if ( output . exit_code != 0 ) { throw Error ( output . stderr ) ; } return outfile ; } const COMMANDS = [ "google-chrome" , "chromium-browser" ] ; let cache = "" ; async function getCommand ( ) { if ( cache ) return cache ; for ( const cmd of COMMANDS ) { try { await which ( cmd ) ; cache = cmd ; return cmd ; } catch ( _err ) { } } throw Error ( `one of ${ COMMANDS . join ( " or " ) } must be installed to convert to PDF` ) ; }