Skip to content

Instantly share code, notes, and snippets.

@ledunguit
Created November 5, 2023 06:57
Show Gist options
  • Select an option

  • Save ledunguit/c70d81a94dee717fb7f0700feef357ef to your computer and use it in GitHub Desktop.

Select an option

Save ledunguit/c70d81a94dee717fb7f0700feef357ef to your computer and use it in GitHub Desktop.

Revisions

  1. ledunguit created this gist Nov 5, 2023.
    55 changes: 55 additions & 0 deletions gistfile1.txt
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,55 @@
    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);
    // });
    // });
    });
    });
    });