How do I display a PDF in Rev?

There are occassions when you might want to be able to display a PDF document in your Rev application, for example a help file. In this lesson we will look at a different ways you can do this.

Launching a PDF in an external viewer

Launching a PDF in an external viewer

The simplest way to display a PDF from within Rev is to use the launch document command. This opens a document with the operating system's default program.

launch document "Getting Started with Revolution.pdf"

Displaying a PDF within a Rev stack

Displaying a PDF within a Rev stack

But what if you don't want to display your PDF in a external window? You can use a revBrowser object to display PDFs within a stack.

To see how to do this we will create a new stack called "PDF Viewer", add an image area called "browser image" which will be used to define the area of the revBrowser object and a button that allows us to select the PDF file we want to view.

Initializing the revBrowser

Initializing the revBrowser

Next we need to initialize the revBrowser object. Add the following to the card script. This will initialize the revBrowser object when the card is opened. In this case we do not pass a URL to the revBrowserOpen function so nothing is displayed yet.

global sBrowserId

on openCard

browserOpen

end openCard

on browserOpen

-- We pass the windowId of the stack to revBrowser

-- so that it can determine which window to place the

-- browser object into

put the windowid of this stack into tWinID

 

-- Open the browser, using the windowId and initial url

put revBrowserOpen(tWinID,"") into sBrowserId

-- Set some basic properties for the browser

revBrowserSet sBrowserId, "showborder","true"

revBrowserSet sBrowserId, "rect",rect of image "browser image"

end browserOpen

on browserClose

revBrowserClose sBrowserId

end browserClose

Selecting a PDF

Now lets script the "Browse" button so that the user can select a PDF to view. We use the answer file with type command to display only PDF files.

global sBrowserId

on mouseUp

local tFile

 

answer file "Please choose the file you would like to display" with type "PDF document|pdf|PDF"

if it is not empty then

put it into tFile

browserSetURL tFile

end if

end mouseUp

the browserSetURL handler is implemented in the card script

on browserSetURL pURL

## Set the url to be displayed to the given url

revBrowserSet sBrowserId, "url", pURL

end browserSetURL

 

 

Displaying the PDF

Displaying the PDF

Now try selecting a PDF file.

Advanced Options

You can do a lot more with revBrowser that we have discussed in this lesson, for more information have a look at the revBrowser functions and commands in the Dictionary.

For more examples of what you can do with revBrowser there is a Browser Sampler stack in the Resources/Examples folder of your Rev distribution.

You might also find the  Explore LiveCode: The Internet tutorial interesting.

21 Comments

Bernd Niggemann

if a filename has a space in the document name then revBrowser will silently fail. Replacing space with %20 cures this.

replace " " with "%20" in tMyFilePath

openworld

I'm looking in the Livecode 4.5 that I recently purchased (I'm using Mac OSX).

There's no sign of the Browser sampler stack!

I am eager to include an interactive pane in my LiveCode apps for web pages to display.

How to do this?

Best

Mark

Elanor Buchanan

Hi Mark

Thanks for bringing this to our attention. We will be adding an "Open Resources" option to LiveCode in a future version which will provide access to the sample stacks.

The Browser Sampler stack is still available, it is part of the application bundle now. To open it ctrl-click on the LiveCode 4.5 application and choose Show Package Contents. Within the Contents/Tools/Resources/Examples folder you will find Browser Sampler.rev.

I hope that helps.

Kind regards

Elanor

Michael Lew

The link "Exploring Rev: The Internet" is broken (404).

Elanor Buchanan

Hi Michael

Thanks for letting us know, I have corrected the link.

Kind regards

Elanor

Mike Williams

Does this work with iOS and Android mobile apps?

Hanson Schmidt-Cornelius

Hi Mike,

revBrowser is only supported on desktop platforms. You can view PDF files from the Internet on iOS using Browser Control. Check out this lesson on how to do it: http://lessons.runrev.com/s/lessons/m/4069/l/22836-How-do-I-use-the-Browser-Control-

Kind Regards,

Hanson

Matthias Rebbe

Hi,

your example does not work.

correct me if i am wrong, but shouldn´t be sBrowserID global? Otherwise sBrowserID in the MouseUp handler has no value, right?

