I need a suggestion/advice on how to solve this. Currently I am building a Web application in PHP 8.2 where users can upload PDF files and only users with certain roles have the ability to see them. This application is also available via Mobile Application, however my team decided that we will use Web View for the listing/displaying the pdf files in the Mobile Application. The Web Application are being built in Kotlin for Android and Swift for iOS. Also, to retrieve the file, the "user with certain role" must provide his JWT token for authentication before the system can retrieve the file from an endpoint.
I would also like to point out that the PDF files are stored in my Web Server, so that the UI can retrieve it easily.
The way we plan to display the PDF file is that, the PDF file are embedded inside the WebView page. So far we have tried multiple methods:
- iframe tag (Using direct path from storage)
- iframe tag (Using Google Docs Viewer)
- PDF.js (JS Library)
For the first method, the PDF didn’t load for Android, however iOS can load it. For the second method, both Android and iOS managed to display it, however I have to risk putting the JWT in the URL because there are no other way to pass the JWT token for authentication. For the third method, the PDF didn’t load at all for both Android and iOS.
I feel like I have reached a dead-end and this feature seems impossible to implement within WebView. For the Developers out there, have you created something similar to this? What method did you guys used to retrieve the PDF File so that it can be readable in WebView Page for both iOS and Android.
2
Answers
we have done the same thing using php and pdfjs only.
the role based user permissions to view specific pdf files only.
please check below code how we did his .
the trick is you have to display content by fetching base64 encoded data of pdf file which are stored in directory above public folder. and file parameter is passed through get parameter
To implement pdf_viewer.php from base64 data refer below link.
https://github.com/mozilla/pdf.js/blob/master/examples/learning/helloworld64.html
In my experience, the webview component requires you to use
<embed></embed>
to render the pdf, such as the following:In the past, we will use google pdf viewer link (as above) to do the job, but recently it has been disabled by google. (but if you can find a valid google pdf viewer (or any other alternative pdf viewer) then you can use it – but the url which I am using in the past has been disabled)
So as an alternative, you need to create a reader first
(1) Create a simple PDF reader (assuming that you put it on http://www.yourdomain.com)
reader.php
(2) Use the following in your webview (assuming the pdf is at https://www.targetdomain.com/xxxx.pdf)
The result will be:
Last but not least, if you use the above method, you must be sure that allow_url_fopen directive is enabled otherwise file_get_contents may fail if the reader and pdf are residing on two different servers