fix pdf-salad

This commit is contained in:
2023-04-25 15:20:55 -06:00
parent 0b4ead49f7
commit 9c0ee5a92f
4 changed files with 34 additions and 38 deletions

View File

@@ -9,7 +9,7 @@ extern class 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 copyPages(srcDoc:PDFDocument, indices:Array<Int>):Promise<Array<PDFPage>>;
public function addPage(page:PDFPage):Void;
}

View File

@@ -1,14 +1,9 @@
package;
import haxe.Constraints;
import js.lib.Uint8Array;
import js.node.Fs;
import js.lib.Promise;
import kiss.Kiss;
import kiss.Prelude;
import Externs;
using StringTools;
@:build(kiss.Kiss.build())
class Main {}
class Main {
static function main() {
Main_.main();
}
}

View File

@@ -1,27 +0,0 @@
(function loadAll [:Array<String> paths :Function callback &opt :Array<PDFDocument> 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<PDFDocument> 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))))))

View File

@@ -0,0 +1,28 @@
(import haxe.Constraints)
(import js.lib.Uint8Array)
(import js.node.Fs)
(import js.lib.Promise)
(import Externs)
(function loadAll [:Array<String> paths]
(Promise.all
(filter
(for path paths
(when (path.endsWith ".pdf")
(PDFDocument.load (Fs.readFileSync path)))))))
// 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)]
(awaitLet [inputPdfs (loadAll (for file (Fs.readdirSync sourceDir) (joinPath sourceDir file)))
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))))