PDF Salad with repetition allowed

This commit is contained in:
2020-12-04 20:00:54 -07:00
parent e6869bbc8f
commit ccaf0f83a5
4 changed files with 28 additions and 5 deletions

View File

@@ -3,4 +3,4 @@
-cp src
--main Main
-js index.js
--cmd node index.js example-input
--cmd node index.js example-input 5

View File

@@ -8,4 +8,10 @@ extern class PDFDocument {
public static function create():Promise<PDFDocument>;
public static function load(bytes:Buffer):Promise<PDFDocument>;
public function save():Promise<Buffer>;
public function getPageCount():Int;
public function copyPages(srcDoc:PDFDocument, indices:Array<Int>):Array<PDFPage>;
public function addPage(page:PDFPage):Void;
}
@:jsRequire("pdf-lib", "PDFPage")
extern class PDFPage {}

View File

@@ -2,6 +2,7 @@ package;
import js.lib.Uint8Array;
import js.node.Fs;
import js.lib.Promise;
import kiss.Kiss;
import kiss.Prelude;
import Externs;

View File

@@ -1,5 +1,21 @@
(defun main []
(let [bytesIn (Fs.readFileSync "example-input/antique-phone-shop.pdf")]
(.then (PDFDocument.load bytesIn) (lambda [pdfToModify]
(.then (pdfToModify.save) (lambda [bytesOut]
(Fs.writeFileSync "out.pdf" bytesOut)))))))
(let [[sourceDir numPages] (Sys.args)]
(.then
(Promise.all
(for file (Fs.readdirSync sourceDir)
(PDFDocument.load (Fs.readFileSync (+ sourceDir "/" file)))))
(lambda [inputPdfs]
// TODO make an awaitLet macro that .thens a promise or all() of promises into a binding
(.then (PDFDocument.create) (lambda [saladPdf]
(.then
(Promise.all
(for _ #|0... Std.parseInt(numPages)|#
(let [:PDFDocument pdf (nth inputPdfs (Std.random inputPdfs.length))
page (Std.random (pdf.getPageCount))]
(saladPdf.copyPages pdf [page]))))
(lambda [pages]
(doFor page pages (saladPdf.addPage (first page)))
(.then (saladPdf.save) (lambda [bytesOut]
(Fs.writeFileSync "out.pdf" bytesOut)))))))))))