feat: poppler working for some formats

This commit is contained in:
C4illin
2024-05-30 12:06:02 +02:00
parent f3b4b65c53
commit a8f2cd4e9e
5 changed files with 125 additions and 1 deletions

BIN
bun.lockb

Binary file not shown.

View File

@@ -12,7 +12,8 @@
"@elysiajs/html": "^1.0.2",
"@elysiajs/jwt": "^1.0.2",
"@elysiajs/static": "^1.0.3",
"elysia": "^1.0.22"
"elysia": "^1.0.22",
"node-poppler": "^7.2.0"
},
"module": "src/index.tsx",
"bun-create": {

View File

@@ -20,6 +20,8 @@ import {
properties as propertiesPdflatex,
} from "./pdflatex";
import { convert as convertPoppler, properties as propertiesPoppler } from "./poppler";
import { normalizeFiletype } from "../helpers/normalizeFiletype";
// This should probably be reconstructed so that the functions are not imported instead the functions hook into this to make the converters more modular
@@ -58,6 +60,10 @@ const properties: {
properties: propertiesPdflatex,
converter: convertPdflatex,
},
poppler: {
properties: propertiesPoppler,
converter: convertPoppler,
},
pandoc: {
properties: propertiesPandoc,
converter: convertPandoc,

115
src/converters/poppler.ts Normal file
View File

@@ -0,0 +1,115 @@
const { Poppler } = require("node-poppler");
const poppler = new Poppler();
export const properties = {
from: {
text: ["pdf"],
},
to: {
text: [
"jpeg",
"png",
"tiff",
"eps",
"icc",
"pdf",
"svg",
"ps",
"html",
"text",
],
},
};
export function convert(
filePath: string,
fileType: string,
convertTo: string,
targetPath: string,
// biome-ignore lint/suspicious/noExplicitAny: <explanation>
options?: any,
): Promise<string> {
return new Promise((resolve, reject) => {
const cairoFiles = [
"jpeg",
"png",
"tiff",
"eps",
"icc",
"pdf",
"svg",
"ps",
];
if (cairoFiles.includes(convertTo)) {
const popplerOptions: {
jpegFile?: boolean;
pngFile?: boolean;
tiffFile?: boolean;
epsFile?: boolean;
iccFile?: boolean;
pdfFile?: boolean;
svgFile?: boolean;
psFile?: boolean;
} = {};
switch (convertTo) {
case "jpeg":
popplerOptions.jpegFile = true;
break;
case "png":
popplerOptions.pngFile = true;
break;
case "tiff":
popplerOptions.tiffFile = true;
break;
case "eps":
popplerOptions.epsFile = true;
break;
case "icc":
popplerOptions.iccFile = true;
break;
case "pdf":
popplerOptions.pdfFile = true;
break;
case "svg":
popplerOptions.svgFile = true;
break;
case "ps":
popplerOptions.psFile = true;
break;
default:
reject(`Invalid convertTo option: ${convertTo}`);
}
poppler
.pdfToCairo(filePath, targetPath, popplerOptions)
.then(() => {
resolve("success");
})
.catch((err: Error) => {
reject(err);
});
} else if (convertTo === "html") {
poppler
.pdfToHtml(filePath, targetPath)
.then(() => {
resolve("success");
})
.catch((err: Error) => {
reject(err);
});
} else if (convertTo === "text") {
poppler
.pdfToText(filePath, targetPath)
.then(() => {
resolve("success");
})
.catch((err: Error) => {
reject(err);
});
} else {
reject(`Invalid convertTo option: ${convertTo}`);
}
});
}

View File

@@ -25,6 +25,8 @@ export const normalizeOutputFiletype = (filetype: string): string => {
return "tex";
case "markdown":
return "md";
case "text":
return "txt";
default:
return lowercaseFiletype;
}