processDocument()

in web-app-demo/Frontend/src/app/components/processor-selection/processor-selection.component.ts [164:233]


  processDocument() {
    this.data.changeProcessInProgress(true);
    if (this.fileName == "" || this.file == null) {
      this.data.changeProcessInProgress(false);
      this.data.changeShowError(true);
      this.data.changeErrorMessage("ERROR : PDF was not selected");
      return;
    } else if (this.file.type != "application/pdf") {
      this.data.changeShowError(true);
      this.data.changeErrorMessage(
        "ERROR : File type does not match accepted type (PDF)"
      );
      return;
    }

    const data = new FormData();
    data.append("file", this.file);
    data.append("filename", this.fileName);
    data.append("fileProcessorType", this.processorList[this.processor]);
    data.append("showBounding", String(this.showBounding));

    fetch(this.backend + "api/docai", {
      method: "POST",
      mode: "cors",
      body: data,
    })
      .then(async (response) => {
        const json = await response.json();
        if (
          json["resultStatus"] != undefined &&
          json["resultStatus"] == "ERROR"
        ) {
          throw new Error(json["errorMessage"]);
        } else {
          this.data.changeDocumentProto(json);
          return json;
        }
      })
      .then(() => {
        this.data.changeProcessIsDone(true);
        this.data.changeProcessInProgress(false);
        const canvas = <HTMLCanvasElement>document.getElementById(LAYER2)!;
        const context = canvas.getContext("2d")!;
        const background = new Image();
        background.src =
          "data:image/png;base64," +
          this.documentProto.document.pages[0].image.content;

        // Make sure the image is loaded first otherwise nothing will draw.
        background.onload = () => {
          context.drawImage(
            background,
            0,
            0,
            background.width,
            background.height,
            0,
            0,
            canvas.width,
            canvas.height
          );
          this.drawBoxes(context, canvas);
        };
      })
      .catch((error) => {
        this.data.changeShowError(true);
        this.data.changeErrorMessage(error);
        this.data.changeProcessInProgress(false);
      });
  }