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)
  }
}
相关推荐
2501_9197490310 小时前
华为鸿蒙记录咖啡APP—小羊咖啡
华为·harmonyos·鸿蒙
2501_9197490312 小时前
华为鸿蒙图片像素工具APP—小羊像素
华为·harmonyos·鸿蒙
Magic-ZYJ2 天前
隐私优先 HarmonyOS 应用怎么设计:无账号、无后台、无统计 SDK
华为·harmonyos·鸿蒙·独立开发者·心晴手记
2501_919749032 天前
华为鸿蒙测手速APP—小羊手速
华为·harmonyos·鸿蒙
Magic-ZYJ2 天前
HarmonyOS 调用系统文件保存器导出 JSON 与 CSV
华为·json·harmonyos·鸿蒙·独立开发者
OH_TPC2 天前
HarmonyOS APP开发---“股动力“实时行情App,需要用到这个库
华为·音视频·harmonyos·鸿蒙
2501_919749033 天前
华为鸿蒙免费排班APP—小羊排班
华为·harmonyos·鸿蒙
世人万千丶3 天前
HarmonyOS 股票 K 线图完整源码与金融图表模板
学习·华为·金融·harmonyos·鸿蒙
woshihuanglaoshi5 天前
错题四科入库:鸿蒙错题本种子数据与复习队列效果
学习·华为·harmonyos·鸿蒙
2501_919749035 天前
华为鸿蒙免费音乐APP—小羊免费音乐
华为·harmonyos·鸿蒙