Arkts加载网页url的pdf发票黑屏问题

使用模拟器会闪退,连接真机预览正常

TypeScript 复制代码
import { HMRouter, HMRouterMgr, HMParamType, HMPageParam } from "@hadss/hmrouter";
import { BaseTitle, RouterConstants } from "commonlib";
import { webview } from '@kit.ArkWeb';
import { PDFView } from '@hjm/pdfview'
import { ImagePreview } from "@rv/image-preview";

/*发票PDF查看界面*/
@HMRouter({ pageUrl: RouterConstants.INVOICE_PDF_PAGE})
@ComponentV2
export struct InvoicePDFPage {
  @Local invoiceUrl: string = ''; // 直接存储URL
  private webviewController: WebviewController = new webview.WebviewController()
  private scroller: Scroller = new Scroller()
  aboutToAppear(): void {
    // 获取传递的参数
    let param = HMRouterMgr.getCurrentParam(HMParamType.all);

    // 安全地访问参数
    try {
      const paramStr = JSON.stringify(param);
      console.log('接收到的参数:', paramStr);

      // 使用字符串匹配提取URL
      const match = paramStr.match(/"invoiceUrl"\s*:\s*"([^"]+)"/);
      if (match && match[1]) {
        this.invoiceUrl = match[1];
        // 解码可能的URL转义字符
        try {
          this.invoiceUrl = decodeURIComponent(this.invoiceUrl);
        } catch (e) {
          // 如果解码失败,使用原始URL
        }
        console.log('提取到的发票URL:', this.invoiceUrl);
      } else {
        console.log('未找到invoiceUrl字段');
      }
    } catch (e) {
      console.log('参数处理失败:', e);
    }
  }

  build() {
    Column() {
      BaseTitle({
        showBackBtn: true,
        customTitle: this.titleBuilder,
        bgColor: $r('app.color.title_primary_bg'),
        onBack: () => {
          HMRouterMgr.pop()
        }
      })
      Scroll(this.scroller){
        Column(){
          if (this.invoiceUrl && this.invoiceUrl.length > 0) {
            ImagePreview() {
              PDFView({
                /* 数据源 */
                pdfUrl: this.invoiceUrl,
              })
            }
            // 不使用任何可能不存在的额外属性
          } else {
            Column() {
              Text("发票链接无效")
                .fontSize($r('app.float.16_font'))
                .fontColor($r('app.color.tips_font'))
            }
            .width('100%')
            .height('100%')
            .justifyContent(FlexAlign.Center)
          }
        }
        }
      }

    .width('100%')
    .height('100%')
  }

  @Builder
  titleBuilder() {
    Row() {
      Text($r('app.string.invoice_pdf_title'))
        .fontSize($r('app.float.20_font'))
        .fontColor($r('app.color.title_primary_font'))
    }
    .width('100%')
    .justifyContent(FlexAlign.Center)
  }
}
相关推荐
笔触狂放1 小时前
【项目】基于ArkTS的老年人智能应用开发(1)
harmonyos·arkts·鸿蒙
_waylau7 小时前
历时三年《鸿蒙系统(HarmonyOS)移动开发实战》简介
华为·harmonyos·鸿蒙·鸿蒙系统
梦想不只是梦与想1 天前
鸿蒙 应用市场更新功能:版本检测与更新提醒
harmonyos·鸿蒙·版本更新
UnicornDev1 天前
【HarmonyOS 6】底部悬浮导航的沉浸光感适配(API23)
华为·harmonyos·arkts·鸿蒙·鸿蒙系统
三声三视2 天前
ArkTS 性能优化实战:从卡顿分析到高帧率应用全攻略
华为·性能优化·harmonyos·鸿蒙
小成Coder4 天前
【Jack实战】如何给《时光旅记》接入跨设备拍照和跨设备相册导入
华为·harmonyos·鸿蒙·码上创新
三声三视5 天前
ArkTS Navigation 路由实战:从 Router 迁移到 NavPathStack,打造企业级路由体系
华为·harmonyos·鸿蒙
_waylau5 天前
HarmonyOS 6.0升级至6.1改动点
华为·ai·harmonyos·鸿蒙·鸿蒙系统
仓颉编程语言5 天前
Cangjie 1.1.0 正式发布,支持 Android/iOS 跨平台运行,中心仓正式上线 | STS Beta 测试活动获奖名单公示
华为·鸿蒙·仓颉编程语言
UnicornDev6 天前
【HarmonyOS 6】设置页面 UI 设计
ui·华为·harmonyos·arkts·鸿蒙