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)
  }
}
相关推荐
Davina_yu2 小时前
数据持久化(2):RelationalStore关系型数据库(SQLite)操作(14)
harmonyos·鸿蒙·鸿蒙系统
Davina_yu4 小时前
环境变量管理:Environment与LocalStorage的应用场景(23)
harmonyos·鸿蒙·鸿蒙系统
轻口味7 小时前
轻规划鸿蒙开发实战11:自研 Haptic Canvas 粒子系统,纯 ArkUI 高性能烟花渲染与性能避
华为·harmonyos·鸿蒙
Davina_yu7 小时前
JSON数据处理:字符串序列化与反序列化实战(20)
harmonyos·鸿蒙·鸿蒙系统
轻口味7 小时前
轻规划鸿蒙开发实战10:分布式数据同步深度博弈,UserId 隔离与并发数据冲突消解机
分布式·华为·harmonyos·鸿蒙
Davina_yu8 小时前
沙箱机制:理解鸿蒙应用的文件访问权限与目录结构(15)
harmonyos·鸿蒙·鸿蒙系统
梦想不只是梦与想1 天前
鸿蒙 消息推送:Push Token的获取(四)
harmonyos·鸿蒙·推送
不羁的木木1 天前
HarmonyOS AI开发提效工具:DevEco Code & DevEco CLI - 跨设备调试与AI应用部署
人工智能·华为·harmonyos·鸿蒙
不羁的木木1 天前
HarmonyOS AI开发提效工具:DevEco Code & DevEco CLI - AOT编译加速AI应用启动
harmonyos·鸿蒙
浮芷.1 天前
鸿蒙 6.1 新特性-60fps流畅人物跳跃功能算法深度解析-鸿蒙PC端正弦值计算法
算法·华为·harmonyos·鸿蒙·鸿蒙系统