After upgrading from Pdfjs 7.1.2 to 7.3.0, setCustomDrawHandler is creating problem as 'Invalid Pdf Structure'

PDF.js Express Version
7.3.0

Detailed description of issue
I have created a custom draw handler for using a Perimeter tool for just 2 point click draw. And another custom handler for drawing perimeter annotations with arc ends joining the edges of the 2 lines like this -


As soon as I call setCustomDrawHandler while drawing Perimeter annotation, webveiwer throws this error - INVALID PDF STRUCTURE

Code snippet of following - SAME GIVEN BELOW.

const { Annotations, docViewer } = this.viewerInstance;
Annotations.setCustomDrawHandler(Annotations.PolylineAnnotation, function(
ctx,
pageMatrix,
rotation,
{ annotation, originalDraw }
) {
originalDraw(ctx, pageMatrix);
const paths = annotation.getPath();
if (paths.length === 3) {
const tool = docViewer.getTool(AnnotationTools.PERIMETER);
const ke = new KeyboardEvent(‘keydown’, {
bubbles: true, cancelable: true
});
tool.mouseDoubleClick(ke);
}
const { StrokeThickness, StrokeColor } = annotation;
for (let i = 1; i < paths.length - 1; i++) {
const { x, y } = paths[i];
ctx.beginPath();
ctx.arc(x, y, StrokeThickness * 3, 0, 2 * Math.PI, false);
ctx.fillStyle = StrokeColor.toHexString();
ctx.fill();
}
});

Also Tried this -

Expected behaviour
Earlier it was working properly in 7.1.2.

Does your issue happen with every document, or just one?
Every Document

Code snippet
const { Annotations, docViewer } = this.viewerInstance;
Annotations.setCustomDrawHandler(Annotations.PolylineAnnotation, function(
ctx,
pageMatrix,
rotation,
{ annotation, originalDraw }
) {
originalDraw(ctx, pageMatrix);
const paths = annotation.getPath();
if (paths.length === 3) {
const tool = docViewer.getTool(AnnotationTools.PERIMETER);
const ke = new KeyboardEvent('keydown', {
bubbles: true, cancelable: true
});
tool.mouseDoubleClick(ke);
}
});

Please let us know its solution ASAP.

Hey there,

I can reproduce this issue. I will try and get a patch out today.

Thanks!

Hey @kiran.patil, looks like there is a regression with this function that I need to figure out.

As a quick workaround, you can pass a third parameter to setCustomDrawHandler like this:

Annotations.setCustomDrawHandler(
    Annotations.PolylineAnnotation,
    function (ctx, pageMatrix, rotation, { annotation, originalDraw }) {
         // draw code here
    },
    {
      generateAppearance: false
    }
  );

This is fixed in 7.3.1

1 Like