Created Date: Mar 12, 2021 13:42
Last Modifed Date: Mar 12, 2021 13:42


Begin

Begin by extracting the Python libraries from the Mako distribution. Look for the Windows SWIG (C#, Java, Python) folder which contains a ZIP. Unzip this to retrieve the Python libraries from mako-SWIG\swig\python\release-win-x64-vc16\bindings.

Visual Studio Code

There are several choices of development environment for developing in Python. I have chosen VSCode for this example.

First:

  • Install Python if you have not already done so. Choose Python 3.8.x as this is the version used by the Mako library.
  • Install the Python extension in Visual Studio Code (VSCode). Get it from this Microsoft website (or just search for "VSCode Python" in the search engine of your choice).

Copy the two files to a new folder. It can be located anywhere, but it will become your VSCode workspace, so you may wish to locate it in an appropriate place for use with your version control system.

Next, in VSCode, follow these steps:

Open a folder. You can also use the Create Python Project, but for simplicity we'll use the Open Folder option.The sidebar after opening the folder, which in this case is called GettingStartedNow choose File>New File, and type the code you see here. Save as 'GettingStarted.py'.

Press F5 to make sure it runs. The output window at the bottom of the VSCode windows should display "Hello World".

Adding some Python code

You are now ready to add some code. Here is some Python code you can paste in, to replace what is there.

from jawsmakoIF_python import *

# Instantiate Mako
jawsmako = IJawsMako.create()
IJawsMako.enableAllFeatures(jawsmako)
factory = jawsmako.getFactory()

# Create an assembly, document and page
assembly = IDocumentAssembly.create(jawsmako)
document = IDocument.create(jawsmako)
assembly.appendDocument(document)
page = IPage.create(jawsmako)
document.appendPage(page)

# Create a fixed page A5 landscape.
pagewidth = 793.7
pageheight = 561.1
fixedpage = IDOMFixedPage.create(factory, pagewidth, pageheight)

# Now create some DOM for the page contents.

# Find a font
font: IDOMFont = jawsmako.findFont('Arial Bold')

# Create a blue brush
hcolorspace = IDOMColorSpacesRGB.create(factory)
bluecolor = IDOMColor.create(factory, hcolorspace, 1.0, 0.0, 0.0, 1.0)
solidBrush = IDOMSolidColorBrush.create(factory, bluecolor)

# Create glyphs
message = 'Mako Python API'
glyphs = IDOMGlyphs.create(factory, message, 72, FPoint(10, pageheight / 2),
                           solidBrush, font[0], 0)

# And add to the page
fixedpage.appendChild(glyphs.toIDOMNode())

# Assign content to a page
page.setContent(fixedpage)

# Now we can write this to a PDF
IPDFOutput.create(jawsmako).writeAssembly(assembly, 'TestPython.pdf', IProgressTick.Null())

print('File created.')
PY

Run this code by pressing F5. You should see the message "File Created." in the output window. You will find the output in your project folder.