HarmonyOS鸿蒙Next中filePreview预览pdf,不显示pdf内容

HarmonyOS鸿蒙Next中filePreview预览pdf,不显示pdf内容

let uiContext = this.getHostContext() as Context

let pathDir = uiContext.filesDir + '/pdf123.pdf';
let uri = fileUri.getUriFromPath(pathDir);
let fileInfo: filePreview.PreviewInfo = {
  // 该uri仅为参考,需要根据实际业务传入真实uri
  uri: uri,
  mimeType: 'application/pdf'
};
console.info("The uri " + uri);
//file://com.example.myapplication3/data/storage/el2/base/haps/entry/files/pdf123.pdf

filePreview.canPreview(uiContext, uri).then((result) => {    // 文件存在且符合类型时,此处返回true
  console.info(`Succeeded in obtaining the result of whether it can be previewed. result = ${result}`);

  filePreview.openPreview(uiContext, fileInfo).then(() => {
    console.info('Succeeded in opening preview');
  }).catch((err: BusinessError) => {
    console.error(`Failed to open preview, err.code = ${err.code}, err.message = ${err.message}`);
  });

}).catch((err: BusinessError) => {
  console.error(`Failed to obtain the result of whether it can be previewed, err.code = ${err.code}, err.message = ${err.message}`);
})
3 回复

在rawfile下预制文件,然后将rawfile文件导入沙箱中,确保需要预览的文件,沙箱中存在。

通过Preview Kit(文件预览服务)预览沙箱中的文件。 以下以预览pdf文件为例,示例代码:

import { BusinessError } from '@kit.BasicServicesKit';
import { fileUri, fileIo as fs } from '@kit.CoreFileKit';
import { filePreview } from '@kit.PreviewKit';

@Entry
@Component
struct Index {
  private context = this;
  private pdfPath = this.context.filesDir + "/123.pdf"

  aboutToAppear(): void {
    // 将rawfile目录下文件复制到沙箱目录
    this.context.resourceManager.getRawFileContent('123.pdf', (_err, value) => {
      let myBuffer: ArrayBufferLike = value.buffer
      console.log("pdf path:" + this.pdfPath);
      let file = fs.openSync(this.pdfPath, fs.OpenMode.READ_WRITE | fs.OpenMode.CREATE);
      fs.writeSync(file.fd, myBuffer);
      console.info("write file success");
      fs.closeSync(file);
    })
  }

  build() {
    Column() {
      Button('预览pdf')
        .id('stop')
        .fontSize(20)
        .fontWeight(FontWeight.Bold)
        .onClick(() => {
          let uri = fileUri.getUriFromPath(this.pdfPath);
          let fileInfo: filePreview.PreviewInfo = {
            uri: uri,
            mimeType: 'application/pdf'
          };

          filePreview.canPreview(this.context, uri).then((result) => {
            // 文件存在且符合类型时,此处返回true
            console.info(`Succeeded in obtaining the result of whether it can be previewed. result = ${result}`);
            if (result) {
              // 预览文件
              filePreview.openPreview(this.context, fileInfo).then(() => {
                console.info('Succeeded in opening preview');
              }).catch((err: BusinessError) => {
                console.error(`Failed to open preview, err.code = ${err.code}, err.message = ${err.message}`);
              });
            }
          }).catch((err: BusinessError) => {
            console.error(`Failed to obtain the result of whether it can be previewed, err.code = ${err.code}, err.message = ${err.message}`);
          })
        })
    }
    .height('100%')
    .width('100%')
    .alignItems(HorizontalAlign.Center)
    .justifyContent(FlexAlign.Center)
  }
}

更多关于HarmonyOS鸿蒙Next中filePreview预览pdf,不显示pdf内容的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


在HarmonyOS鸿蒙Next中,filePreview组件用于预览文件,包括PDF文件。如果PDF内容无法显示,可能的原因包括:

  1. 文件路径错误:确保提供的文件路径正确,文件存在于指定路径。
  2. 文件格式不支持:虽然filePreview支持PDF,但某些特殊格式或加密的PDF可能无法正常显示。
  3. 权限问题:检查应用是否具有读取文件的权限,确保在config.json中正确配置了权限。
  4. 组件配置错误:检查filePreview组件的配置,确保所有必要参数正确设置。
  5. 系统或API问题:可能是系统或API的bug,建议检查是否有相关更新或补丁。

示例代码:

import filePreview from '@ohos.file.preview';

let filePath = 'path/to/your/file.pdf';
filePreview.preview(filePath).then(() => {
    console.log('PDF preview started');
}).catch((err) => {
    console.error('Failed to preview PDF:', err);
});

如果问题仍然存在,建议检查日志以获取更多错误信息。

在HarmonyOS鸿蒙Next中,如果filePreview无法显示PDF内容,可能的原因包括:

  1. PDF文件损坏或格式不支持;
  2. 缺少必要的PDF渲染库或插件;
  3. 文件路径错误或权限不足。

建议检查文件完整性,确保系统已安装PDF渲染组件,并验证文件路径和权限设置。

回到顶部
AI 助手
你好,我是IT营的 AI 助手
您可以尝试点击下方的快捷入口开启体验!