More debuggable pdf-salad

This commit is contained in:
2020-12-05 21:47:37 -07:00
parent 2fd60370e8
commit 567e531208
3 changed files with 24 additions and 6 deletions

View File

@@ -6,12 +6,20 @@ import js.lib.Promise;
@:jsRequire("pdf-lib", "PDFDocument")
extern class PDFDocument {
public static function create():Promise<PDFDocument>;
public static function load(bytes:Buffer):Promise<PDFDocument>;
public static function load(bytes:Buffer, ?options:LoadOptions):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;
}
typedef LoadOptions = {
?capNumbers:Bool,
?ignoreEncryption:Bool,
?parseSpeed:Float,
?throwOnInvalidObject:Bool,
?updateMetadata:Bool
};
@:jsRequire("pdf-lib", "PDFPage")
extern class PDFPage {}

View File

@@ -1,5 +1,6 @@
package;
import haxe.Constraints;
import js.lib.Uint8Array;
import js.node.Fs;
import js.lib.Promise;

View File

@@ -1,10 +1,19 @@
(defun loadAll [paths :Function callback &opt :Array<PDFDocument> pdfs]
(unless pdfs (set pdfs []))
(deflocal nextPdf (paths.shift))
(print nextPdf)
(.then (PDFDocument.load (Fs.readFileSync nextPdf))
(lambda [pdf]
(pdfs.push pdf)
(if paths (loadAll paths callback pdfs)
(callback pdfs)))
(lambda [error]
(throw #|'error $error loading $nextPdf'|#))))
(defun main []
(let [[sourceDir numPages] (Sys.args)]
(.then
(Promise.all
(for file (Fs.readdirSync sourceDir)
(PDFDocument.load (Fs.readFileSync (+ sourceDir "/" file)))))
(lambda [inputPdfs]
(loadAll (for file (Fs.readdirSync sourceDir) (+ sourceDir "/" file))
(lambda [:Array<PDFDocument> inputPdfs]
// TODO make an awaitLet macro that .thens a promise or chain of promises (so the bindings are sequential) into a binding
(.then (PDFDocument.create) (lambda [saladPdf]
(.then