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)
  }
}
相关推荐
2301_822703209 小时前
渐变壁纸生成:基于鸿蒙Flutter的跨平台壁纸创建工具
flutter·华为·harmonyos·鸿蒙
2301_8227032013 小时前
开源鸿蒙跨平台Flutter开发:幼儿疫苗全生命周期追踪系统:基于 Flutter 的免疫接种档案与状态机设计
算法·flutter·华为·开源·harmonyos·鸿蒙
2301_8227032013 小时前
鸿蒙flutter三方库实战——教育与学习平台:Flutter Markdown
学习·算法·flutter·华为·harmonyos·鸿蒙
2301_8227032016 小时前
开源鸿蒙跨平台Flutter开发:蛋白质序列特征提取:氨基酸组成与理化性质计算
flutter·华为·开源·harmonyos·鸿蒙
钛态16 小时前
Flutter 三方库 ethereum_addresses 的鸿蒙化适配指南 - 掌控区块链地址资产、精密校验治理实战、鸿蒙级 Web3 专家
flutter·harmonyos·鸿蒙·openharmony·ethereum_addresses
Industio_触觉智能17 小时前
触觉智能Purple Pi OH开发板已适配OpenHarmony6.1,将作为LTS长期支持版,附API参考说明
鸿蒙·鸿蒙系统·openharmony·lts·开源鸿蒙·鸿蒙开发板·openharmony6.1
2301_8227032019 小时前
鸿蒙Flutter三方库适配:Flutter Markdown适配实战-鸿蒙平台的Markdown渲染解决方案
flutter·华为·信息可视化·开源·harmonyos·鸿蒙·三方库
_waylau20 小时前
鸿蒙架构师修炼之道-B/S与C/S架构
华为·架构·harmonyos·鸿蒙·鸿蒙系统
雷帝木木20 小时前
Flutter 组件 http_interop 的适配 鸿蒙Harmony 深度进阶 - 驾驭多级拦截器链、实现鸿蒙端标准化通讯审计与流量路由中继方案
flutter·harmonyos·鸿蒙·openharmony·http_interop
2301_8227032020 小时前
鸿蒙Flutter第三方库FlutterUnit组件百科适配与具体功能演示
flutter·华为·开源·harmonyos·鸿蒙