• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

open a pdf with AS3 on AIR app

Contributor ,
Jul 26, 2015 Jul 26, 2015

Copy link to clipboard

Copied

Hi,

I'm trying to open a pdf when the user click on a button, for an AIR app.

Here's my code for that :

perso.addEventListener(MouseEvent.CLICK, print, false, 0, true);

function print(event:MouseEvent):void{

var myPDF = new URLRequest("tout.pdf");

navigateToURL(myPDF);

}

Problem : it's working on the emulator of Adobe flash CC, but when I'm installing the apk on an Android device, it doesn't work. (nothing happen when I click on "perso").

Any idea what could be wrong ?

(Note : I've add the "tout.pdf" to my apk in the publish setting like that :

Sans titre.png)

TOPICS
ActionScript

Views

6.6K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 26, 2015 Jul 26, 2015

Copy link to clipboard

Copied

your android doesn't have a browser plugin pdf reader.

try using the htmlloader class to load your pdf.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Jul 30, 2015 Jul 30, 2015

Copy link to clipboard

Copied

If the PDF is too complex for HTMLLoader you should follow Android guidelines. Opening files of specific types requires, as kglad indicated, an app for doing so. You should use an ANE to invoke an intent to open the PDF and let the system deal with it if you don't have your own PDF rendering system built-in. The intent will bring up what all Android users are familiar with, the screen to choose which app will open the PDF. Often there are several to choose from and it's not uncommon to have something pre-installed, such as Adobe Acrobat or Polaris Office. It entirely depends on the manufacturers choice.

I purchased an ANE previously called DocLauncher, but this was a very long time ago. It nicely came with Java source code so I could modify it and even add more functionality if I desired. Unfortunately the software doesn't appear to be available anymore (the author did not keep up with it), yet the ANE still works for me to this day. I'm fairly sure there are alternate options for you if you search around for an ANE. I just can't give you this ANE because it is commercial. I can tell you the code in it is extremely simple if you want to get your feet wet in Java. You send the PDF full path to the ANE which verifies it exists, creates a context, creates an intent (Intent.ACTION_VIEW), sets the intents setDataAndType(fileURI, mimeType), then starts an Activity activity = theContext.getActivity() and then starts it up activity.startActivity(intent). Yes it's cryptic but that general workflow is basic, albeit dated. Works up to the latest OS5.x and back to, well, way before anyone would still have those old Androids.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 14, 2016 Dec 14, 2016

Copy link to clipboard

Copied

Hey sinious!

I am trying to use ANE native extension to open a pdf file with no sucess.

Could you tell me what's wrong with my code, please?

var pdf:PdfViewer;

btn.addEventListener(MouseEvent.CLICK, abrirpdf, false, 0, true);

function abrirpdf(e:MouseEvent):void

{

  pdf = new PdfViewer();

  pdf.addEventListener(PdfViewerEvent.STATUS, onStatus);

  var src:File = File.applicationDirectory.resolvePath("/pdf/meupdf.pdf");

  var dis:File = File.documentsDirectory.resolvePath(src.name);

  if (dis.exists) dis.deleteFile();

  if (!dis.exists) src.copyTo(dis, true);

  pdf.run(dis);

}

Thank You!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 15, 2016 Dec 15, 2016

Copy link to clipboard

Copied

Problem fixed!

My code wasn't rigth and i haven't inicialize the necessary permissions.

But now, it just dont execute the pdf in pdf software and i cant realize why.

My code is that:

btn.addEventListener(MouseEvent.CLICK, abrirpdf, false, 0, true);

function abrirpdf(e:MouseEvent):void

{

  var src:File = File.applicationDirectory.resolvePath("pdf/meupdf.pdf");

  var dis:File = File.documentsDirectory.resolvePath(src.name);

  if (dis.exists) dis.deleteFile(); else src.copyTo(dis, true);

  pdf = new PdfViewer();

  pdf.addEventListener(PdfViewerEvent.STATUS, onStatus);

  texto.text="trying to open pdf file: " + pdf.run(dis);

}

Everything in running as it should, the "dis" variable has what it must have, but the output is:

"Trying o open pdf file: false";

Why ist that?

Thank you very much!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 15, 2016 Dec 15, 2016

Copy link to clipboard

Copied

Now it works!

I've just used a navigateToUrl pointing to "dis".

But i still have the "ANE Demo" text box appearing in the beginning of my app.

Is there any other method to add the same permissions, without this (ANE) extension?

Thank you!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 15, 2016 Dec 15, 2016

Copy link to clipboard

Copied

why don't you use stagewebview?  (htmlloader doesn't work on mobiles.)

eg,

import flash.media.StageWebView;

var webView: StageWebView = new StageWebView();

webView.stage = this.stage;

webView.viewPort = new Rectangle(0, 0, stage.stageWidth, stage.stageHeight);

webView.loadURL("http://www.orimi.com/pdf-test.pdf");

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 15, 2016 Dec 15, 2016

Copy link to clipboard

Copied

kglad, hey!

I am trying to make it, but still didn't get it:

