简体   繁体   English

如何使用ionic 4在网络浏览器中打开pdf?

[英]How to open pdf in web browser using ionic 4?

I'm creating ionic 4 angular app ,and using file ,file opener ,file transfer , document viewer to open pdf in mobile devices .我正在创建 ionic 4 angular 应用程序,并使用文件、文件打开器、文件传输、文档查看器在移动设备中打开 pdf。 Now i want to open pdf in web browser ?现在我想在网络浏览器中打开 pdf 吗? Below my code for open 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);
  }
 }

One option is to use IAB / InAppBrowser for this.一种选择是为此使用 IAB / InAppBrowser

You can use this line of code:您可以使用这行代码:

this.iab.create("https://www.example.com/some.pdf", '_system', 'location=yes');

It's designed to let you open the system browser and other things on devices, but on desktop it will just open the link in a new tab on a browser.它旨在让您在设备上打开系统浏览器和其他内容,但在桌面上,它只会在浏览器的新选项卡中打开链接。

I assume you can look up the platform code to detect desktop platform that would need wrapping around this.我假设您可以查找platform代码来检测需要围绕它的桌面平台。

I found solution and this work for me:我找到了解决方案,这对我有用:

public openPDF() {
  this.iab.create('assets/docs/doc.pdf', '_system', 'location=yes');
}

This code simple open your local pdf file on default device browser.此代码简单地在默认设备浏览器上打开您的本地 pdf 文件。 I used https://ionicframework.com/docs/native/in-app-browser capacitor plugin.我使用了https://ionicframework.com/docs/native/in-app-browser电容器插件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM