Skip to content

Instantly share code, notes, and snippets.

@ledunguit
Created November 5, 2023 06:57
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ledunguit/c70d81a94dee717fb7f0700feef357ef to your computer and use it in GitHub Desktop.
Save ledunguit/c70d81a94dee717fb7f0700feef357ef to your computer and use it in GitHub Desktop.
Tauri read pdf and extract images
readBinaryFile(event.payload[0]).then(async (data) => {
pdfjsLib.getDocument(data.buffer).promise.then((pdf) => {
pdf.getPage(1).then((page) => {
page.getOperatorList().then((ops) => {
const fns = ops.fnArray;
const args = ops.argsArray;
const validObjectTypes = [
pdfjsLib.OPS.paintImageXObject, // 85
pdfjsLib.OPS.paintImageXObjectRepeat, // 88,
];
args.forEach((arg, i) => {
if (!validObjectTypes.includes(fns[i])) {
return;
}
page.objs.get(arg[0], async (image: any) => {
const imageUnit8Array = image.data;
const imageWidth = image.width;
const imageHeight = image.height;
const imageUint8ArrayWithAlphaChanel = addAlphaChannelToUnit8ClampedArray(imageUnit8Array, imageWidth, imageHeight);
const imageData = new ImageData(imageUint8ArrayWithAlphaChanel, imageWidth, imageHeight);
const canvas = document.createElement('canvas');
const ctx = canvas.getContext('2d');
canvas.width = imageWidth;
canvas.height = imageHeight;
if (ctx) {
ctx.putImageData(imageData, 0, 0);
const data = ctx.canvas.toDataURL();
setImgList((imgList) => [...imgList, data]);
}
});
});
});
//
// const scale = 1;
// const viewport = page.getViewport({ scale });
//
// page.getOperatorList().then((opList) => {
// const svgGfx = new pdfjsLib.SVGGraphics(page.commonObjs, page.objs);
//
// svgGfx.getSVG(opList, viewport).then((svg) => {
// // @ts-ignore
// document.body.appendChild(svg);
// });
// });
});
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment