How do I display a PDF in LiveCode?

There are occassions 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 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 LiveCode is to use the launch document command. This opens a document with the operating system's default program.

launch document "LiveCodeNotes-8_1_3.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.

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.

8 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

Add your comment

E-Mail me when someone replies to this comment