var src:File = File.applicationDirectory.resolvePath("pdf/meupdf.pdf");

  var dis:File = File.documentsDirectory.resolvePath(src.name);

  var webView: StageWebView = new StageWebView();

  if (dis.exists)

  dis.deleteFile();

  src.copyTo(dis, true);

  webView.stage = this.stage;

  webView.viewPort = new Rectangle(0, 0, stage.stageWidth, stage.stageHeight);

  webView.loadURL("dis.url");

Do you know what could be wrong?

My problem is for being a local file.

There is some issue acessing a file in the application Directory, that's why i have to copy for documents directory,

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 15, 2016 Dec 15, 2016

Copy link to clipboard

Copied

what used to work doesn't appear to work anymore, load PDF file inside the application (Android and Ipad)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
LEGEND ,
Dec 15, 2016 Dec 15, 2016

Copy link to clipboard

Copied

I don't remember it ever working but v3.6 was years ago and I never tested things in the "Adobe Flex" and Flash Builder era. I still use Flash Builder, but I believe this is just Android OS's choice type of handling.

Apps that need to display media simply need libraries to do so, or otherwise launch the document in a new process to the OS and let it put up a chooser to select an app to view that file, like most apps. Unfortunately that's how I've managed PDFs in my Android apps to this day. I wish it was like iOS but I never remember it being like that.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 15, 2016 Dec 15, 2016

Copy link to clipboard

Copied

to be honest, i don't remember either, but i wouldn't have posted that code and a fla (linked in message 10) unless it was working (3+ years ago).

in any case, that's moot now.  i would just use navigateToURL and leave it the user to make sure their browser had pdf-viewing capabilities.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 20, 2016 Dec 20, 2016

Copy link to clipboard

Copied

Hello!

With that code i have no error, but the pdf doesn't show up!

It just creates a blank square:

function abrirpdf(e:MouseEvent):void

{

var swv: StageWebView = new StageWebView();

swv.viewPort = new Rectangle(0, 0, 500, 500); // 500px width, 450px height

swv.stage = this.stage; // set stage of StageWebView to document class

var templateFile:File = File.applicationDirectory.resolvePath("pdf/meupdf.pdf" );

var workingFile:File = File.createTempFile();

templateFile.copyTo(workingFile, true );

try{

    swv.loadURL( workingFile.url );

} catch (e:Error){

    trace( e

);

}}

If i change "applicationDirectory" to "applicationStorageDirectory" it gives an error "file or directory does not exist".

If i write like that, does not occur any error, but the pdf doesn't appear either.

Can you help me, guys?

I would like very much to not depend on a paid extension.

Thank you very much!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 20, 2016 Dec 20, 2016

Copy link to clipboard

Copied

i don't know how to do that any more for android without an ane.  like i suggested, just use navigateToURL.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 21, 2016 Dec 21, 2016

Copy link to clipboard

Copied

Hello and thank you for your help.

I have tried with navigateToURL and i got it, but it only works in windows.

In android smartphone it doesn't work

Can you give me some help, please?

Pdf reader function:

function abrirpdf(e:MouseEvent):void

{

  var src:File = File.applicationDirectory.resolvePath("pdf/meupdf.pdf");

  var dis:File = File.documentsDirectory.resolvePath(src.name);

  src.copyTo(dis, true);

  if (dis) texto.text="Existe dis";

// texto.text="Chega até aqui";

  navigateToURL(new URLRequest(dis.url));

  texto.text="E até aqui";

}

App-app.xml:

<android>

    <manifestAdditions>

  <![CDATA[<manifest android:installLocation="auto">

  <uses-permission android:name="android.permission.WAKE_LOCK" />

  <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

     <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

Thank you

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 21, 2016 Dec 21, 2016

Copy link to clipboard

Copied

add your pdf to a file server and test (without use the File class):

navigateToURL(new URLRequest('path../meuppdf'));

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 22, 2016 Dec 22, 2016

Copy link to clipboard

Copied

I have already tried in computer and in smartphone.

In computer it opens the pdf file.

In smartphone its says that "can't open meupdf.pdf" but it seems to find it!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 22, 2016 Dec 22, 2016

Copy link to clipboard

Copied

are you using a server?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 22, 2016 Dec 22, 2016

Copy link to clipboard

Copied

No, i am using a local file

Could that be the reason of the problem?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 22, 2016 Dec 22, 2016

Copy link to clipboard

Copied

reread message 16.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 22, 2016 Dec 22, 2016

Copy link to clipboard

Copied

what's the url to your pdf and what code are you using in navigateToURL?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 22, 2016 Dec 22, 2016

Copy link to clipboard

Copied

navigateToURL(new URLRequest("pdf/meupdf.pdf"));

I am sorry but i cant find the solution

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 22, 2016 Dec 22, 2016

Copy link to clipboard

Copied

no, upload to a server.  eg,

navigateToURL(new URLRequest("http://www.kglad.com/Files/forums/test.pdf"))

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 22, 2016 Dec 22, 2016

Copy link to clipboard

Copied

This works, but my company does not want the file to be on the web.

That's my problem!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 22, 2016 Dec 22, 2016

Copy link to clipboard

Copied

i don't know any easy way to do that.

you've spent so much time on this why not buy an ane and save your company some money?

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Beginner ,
Dec 22, 2016 Dec 22, 2016

Copy link to clipboard

Copied

Yes, that is what i gotta do.

Thank you for all your effort!

Thanks a lot!

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines