Skip to content

Instantly share code, notes, and snippets.

@lbrenman
Created July 22, 2015 02:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lbrenman/6334dc808e4d7614790b to your computer and use it in GitHub Desktop.
Save lbrenman/6334dc808e4d7614790b to your computer and use it in GitHub Desktop.
Appcelerator Titanium PDF Viewer Demo 2 - If not already copied, then retrieve PDF from resource directory and store in file system and view

#PDF Viewer demo - Application Resource

  1. For Android check if PDF has been copied to SDCard
  2. if not, then copy to SDCard and open
  • use docviewer on ios and exernal app (via intent) on android
  1. if so, then open pdf
  • use docviewer on ios and exernal app (via intent) on android

Note that Android, the file is stored in the externalStorageDirectory (SDCard) so deleting the application will not remove the pdf form the SDCard. Use a File Manager app (pn Android) to delete the file

Remember to make sure you have a PDF viewer installed on your Android device or emulator

function doClick(e) {
//alert($.label.text);
openPDF();
}
function viewPDF(appfilepath) {
if(OS_ANDROID) {
try{
Ti.Android.currentActivity.startActivity(Ti.Android.createIntent({
action: Ti.Android.ACTION_VIEW,
type: 'application/pdf',
data: appfilepath
}));
} catch(e) {
Ti.API.info('error trying to launch activity, e = '+e);
alert('No PDF apps installed!');
}
} else {
docViewer = Ti.UI.iOS.createDocumentViewer({url:appfilepath});
docViewer.show();
}
}
function openPDF() {
var appFile;
if(OS_ANDROID) {
//copy from app directory to SDCard (once)
var originalFile = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, 'map.pdf');
appFile = Ti.Filesystem.getFile(Ti.Filesystem.externalStorageDirectory, 'map.pdf');
if(appFile.exists()===false) {
appFile.write(originalFile.read());
}
} else {
appFile = Ti.Filesystem.getFile(Ti.Filesystem.resourcesDirectory, 'map.pdf');
}
Ti.API.info('appFile = '+appFile);
var appfilepath = appFile.nativePath;
Ti.API.info('appFile.nativePath = '+appFile.nativePath);
viewPDF(appfilepath);
}
$.index.open();
".container": {
backgroundColor:"white"
}
"Label": {
width: Ti.UI.SIZE,
height: Ti.UI.SIZE,
color: "#000"
}
"#label": {
font: {
fontSize: 12
}
}
<Alloy>
<Window class="container">
<Label id="label" onClick="doClick">Open PDF</Label>
</Window>
</Alloy>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment