[英]How to open pdf in web browser using ionic 4?
我正在創建 ionic 4 angular 應用程序,並使用文件、文件打開器、文件傳輸、文檔查看器在移動設備中打開 pdf。 現在我想在網絡瀏覽器中打開 pdf 嗎? 在我打開 pdf 的代碼下面...
// My method for open pdf
openLocalPdf() {
let filePath = this.file.applicationDirectory + 'www/assets';
if (this.platform.is('android')) {
let fakeName = Date.now();
this.file.copyFile(filePath, '5-tools.pdf', this.file.dataDirectory, `${fakeName}.pdf`).then(result => {
this.fileOpener.open(result.nativeURL, 'application/pdf')
.then(() => console.log('File is opened'))
.catch(e => console.log('Error opening file', e));
})
} else {
// Use Document viewer for iOS for a better UI
const options: DocumentViewerOptions = {
title: 'My PDF'
}
this.document.viewDocument(`${filePath}/5-tools.pdf`,
'application/pdf', options);
}
}
一種選擇是為此使用 IAB / InAppBrowser 。
您可以使用這行代碼:
this.iab.create("https://www.example.com/some.pdf", '_system', 'location=yes');
它旨在讓您在設備上打開系統瀏覽器和其他內容,但在桌面上,它只會在瀏覽器的新選項卡中打開鏈接。
我假設您可以查找platform
代碼來檢測需要圍繞它的桌面平台。
我找到了解決方案,這對我有用:
public openPDF() {
this.iab.create('assets/docs/doc.pdf', '_system', 'location=yes');
}
此代碼簡單地在默認設備瀏覽器上打開您的本地 pdf 文件。 我使用了https://ionicframework.com/docs/native/in-app-browser電容器插件。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.