pdf-tools/util/saveLoadPdf.ts

20 lines
550 B
TypeScript

import { PDFDocument } from "pdf-lib";
export async function loadPdfForm(path: string) {
const pdfDoc = await loadPdf(path);
const form = pdfDoc.getForm();
return form;
}
export async function loadPdf(path: string) {
const pdfBytes = await Deno.readFile(path);
const pdfDoc = await PDFDocument.load(pdfBytes);
return pdfDoc;
}
export async function savePdf(doc: PDFDocument, path: string) {
const pdfBytes = await doc.save();
if (Deno.env.get("DRYRUN") || path.includes("dryrun")) return;
await Deno.writeFile(path, pdfBytes);
}