(function loadAll [:Array paths :Function callback &opt :Array pdfs] (unless pdfs (set pdfs [])) (localVar nextPdf (paths.shift)) (if (nextPdf.endsWith ".pdf") (awaitLet [pdf (PDFDocument.load (Fs.readFileSync (print nextPdf)))] (pdfs.push pdf) (if paths (loadAll paths callback pdfs) (callback pdfs))) (when paths (loadAll paths callback pdfs)))) // TODO add sequentialPerPDF argument (which, when used, .shift()s pages from the beginning of PDFs) // TODO add chunkSize argument (default 1, which specifies how many pages in order to pull from a random PDF. value of -1 means take the whole PDF, and remove it from the list) // TODO make output page limit optional (let [[sourceDir numPages] (Sys.args)] (loadAll (for file (Fs.readdirSync sourceDir) (+ sourceDir "/" file)) (lambda [:Array inputPdfs] (awaitLet [saladPdf (PDFDocument.create) pages (Promise.all (for _ (range 0 (Std.parseInt numPages)) (let [:PDFDocument pdf (nth inputPdfs (Std.random inputPdfs.length)) page (Std.random (pdf.getPageCount))] (saladPdf.copyPages pdf [page]))))] (doFor page pages (saladPdf.addPage (first page))) (awaitLet [bytesOut (saladPdf.save)] (Fs.writeFileSync "out.pdf" bytesOut))))))