PDF Salad with repetition allowed
This commit is contained in:
@@ -3,4 +3,4 @@
|
||||
-cp src
|
||||
--main Main
|
||||
-js index.js
|
||||
--cmd node index.js example-input
|
||||
--cmd node index.js example-input 5
|
@@ -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 {}
|
||||
|
@@ -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;
|
||||
|
@@ -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)))))))))))
|
||||
|
||||
|
Reference in New Issue
Block a user