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)
  }
}
相关推荐
森之鸟3 天前
DevEco Studio 6.0.0 元服务页面跳转失败
鸿蒙
Quarkn3 天前
鸿蒙原生应用ArkUI之自定义List下拉刷新动效
list·harmonyos·arkts·鸿蒙·arkui
HarmonyOS小助手6 天前
【上新啦】HarmonyOS官方模板优秀案例 (第2期:新闻行业 · 综合新闻)
harmonyos·鸿蒙·鸿蒙生态
humors2217 天前
鸿蒙示例代码使用心得
华为·实战·harmonyos·鸿蒙·项目·huawei·实操
九丘教育8 天前
【华为仓颉编程语言】运行第一个仓颉程序
华为·鸿蒙·仓颉编程语言·仓颉·仓颉语言
长弓三石9 天前
鸿蒙网络编程系列61-仓颉版基于TCP实现最简单的HTTP服务器
网络·harmonyos·鸿蒙·仓颉
HarmonyOS小助手13 天前
餐饮服务与软件创新的融合:解析海底捞 APP 的 Flutter 鸿蒙开发之路
harmonyos·鸿蒙·鸿蒙生态
鸿蒙开发工程师—阿辉17 天前
HarmonyOS 应用拉起系列(一):应用与元服务互通方式
华为·harmonyos·arkts·鸿蒙
俺不理解18 天前
纯血鸿蒙 AudioRenderer+AudioCapturer+RingBuffer 实现麦克风采集+发声
音频·鸿蒙·录音·播放·纯血鸿蒙