0

我正在使用 Ionic 4、Capacitor 和 pdfMake 生成 pdf 并在 android 模拟器上打开。由于电容器文件写入不支持 blob,因此我将其作为 base64 发送。

注意:我尝试使用 Cordova 插件保存为 blob,但它不能与电容器一起正常工作。此外,我的解决方案在仅限 Cordova 的项目中运行良好,但我需要使用电容器。

但是,当我在设备上打开 pdf 时,它说它的格式无效。 有什么想法我在这里做错了吗?

if (this.plt.is('cordova')) {
  this.pdfObj.getBase64((data) => {
    this.pdfBase64 = data;
    console.log(this.pdfBase64);
  });


downloadPdf() {
const { Filesystem } = Plugins;

if (this.plt.is('cordova')) {
  console.log('3');
    // Save the PDF to the data Directory of our App
    const fileName = 'defectreport.pdf';
    try {
      Filesystem.writeFile({
        path: fileName,
        data: this.pdfBase64,
        directory: FilesystemDirectory.Data,
        encoding: FilesystemEncoding.ASCII
      }).then((writeFileResult) => {
        console.log('File Written');
        Filesystem.getUri({
            directory: FilesystemDirectory.Data,
            path: fileName
        }).then((getUriResult) => {
            console.log(getUriResult);
            const path = getUriResult.uri;
            this.fileOpener.open(path, 'application/pdf')
            .then(() => console.log('File is opened'))
            .catch(error => console.log('Error openening file', error));
        }, (error) => {
            console.log(error);
        });
      });
      console.log('writeFile complete');
    } catch (error) {
      console.error('Unable to write file', error);
    }
4

1 回答 1

0

电容器文件系统的文档具有误导性。如果您将编码参数传递给 Filesystem.writeFile,它假定它是一个纯文本文件。如果您不提供它,则假定为 base64。下面的代码有效。

 Filesystem.writeFile({
        path: fileName,
        data: this.pdfBase64,
        directory: FilesystemDirectory.Documents
于 2019-03-10T20:16:55.377 回答