我对整个Ionic和Cordova环境都很陌生,我在尝试使用默认的原生pdf阅读器从Ionic应用程序打开.pdf文件时遇到了一些麻烦。
这是我的标记:
<ion-scroll overflow-scroll="true" scrollX="true">
<ion-card>
<ion-card-content>
<a href="assets/sheets/clicks_bs.pdf">
<img src="assets/sheets/clicks_bs.jpg"/>
</a>
</ion-card-content>
</ion-card>
<ion-card>
<ion-card-content>
<a href="assets/sheets/makro.pdf">
<img src="assets/sheets/makro.png" />
</a>
</ion-card-content>
</ion-card>
<ion-card>
<ion-card-content>
<a href="assets/sheets/picknpay_bs.pdf">
<img src="assets/sheets/picknpay_bs.jpg"/>
</a>
</ion-card-content>
</ion-card>
</ion-scroll>
这是我的组件:
import { Component, ViewChild } from '@angular/core';
import { Slides } from 'ionic-angular';
import { FileOpener } from '@ionic-native/file-opener';
@Component({
selector: 'page-home',
templateUrl: 'home.html',
providers: [FileOpener]
})
export class HomePage {
@ViewChild('storebrands') slides: Slides;
constructor(private fileOpener: FileOpener ) {
let i = 0;
setInterval(() => {
this.goToSlide(i);
if (i === 8)
i = 0;
else
i++;
}, 5000);
this.fileOpener.open('assets/sheets/*.pdf', 'application/pdf')
.then(() => console.log('File is opened'))
.catch(e => console.log('Error openening file', e));
}
}
我已经导入了文件打开器并将其注入构造函数中,并且我已将其添加为提供程序,但是当我尝试在Android中单击图像时,它什么也没做。有人可以提供一些见解,说明为什么它没有在本机设备上的默认.pdf阅读器中打开。
提前致谢!
答案 0 :(得分:0)
使用DocumentViewer插件
showDocument(pdff){
const options: DocumentViewerOptions = {
title: 'My PDF',
openWith: { enabled: true},
bookmarks : {
enabled : true
},
search : {
enabled : true
},
autoClose: {
onPause : true
}
}
const imgeLocation = `${cordova.file.applicationDirectory}www/assets/imgs/${pdff}`;
this.document.viewDocument(imgeLocation, 'application/pdf', options)
}
答案 1 :(得分:0)
DocumentVieweréruim,美国或插件FileOpener2,位于美国,但没有安装AppPadrão的应用程序
Segue minha解决方案
Codigo usando o文件服务e o插件文件打开程序可完成:
import { FileOpener } from '@ionic-native/file-opener';
constructor(
private fileOpener: FileOpener
) {}
private abrirComAppPadrao(arquivoInfo: ArquivoInfo): void {
this.fileService.download(arquivoInfo.urlExterna, arquivoInfo.getNomeArquivoTemp())
.then((fileEntry: FileEntry) => {
this.fileOpener.open(fileEntry.nativeURL, arquivoInfo.mimeType)
.then(this.loadingService.dismissLoading)
.catch(e => this.abrirComoLinkExterno(arquivoInfo, e));
})
.catch(e => this.abrirComoLinkExterno(arquivoInfo, e));
}
通过FileService进行本地下载服务:
import { FileTransfer, FileTransferObject } from '@ionic-native/file-transfer';
constructor(
private transfer: FileTransfer
) {}
download(remoteUrl: string, fileName: string,
storageType: StorageTypeEnum = StorageTypeEnum.TEMPORARY): Promise<any> {
const fileTransfer: FileTransferObject = this.transfer.create();
let targetPath = this.getWhereToSave(storageType);
return fileTransfer.download(remoteUrl, targetPath + fileName);
}
private getWhereToSave(storageType: StorageTypeEnum) {
if (StorageTypeEnum.TEMPORARY == storageType) {
if (this.platform.is('ios')) {
return this.file.tempDirectory;
} else {
return this.file.cacheDirectory;
}
} else if (StorageTypeEnum.PRIVATE == storageType) {
return this.file.dataDirectory;
} else if (StorageTypeEnum.PUBLIC == storageType) {
return this.file.documentsDirectory;
}
}