HarmonyOS Web组件打开pdf的页面

HarmonyOS
2024-12-17 14:09:16
950浏览
收藏 0
回答 1
回答 1
按赞同
/
按时间
fox280
import { http } from '@kit.NetworkKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { common } from '@kit.AbilityKit';
import fs, { ReadOptions, WriteOptions } from '@ohos.file.fs';
import { filePreview } from '@kit.PreviewKit';
import { pdfService } from '@kit.PDFKit';
import web_webview from '@ohos.web.webview'
import { promptAction } from '@kit.ArkUI';

@Entry
@Component
struct Index {
  context = getContext(this) as common.UIAbilityContext;
  controller: web_webview.WebviewController = new web_webview.WebviewController();

  aboutToAppear(): void {

  }

  build() {

    Column() {
      Button("下载pdf").onClick(() => {
        // 每一个httpRequest对应一个HTTP请求任务,不可复用
        let httpRequest = http.createHttp();
        httpRequest.requestInStream('xxx.pdf', {
          extraData: http.HttpDataType.ARRAY_BUFFER
        })

        // 用于订阅HTTP流式响应数据接收事件
        let res = new ArrayBuffer(0);
        httpRequest.on('dataReceive', (data: ArrayBuffer) => {
          const newRes = new ArrayBuffer(res.byteLength + data.byteLength);
          const resView = new Uint8Array(newRes);
          resView.set(new Uint8Array(res));
          resView.set(new Uint8Array(data), res.byteLength);
          res = newRes;
          console.info('res length: ' + res.byteLength);
        });
        // 用于订阅HTTP流式响应数据接收完毕事件
        httpRequest.on('dataEnd', () => {
          console.info('No more data in response, data receive end');
          promptAction.showToast({
            message: "下载成功"
          })
          let srcFile = fs.openSync(this.context.filesDir + '/test.pdf', fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
          fs.write(srcFile.fd, res)
        });
      })
      Button("加载pdf").onClick(() => {

        const filePath = `${this.context.filesDir}/test.pdf`
        let document: pdfService.PdfDocument = new pdfService.PdfDocument();
        document.loadDocument(filePath, '', (progress: number) => {
          return progress;
        });
        this.controller.loadUrl(`file://${this.context.filesDir}/test.pdf#scrollbars=0&toolbar=0&statusbar=0`)
      })
      Web({
        src: `www.huawei.com`,
        controller: this.controller
      }).domStorageAccess(true)
    }
  }
}
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.
  • 40.
  • 41.
  • 42.
  • 43.
  • 44.
  • 45.
  • 46.
  • 47.
  • 48.
  • 49.
  • 50.
  • 51.
  • 52.
  • 53.
  • 54.
  • 55.
  • 56.
  • 57.
  • 58.
  • 59.
  • 60.
  • 61.
  • 62.
  • 63.
  • 64.
  • 65.
分享
微博
QQ
微信
回复
2024-12-17 15:52:45
相关问题
HarmonyOS web组件加载pdf问题
1565浏览 • 1回复 待解决
HarmonyOS Web组件加载pdf预览
1247浏览 • 1回复 待解决
HarmonyOS web组件预览PDF文件问题
955浏览 • 1回复 待解决
如何通过web组件预览PDF
1466浏览 • 1回复 待解决
HarmonyOS web组件无法打开
668浏览 • 1回复 待解决
HarmonyOS使用Web组件预览PDF和图片
1647浏览 • 1回复 待解决
HarmonyOS 应用内打开pdf
1376浏览 • 1回复 待解决
HarmonyOS 怎么打开PDF文件?
1024浏览 • 1回复 待解决
使用web组件实现预览沙箱中pdf
2978浏览 • 1回复 待解决
HarmonyOS 如何打开在线PDF
760浏览 • 1回复 待解决
HarmonyOS web组件 加载web页面异常
1268浏览 • 1回复 待解决