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") @:jsRequire("pdf-lib", "PDFDocument")
extern class PDFDocument { extern class PDFDocument {
public static function create():Promise<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 save():Promise<Buffer>;
public function getPageCount():Int; public function getPageCount():Int;
public function copyPages(srcDoc:PDFDocument, indices:Array<Int>):Array<PDFPage>; public function copyPages(srcDoc:PDFDocument, indices:Array<Int>):Array<PDFPage>;
public function addPage(page:PDFPage):Void; public function addPage(page:PDFPage):Void;
} }
typedef LoadOptions = {
?capNumbers:Bool,
?ignoreEncryption:Bool,
?parseSpeed:Float,
?throwOnInvalidObject:Bool,
?updateMetadata:Bool
};
@:jsRequire("pdf-lib", "PDFPage") @:jsRequire("pdf-lib", "PDFPage")
extern class PDFPage {} extern class PDFPage {}

View File

@@ -1,5 +1,6 @@
package; package;
import haxe.Constraints;
import js.lib.Uint8Array; import js.lib.Uint8Array;
import js.node.Fs; import js.node.Fs;
import js.lib.Promise; 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 [] (defun main []
(let [[sourceDir numPages] (Sys.args)] (let [[sourceDir numPages] (Sys.args)]
(.then (loadAll (for file (Fs.readdirSync sourceDir) (+ sourceDir "/" file))
(Promise.all (lambda [:Array<PDFDocument> inputPdfs]
(for file (Fs.readdirSync sourceDir)
(PDFDocument.load (Fs.readFileSync (+ sourceDir "/" file)))))
(lambda [inputPdfs]
// TODO make an awaitLet macro that .thens a promise or chain of promises (so the bindings are sequential) into a binding // 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 (PDFDocument.create) (lambda [saladPdf]
(.then (.then