How to create PDFs using LiveCode
This lesson will show you how to create PDFs using LiveCode. We will see how to create external links within the document and create a document outline for the document.
Creating a stack to be printed

The LiveCode pdf printing commands are used to print cards of a stack. For this lesson we will use a stack with two cards to be printed. Each card has a field on it containing text from the LiveCode User Guide.
To create the sample stack select new stack > default size from the File menu and call it PDF Printing. The sample stack can be whatever size you want but as we will be printing to A4 it is best to have stack that has about the same ratio as A4 so the cards scale correctly when printed. I have set my sample stack to 575 x 800 in the Position tab of the Property Inspector.
Adding a card

Add a second card to the stack using the New Card option in the Object menu.
Adding fields and text

Go back to the first card by selecting Go First in the View menu. Add a field to the stack and set it to the same size as the stack, turn off any scrollbars on the field. Then put some text into the field. The card will be printed as you see it so ensure it looks just as you want it to appear in the PDF document.
Printing a card

The first step is to create a PDF document by printing a single card.
Open the stack script from the Object Menu and add the following printCards handler
on printCards
## Path to the pdf file we want to create
put specialFolderPath("documents") & "/UserGuide.pdf" into tPDFPath
open printing to pdf tPDFPath
if the result is "Cancel" then
## The user has cancelled printing
exit printCards
else
## Print the card into the printable area
print card 1 of this stack into 0,0,575,800
end if
close printing
end printCards
We use print into rect to scale the card to fit the specified rect. In this case the rect we give is the same size as our stack so it should scale perfectly.
Printing Multiple Cards
Now set up the second card of the stack with a field and some text in the same way as the first card.
We need to update the printCards handler to print both cards. When printing multiple cards you need to put a page break after each card otherwise they will be printed on top of each other. Use the LiveCode print break command for this.
on printCards
## Path to the pdf file we want to create
put specialFolderPath("documents") & "/UserGuide.pdf" into tPDFPath
open printing to pdf tPDFPath
if the result is "Cancel" then
## The user has cancelled printing
exit printCards
else
## Print the cards into the printable area
print card 1 of this stack into 0,0,575,800
print break
print card 2 of this stack into 0,0,575,800
end if
close printing
end printCards
If you want to print all the cards of a stack you can use a repeat loop to step through each card of the stack and print it followed by a break. Test your progress by opening the message box and typing
printCards
into it and return.
Adding an external link to the PDF

On page 2 of the stack there is a link to the LiveCode lessons. We can use the print link command to add an external link to the pdf.
The print link command can be used to to add a link to external url or an internal link within the document as defined by the print anchor command. To use the print link command you specify a target for the link and a rectangle describing the area on the page that will link to the given target.
To find the rect of the link area draw a graphic over the area you want to link to a url, make the graphic transparent and make a note of the rect of the graphic. Then update the printCards handler to print the link to the pdf.
on printCards
## Path to the pdf file we want to create
put specialFolderPath("documents") & "/UserGuide.pdf" into tPDFPath
open printing to pdf tPDFPath
if the result is "Cancel" then
## The user has cancelled printing
exit printCards
else
## Print the cards into the printable area
print card 1 of this stack into 0,0,575,800
print break
print card 2 of this stack into 0,0,575,800
print link to url "http://lessons.livecode.com" with rect 5,17,209,36
end if
close printing
end printCards
When you open the PDF that is created that area of page 2 should link to livecode.com when you click on it.
Creating a document outline
The print bookmark command can be use to create a document outline in the PDF. The print bookmark command is used similarly to the print link command we used in the previous. To use it you specify the bookmark title, level and location where the title is the name of the bookmark which will be displayed in the outline, the level is the depth of the bookmark and the location is an x,y coordinate describing where on the current page the bookmark will be placed.
In this example we will place Level 1 bookmarks for chapter titles, level 2 bookmarks for a.b sections and level 3 bookmarks for a.b.c sections eg
print bookmark "1.3.1 All Operating Systems" with level 3 at 5,253
You can use the function to get the location of bookmarks, just place the mouse over the point on the card where you want to place the bookmark and execute
put the mouseLoc
in the message box, this will give you a coordinate you can use.
Now update the printCards handler with all the bookmarks you want to add
on printCards
## Path to the pdf file we want to create
put specialFolderPath("documents") & "/UserGuide.pdf" into tPDFPath
open printing to pdf tPDFPath
if the result is "Cancel" then
## The user has cancelled printing
exit printCards
else
print card 1 of this stack into 0,0,575,800
print bookmark "1 Introduction" with level 1 at 5,51
print bookmark "1.1 Welcome" with level 2 at 5,147
print bookmark "1.2 Where to begin" with level 2 at 5,773
print break
print card 2 of this stack into 0,0,575,800
print link to url "http://lessons.livecode.com" with rect 5,17,209,36
## Print bookmarks
print bookmark "1.3 System Requirements" with level 2 at 5,75
print bookmark "1.3.1 All Operating Systems" with level 3 at 5,253
print bookmark "1.3.2 Requirements for Windows System" with level 3 at 5,400
print bookmark "1.3.3 Requirements for Linux Systems" with level 3 at 5,575
end if
close printing
end printCards
The final pdf

Your final PDF should include a link to the LiveCode lessons portal and a document outline that allows you to navigate around the document
This works fine on all platforms except my iPAD. I have added "launch document tPDFPath" to this script so the pdf opens automatically as well as saving to the documents folder.
However, when attempting this on the iPAD device, nothing happens. The PDF does not open, or I cannot find where the pdf is saved on the iPAD.
Can you shed any light on this? I have trawled through the forums and still no joy.