Files
resume/utils/generatePdf.ts
2024-09-11 13:20:17 -07:00

33 lines
1.1 KiB
TypeScript

import fs from 'node:fs';
import path from 'node:path';
import { checkFileExists, checkFolderExists } from './fsChecks';
import { getLogger } from './logger';
import { markdownToPdf } from './markdownToPdf';
const logger = getLogger("generatePdf");
export async function generatePdf(inputFilePath: string, outputFilePath: string) {
logger.info("Checking if input file exists...");
if (!checkFileExists(inputFilePath)) {
throw new Error(`File ${inputFilePath} does not exist. How am I supposed to build you a resume, mate?`);
}
logger.info("Checking if output folder exists...");
if (!checkFolderExists(outputFilePath)) {
fs.mkdirSync(outputFilePath.split('/').slice(0, -1).join("/"), { recursive: true });
}
try {
logger.info("Trying to generate PDF...");
const pdf = await markdownToPdf(inputFilePath);
if (pdf) {
logger.info("Writing PDF to file...");
fs.writeFileSync(path.resolve(__dirname, outputFilePath), pdf.content);
return;
}
} catch (error: any) {
logger.error(`Error occurred: ${error.message}`);
throw error;
}
}