linkService is undefined pdfjs nextjs

Viewed 164

I am currently using pdfjs to show the annotations from a provided pdf but when trying to render, it shows linkService is undefined and i cannot find a way to solve this issue. Here is the piece of code if it might help:

function renderPage(pageNumber: number, canvas: HTMLCanvasElement, loadedPdf: { getPage: (arg0: any) => Promise<any>}){
    loadedPdf.getPage(pageNumber).then(async function (page) {
        viewport = page.getViewport({scale: 1.5});
        if (!!canvas) {
            canvas.height = viewport.height;
            canvas.width = viewport.width;
        }
        const renderContext = {
            canvasContext: canvas.getContext('2d'),
            viewport: viewport
        }
        page.render(renderContext);

        // annotation processing portion
        const view = page.view;
        const annotations = await page.getAnnotations();
        let annotationContainer = document.getElementById("annotation-layer");

        if(!!annotationContainer) {
            canvas.appendChild(annotationContainer);
            annotationContainer.style.left = canvas.offsetLeft + "px";
            annotationContainer.style.top = canvas.offsetTop + "px";
            annotationContainer.style.height = canvas.height + "px";
            annotationContainer.style.width = canvas.width + "px";
        }

        pdfjsLib.AnnotationLayer.render({
            linkService: undefined,
            downloadManager: undefined,
            renderForms: false,
            viewport: viewport.clone({dontFlip: true}),
            div: annotationContainer,
            annotations: annotations,
            page: page
        });

    });
}

Upon looking into the library i need to provide IPDFLinkService for the link service. How to solve this issue? Can someone help?

1 Answers

If you don't mind, you can try using an older version,i.e. install pdfjs-dist version 2.6.347. For the linkService, add pdfjsLib.LinkTarget.BLANK i.e. your annotation layer render now becomes:

        pdfjsLib.AnnotationLayer.render({
            downloadManager: undefined,
            renderInteractiveForms: false,
            viewport: viewport.clone({dontFlip: true}),
            div: annotationContainer,
            annotations: annotations,
            linkService: pdfjsLib.LinkTarget.BLANK,
            page: page
        });
Related