If had replace "local sBrowserID" with "global sBrowserID" in the card script and had to add "global sBrowserID" to the button script, to get your example working.

Regards,

Matthias

Hanson Schmidt-Cornelius

Hi Matthias,

thank you very much for your comment on this lesson.

The lesson may provide opportunity for some misinterpretation with regard to the "Button" code. The lesson does not specify where the code for the "Browse" button script is to be placed.

Intuition does suggest that this should be placed in the script of the button, as you suggest. In which case you are right, and you would have to add a global variable.

If you add the code for the "Browse" button to the card script, then there is no need to make any modifications to the code.

Kind Regards,

Hanson

Tim Jones

I don't believe this works at all on OSX. The forums would suggest it makes no difference if Preview or Adobe Reader is the default viewer, it simply doesn't work. It would be nice to see this officially recognised by the developers as this tutorial incorrectly suggests this to be a cross-platform feature.

Hanson Schmidt-Cornelius

Hi Tim,

thank you very much for your lesson comment.

I tested the lesson on Mac OSX 10.7.3 today and was able to display a PDF file in the browser object. I did notice that "sBrowserId" was not included in the scope of the button and added this as a global variable. The lesson has been updated accordingly.

If this does not address the problem, then please let me know what exactly is not working and I will look into it further.

Kind Regards,

Hanson

Jessamy Goddard

Dear Elanor and Hanson,

Thankyou so much for your tutorials - they are such a help to beginners like myself.

Please could you tell me how to open a browser within a program. I want to direct the user to a wiki page while being able to return back to my program after.

I appreciate your help!!
Jessamy.

Hanson Schmidt-Cornelius

Hi Jessamy,

why do you not include a browser in your LiveCode application? That way people would not have to leave your program at all.

If you are developing a program that is running on a mobile device, then have a look at this lesson: http://lessons.runrev.com/s/lessons/m/4069/l/22836

If you are using revBrowser in a desktop application, then you can use the framework of this lesson and tweak it a bit. Have a look at the dictionary entry for "revBrowserSet". There you will find an example for setting an URL for a webpage, rather than a file.

Kind Regards,

Hanson

Jessamy Goddard

Dear Hanson,

This worked - thank you very much. I now have an internal browser with a back button to return to the app.

Thankyou so much for all your help.

Jessamy Goddard.

trevix

This does not work (Windows 7 Livecode 5.5.3)
revBrowserSet result is empty but the screen remain the same as after the openCard.
Trevix

Hanson Schmidt-Cornelius

Hi Trevix,

strange, I tested this stack on Windows 7 with LiveCode 5.5.3, and it produced the expected results.

Were you able to verify that "openCard" and "revBrowserSet" were executed, possibly by setting breakpoints on these lines and then running the stack?
"openCard" has to be called before you can load a pdf file via the button.
Possibly also try the stack with some PDF files from different sources, just in case the PDF file is corrupted.

Kind Regards,

Hanson

Ray Horsley

I don't believe I see anything documenting how to display page 2, page 3, etc. of a PDF file. Ultimately I'd like to be able to convert a single PDF to multiple PNG's or JPEG's. Any suggestions?

Hanson Schmidt-Cornelius

Hi Ray,

I can't think of anything in LiveCode that would do exactly what you are trying to do. You may be able to do this by calling command line applications that do the conversion for you.

Kind Regards,

Hanson

Greg

when I try to execute on Mac OSX 10.8.3 I get the following "execution error at line n/a (external handler:exception) near "unknown browser id"". What did I do wrong?

Kevin Paszalek

I can't get this to work on OSX 10.8.3. I only get the browser rect filled with black, nor nothing.
Any suggestions? I have copied and pasted from the lesson.
Any suggestions?
Thanks very much!
Kevin

Hanson Schmidt-Cornelius

Hi Greg and Kevin,

there have been licensing issues with revBrowser, since we introduced the open source version of LiveCode. The problem should be addressed as of LiveCode 6.0.2.

In particular, I would expect to get messages such as "unknown browser id" or nothing appearing at all.

Kind Regards,

Hanson

Add your comment

E-Mail me when someone replies to this comment