目录

Web组件上PDF的预览下载以及常见问题定位解决

问题现象

如何通过Web组件实现预览和下载PDF文件?遇到PDF预览相关的问题应该如何定位解决?

背景知识

解决方案

常见FAQ

Q:使用Web预览PDF时,如何判断PDF文件是否是最后一页或者滑动到底部?

A:可以通过onPdfScrollAtBottom事件来通知用户PDF页面已滚动到底,参考如下demo:

import { webview } from '@kit.ArkWeb';

@Entry
@Component
struct BottomDemo {
  controller: webview.WebviewController = new webview.WebviewController();

  build() {
    Column() {
      Web({
        src: $rawfile('test.pdf'),
        controller: this.controller
      })
        .domStorageAccess(true)
        .fileAccess(false)
        .geolocationAccess(false)
        .onPdfScrollAtBottom(
          (eventInfo: OnPdfScrollEvent) => {
            console.info(`Scroll at bottom callback called. url: ${eventInfo.url}.`);
          }
        );
    };
  };
}

Q:如何监听Web组件是否加载PDF成功?

A:可以通过onPdfLoadEvent事件通知用户PDF页面加载状态,包括成功或失败,样例demo如下:

import { webview } from '@kit.ArkWeb';

@Entry
@Component
struct LoadEventDemo {
  controller: webview.WebviewController = new webview.WebviewController();

  build() {
    Column() {
      Web({
        src: $rawfile('test.pdf'),
        controller: this.controller
      })
        .domStorageAccess(true)
        .fileAccess(false)
        .geolocationAccess(false)
        .onPdfLoadEvent(
          (eventInfo: OnPdfLoadEvent) => {
            console.info(`Load event callback called. url: ${eventInfo.url}, result: ${eventInfo.result}.`);
          }
        );
    };
  };
}

Q:如何预览base64格式的PDF文件?

A:可以通过data url方式直接加载,例如:

import { webview } from '@kit.ArkWeb';

@Entry
@Component
struct Base64Demo {
  controller: webview.WebviewController = new webview.WebviewController();
  pdfStr: string = 'data:application/pdf;base64,xxx'; // 使用时将xxx替换为PDF的base64格式数据

  build() {
    Column() {
      Web({
        src: this.pdfStr,
        controller: this.controller
      })
        .domStorageAccess(true)
        .geolocationAccess(false)
        .fileAccess(false);
    };
  }
}

Q:如何在预览PDF时显示加载进度条?

A:可以通过Progress组件结合onProgressChange事件来实现,demo如下:

import { webview } from '@kit.ArkWeb';

@Entry
@Component
struct LoadProgressDemo {
  controller: webview.WebviewController = new webview.WebviewController();
  // 创建变量表示进度值
  @State value: number = 0;

  build() {
    Column() {
      Progress({ value: this.value, total: 100, type: ProgressType.Capsule })
        .height('200px')
        .borderRadius(80);
      Web({
        src: $rawfile('test.pdf'),
        controller: this.controller
      })
        .domStorageAccess(true)
        .fileAccess(false)
        .geolocationAccess(false)
        .onProgressChange((event) => {
          if (event) {
            this.value = event.newProgress;
          }
        });
    };
  }
}

Q:Web预览PDF时如何展示标题title?

A:使用Column嵌套Row和Web实现布局,通过onTitleReceive方法获取加载的网页的标题。参考如下demo:

import { webview } from '@kit.ArkWeb';

@Entry
@Component
struct TitleDemo {
  @State title: string = '';
  private webviewController: WebviewController = new webview.WebviewController();

  build() {
    Column() {
      Row() {
        Text(this.title)
          .fontSize('50px')
          .layoutWeight(1)
          .textAlign(TextAlign.Center);
      }
      .width('100%')
      .height(40);

      Web({
        src: 'xxx.com/xxx.pdf', // 运行时替换成真实的地址
        controller: this.webviewController,
      })
        .width('100%')
        .domStorageAccess(true)
        .fileAccess(false)
        .geolocationAccess(false)
        .onTitleReceive((event) => {
          if (event) {
            this.title = event.title;
          }
        });
    };
  }
}