How do I display a PDF in LiveCode?

There are occasions when you might want to be able to display a PDF document in your LiveCode application, for example a help file. In this lesson we will look at 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 LiveCode is to use the launch document command. This opens a document with the operating system's default program.

launch document "/Users/<username here>/Documents/mydocument.pdf"

Displaying a PDF within a LiveCode 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 browser widget to display PDFs within a stack.

To see how to do this we will create a new stack called "PDF Viewer", add a browser widget and a button that allows us to select the PDF file we want to view. Drag a browser widget onto your stack and resize it, add a button and name the button "Browse". Open the code editor from the toolbar and add this script (don't forget to click "Apply"):

Selecting a PDF

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
      
      // Allow for spaces in filenames
      replace " " with "%20" in tFile
      
      set the url of widget "browser" to tFile
   end if
end mouseUp

Displaying the PDF

Displaying the PDF

Switch to Run mode and try selecting a PDF file.

Displaying with a PDF Widget

As of LiveCode 9.5, PDFs may also be displayed using the PDF widget in place of the browser widget. The code above is mostly reusable with its PDF widget equivalent looking something like:

on mouseUp
   answer file "Please choose the file you would like to display" with type "PDF document|pdf|PDF"
   if it is not empty then set the filename of widget "PDF" to it
end mouseup

16 Comments

tb

here I get the answer:
button "loadPdf": execution error at line 15 (LCB Error in file browser.lcb at line 571: Value is not of correct type for passing as argument - expected type for passing to parameter pUrl of com.livecode.widget.browser.OnBrowserRequestCallback)

What am I doing wrong?

tb

Elanor Buchanan

Hi tb, I've just done a quick test here and this works ok in 8.1.6 and 9.0 on Mac. What version of LiveCode and OS are you on? There is a known issue where the browser widget does not work on 32-bit Linux.

Kind regards

Elanor

tb

Dear Eleanor
I use 9.0.0 dp-9 and macOS Sierra Version 10.12.6
Everything works perfect if I open a url-adress outside of my Mac but if I try to open a pdf document on my System it doesn't (and the address is a file not an url).
tb

Elanor Buchanan

Hi tb, it turns out there is a bug with PDF filenames that have spaces or Unicode characters. Could this be what you are experiencing?

http://quality.livecode.com/show_bug.cgi?id=19226

If not could you perhaps report the problem as as bug at

http://quality.livecode.com/

So we can investigate further.

Kind regards

Elanor

Simon Knight

The code as written in the tutorial would not display any on the PDFs I tried due to the use of problem characters in the filenames. (This is also a problem/feature of the rev browser). My solution which works so far is to substitute two characters with their hex code in the filename :

example filename as typed by me then copied into and out from Mac OS Terminal app:

1704_FirstDirect_Goldcard_Statement_Financial.pdf
/Goldcard\ Statements/Goldcard-\(PDF\)/1704_FirstDirect_Goldcard_Statement_Financial\(1\).pdf

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
replace " " with "%20" in tFile -- code based on script used with revbrowser
replace "|" with "%7C" in tFile -- code based on script used with revbrowser
set the url of widget "browser" to tFile
end if
end mouseUp

What I find confusing is that the line replacing the | is required yet the | does not seem to be in the file name.

Also, please will LC confirm if Elanor is correct re Unicode as the work arounds seems to be dealing with so called "unsafe" 8 bit (1 byte) ASCII characters such as space rather than unicode.
From w3Schools.com
"URL encoding replaces unsafe ASCII characters with a "%" followed by two hexadecimal digits."
Please note that using the LC function URLEncode does not work with either of the browsers.

My limited research indicates that MAC OS uses UTF8 character encoding in filenames whereas Windows uses UTF16 which implies that Livecode is translating filenames when it returns a value in the it variable. I'm not sure of the implications but worry that my workaround is to simplistic.
I have also made similar comments in the bug report 19226

Simon Knight

To get the browser to work on my mac I modified the script to:
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
replace " " with "%20" in tFile -- code based on script used with revbrowser
replace "|" with "%7C" in tFile -- code based on script used with revbrowser
set the url of widget "browser" to tFile
end if
end mouseUp

Probably a kludge and there may well be other characters waiting to trip me up.

Simon Knight

I have made several attempts at making a comment and have failed so I have posted to the forum here https://forums.livecode.com/viewtopic.php?f=8&t=30068
The short answer is the use textencode the file name using UTF8 on mac and UTF16 on PC, then use the URLEncode function and pass the result to the browser.

Elanor Buchanan

Dear Simon

Thanks you for your comments, I have added a line to the lesson to manually encode spaces in filenames which should allow the user to select a PDFs with spaces in their names. Thank you for adding your comments to the bug report too.

We are aware of the anomalies in the URLEncode function but changing it could cause backward compatibility issues

http://quality.livecode.com/show_bug.cgi?id=15369

so the workaround you are using is the best solution for now.

Kind regards

Elanor

Till Bandi

LiveCode 9 still seems to have this bug.
I tried to use Robert Caillious function with "äöüÄÖÜ;,?!àèé " but is seems that there are still hidden characters that don't get caught. Is there any possibility to get those?

Elanor Buchanan

Hi Till, what issue are you seeing, I tried converting "äöüÄÖÜ;,?!àèé " using Robert Cailliau's function and then converting the result back using urlDecode and got "äöüÄÖÜ;,?!àèé " again.

Which particular characters are causing problems? You might be able to customise the MyURLEncode function to include more special cases for this characters.

Kind regards

Elanor

Till

With LiveCode 9.5.1(rc 1) the script crashes.

Elanor Buchanan

Hi Till

I tested both methods in LiveCode Business 9.5.1(rc 1) and it works as expected. Can you let us know

- If the crash is in the IDE or a standalone
- What platform you are on
- What edition of LiveCode you are using
- The full path to the PDF file, in case LiveCode is not handling some of the characters correctly as in your previous comment

Thank you.

Elanor

Till

Dear Elanor

- If the crash is in the IDE or a standalone: in the IDE
- What platform you are on: iMac (16,2); macOS Catalina 10.15.1
- What edition of LiveCode you are using: LiveCode Business 9.5.1 (rc 1)
- The full path to the PDF file, in case LiveCode is not handling some of the characters correctly as in your previous comment: I put the file on the desktop and gave it the name "XXX.pdf"

The system-language is Swiss German.

I don't see what I am doing wrong – I did repeat the mistake several times. Should I send you the crash log? (I don't have your eMail address).

Sincerely, Till

Elanor Buchanan

Hi Till

We have not been able to recreate the crash here, does it happen with the launch url method the browser widget or both?

Could you include the full path to the file in your reply to this comment?

Thank you.

Elanor

Oskar

Hello
I copied the above code to display a .pdf file. It does not work.
When I remove "with type "PDF document|pdf|PDF"" i can display .jpg files or .txt files without problem.
I'm using Livecode 9.6.8 on Mac os 13.0
Thanks for your help

Panos Merakos

Hello Oskar,

When you say it does not work, do you mean that the file chooser dialog does not allow you to choose pdf files? Or that the chosen pdf does not display in the browser widget? If the latter, what is the full absolute path of the .pdf file you're trying to display?

Kind regards,
Panos
--

Add your comment

This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.