Cordova download pdf
Learn More. Cordova Smart Class Software. Demo for Smart Class. The Cordova Today. New Arrivals. My Amazing Book of Science. Pratibha Hindi Pathmala. Revised Glitter. The entry. After spending some hours investigating how to get the correct path, this StackOverflow post led me into the right direction by pointing me to an overhaul of the file plugin, as explained the corresponding issue, CB Platforms which cannot support custom URL protocols are free to return URLs which can be used to access the local file system.
And there you have it : instead of using entry. See that InAppBrowser option comma separated list in the window. To install this plugin use :. If you don't know anything about this plugin, read more about how it works in the official repository here. In order to write the PDF base64 string as a file on the device, we'll use the following two methods :. The cordova file writer provided by the cordova-file-plugin doesn't support write files with base64 , therefore we are going to use a little trick.
The base64 string will be processed and converted into a writable Blob. The method is well explained in the snippet, to test it you can test the following code in your project, it should create a hello world PDF in your root directory. As we know the mime type of a PDF, we don't need to retrieve from the entire base64 string, therefore just proceed to use the method and save it :.
The third link, the InAppBrowser one? Returns nothing. I kid you not. The new window opens, and nothing loads. I get zip in the console as well. Or so I thought. Returning back to Chrome's device window shows that it did load as a new web view:.
But that console has nothing in it. I can't even execute JavaScript in the console. It's like the Phantom Zone of debugging. Ok - so after a bit of searching, I found someone recommending using the download attribute.
This is a newish HTML5 feature that tells the browser that it should download the asset instead of trying to render it. This also did nothing. No error, zip. In case you're curious, iOS ignored the download attribute and just responded like it did with the first link. It turns out that the Android web view simply doesn't support PDFs. That seems One could argue that they probably don't fit the mobile form factor very well, but I'd have assumed that showing something would be better than nothing.
They probably have enough money for that. So - I did some Googling around, and asking on Slack, and Simon Prickett shared some things that worked for him. One of them in particular looked interesting, cordova-plugin-file-opener2. This plugin tries to open a file in a local viewer. It seemed easy enough so I decided to try iOS again.
I added a new button and used this code after adding the plugin and the File plugin :. And it seemed to work fine. I did some digging on the plugin's GitHub issues though and ran across this report: Opening local file pdf : "not found". If you read down the thread a bit, you run into a really nice solution by japostigo-atsistemas. I modified his code a bit to work with my solution and came up with this:. As you can see, he is using the FileSystem to copy to an external data directory and then uses the plugin to load it from there.
A royal pain in the ass, but it works I believe. I ended up with:. Which implies that it simply couldn't find a PDF viewer on my Android simulator. I've got a device, but I'm at an airport currently and the device is back home. Considering multiple people up voted the idea, it seems like a good solution. At this point - I consider the issue solved - roughly - and hopefully this will be of help to others. Thanks again to Simon for his help with this plugin! If you like this content, please consider supporting me.
0コメント