Unable to view pdf in android app


#1

Hello,

I am a beginner in Ionic (have previously done a lot of development in Android native). Now I am building a ionic application which displays a pdf file. It works fine when I run ionic serve and ionic cordova run android.

The problem is, when I do ionic cordova build android and then install the APK on my device, the PDF doesn’t show up. From log cat I got this:

Any help will be appreciated. Thanks


#2

Maybe you could use the document viewer?


#3

I tried it, but it was not working for me. I didn’t found a working example for it.

Can I know why the current code is not working?

Thanks


#4

Maybe you can give us an example of your code? Also are you 100% sure the url to the file is correct?


#5

I am 100 % sure because it is working fine with ionic serve. Here is my code:

  <pdf-viewer [src]="displayData.pdfSource" [show-all]="false" [page]="page" [original-size]="false" [zoom]="1.0" style="display: block"
    (after-load-complete)="afterLoadComplete($event)"></pdf-viewer>
    <div *ngIf="isLoaded" class="browse">
      <button (click)="prevPage()" [disabled]="page === 1">Prev</button>
      <span (click)="showModal()">{{ page }} / {{ totalPages }}</span>
      <button (click)="nextPage()" [disabled]="page === totalPages">Next</button>
    </div>
  displayData: any = {
    pdfSource: {
      url: '../assets/myFile.pdf'
    }
  };


#6

Okay, but that it’s working with ionic serve does not mean it works on the device. So I’m pretty sure you shouldn’t be 100% sure ;-). Same is for images, url paths are different on the browser then they’re on your device.

As you can see in your log it’s looking in this folder: android_assets/assets/myFile.pdf . That’s definitely not the correct path.


#7

android_assets/assets/myFile.pdf

This path is not there in my src folder. Also,

Okay, but that it’s working with ionic serve does not mean it works on the device. So I’m pretty sure you shouldn’t be 100% sure :wink:

can you guide me regarding this? I am absolute beginner in Ionic. Thanks for your help


#8

Every time you’re building to the device, it isn’t just the source folder being copied over. Actually it’s generating a build from the src folder. It’s ending up in www/build or in case of your assets in www/assets . So probably working with a path like assets/myFile.pdf should do the trick. Just see what your log show you then.


#9

Ok. Thanks for your help. I’ll try.

BTW, can you help me with some working example of Document Viewer you linked above?


#10

What did you try and didn’t work with the document viewer? Docs seem pretty straightforward to me, so if you can be more precise about what wasn’t working I can probably help you out!


#11

Thanks.

I changed the url to assets/myFile.pdf from ../assets/myFile.pdf and it works now.


#12

I used it not workings in Android 5