mirror of
https://github.com/C4illin/ConvertX.git
synced 2025-10-23 04:52:18 +00:00
feat: poppler working for some formats
This commit is contained in:
@@ -12,7 +12,8 @@
|
|||||||
"@elysiajs/html": "^1.0.2",
|
"@elysiajs/html": "^1.0.2",
|
||||||
"@elysiajs/jwt": "^1.0.2",
|
"@elysiajs/jwt": "^1.0.2",
|
||||||
"@elysiajs/static": "^1.0.3",
|
"@elysiajs/static": "^1.0.3",
|
||||||
"elysia": "^1.0.22"
|
"elysia": "^1.0.22",
|
||||||
|
"node-poppler": "^7.2.0"
|
||||||
},
|
},
|
||||||
"module": "src/index.tsx",
|
"module": "src/index.tsx",
|
||||||
"bun-create": {
|
"bun-create": {
|
||||||
|
@@ -20,6 +20,8 @@ import {
|
|||||||
properties as propertiesPdflatex,
|
properties as propertiesPdflatex,
|
||||||
} from "./pdflatex";
|
} from "./pdflatex";
|
||||||
|
|
||||||
|
import { convert as convertPoppler, properties as propertiesPoppler } from "./poppler";
|
||||||
|
|
||||||
import { normalizeFiletype } from "../helpers/normalizeFiletype";
|
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
|
// 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,
|
properties: propertiesPdflatex,
|
||||||
converter: convertPdflatex,
|
converter: convertPdflatex,
|
||||||
},
|
},
|
||||||
|
poppler: {
|
||||||
|
properties: propertiesPoppler,
|
||||||
|
converter: convertPoppler,
|
||||||
|
},
|
||||||
pandoc: {
|
pandoc: {
|
||||||
properties: propertiesPandoc,
|
properties: propertiesPandoc,
|
||||||
converter: convertPandoc,
|
converter: convertPandoc,
|
||||||
|
115
src/converters/poppler.ts
Normal file
115
src/converters/poppler.ts
Normal 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}`);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
@@ -25,6 +25,8 @@ export const normalizeOutputFiletype = (filetype: string): string => {
|
|||||||
return "tex";
|
return "tex";
|
||||||
case "markdown":
|
case "markdown":
|
||||||
return "md";
|
return "md";
|
||||||
|
case "text":
|
||||||
|
return "txt";
|
||||||
default:
|
default:
|
||||||
return lowercaseFiletype;
|
return lowercaseFiletype;
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user