3

I'm creating ionic 4 angular app ,and using file ,file opener ,file transfer , document viewer to open pdf in mobile devices . Now i want to open pdf in web browser ? Below my code for open 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);
  }
 }

2 Answers 2

1

One option is to use IAB / InAppBrowser for this.

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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. I used https://ionicframework.com/docs/native/in-app-browser capacitor plugin.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.