% language=us runpath=texruns:manuals/luametatex \environment luametatex-style \startdocument[title=PDF] \startsection[title={Introduction}] There is no backend, not even a \DVI\ one. In \CONTEXT\ the main backend is a \PDF\ backend and it is written in \LUA. The \PDF\ format makes it possible to embed \JPEG\ and \PNG\ encoded images as well as \PDF\ images. All these have to be dealt with in \LUA. Although we can parse \PDF\ files with \LUA, the engine has a dedicated \PDF\ library on board written by Paweł Jackowski. A \PDF\ file is basically a tree of objects and one descends into the tree via dictionaries (key|/|value) and arrays (index|/|value). There are a few topmost dictionaries that start at the document root and those are accessed more directly. Although everything in \PDF\ is basically an object we have to wrap a few in so called userdata \LUA\ objects. \starttabulate[|l|l|] \FL \BC PDF \BC \LUA \NC \NR \TL \NC null \NC \NC \NR \NC boolean \NC \NC \NR \NC integer \NC \NC \NR \NC float \NC \NC \NR \NC name \NC \NC \NR \NC string \NC \NC \NR \NC array \NC \NC \NR \NC dictionary \NC \NC \NR \NC stream \NC \NC \NR \NC reference \NC \NC \NR \LL \stoptabulate The interface is rather limited to creating an instance and getting objects and values. Aspects like compression and encryption are mostly dealt with automatically. In \CONTEXT\ users use an interface layer around these, if they use this kind of low level code at all as it assumes familiarity with how \PDF\ is constructed. \stopsection \startsection[title={\LUA\ interfaces}] \startsubsection[title={Opening and closing}] There are two ways to open a \PDF\ file: \starttyping[option=LUA] function pdfe.open ( filename ) return -- pdffile end function pdfe.openfile( filehandle ) return -- pdffile end \stoptyping Instead of from file, we can read from a string: \starttyping[option=LUA] function pdfe.new ( somestring, somelength ) return -- pdffile end \stoptyping Closing the instance is done with: \starttyping[option=LUA] function pdfe.close ( pdffile ) -- no return values end \stoptyping When we used \type {pdfe.open} the library manages the file and closes it when done. You can check if a document opened as expected by calling: \starttyping[option=LUA] function pdfe.getstatus ( pdffile ) return -- status end \stoptyping A table of possible return codes can be queried with: \starttyping[option=LUA] function pdfe.getstatusvalues ( ) return -- values end \stoptyping Currently we have these: \ctxlua{moduledata.pdfe.codes("getstatusvalues")} An encrypted document can be decrypted by the next command where instead of either password you can give \type {nil} and hope for the best: \starttyping[option=LUA] function pdfe.unencrypt ( pdffile, userpassword, ownerpassword ) return -- status end \stoptyping \stopsubsection \startsubsection[title=Getting basic information] A successfully opened document can provide some information: \starttyping[option=LUA] function pdfe.getsize( pdffile ) return -- nofbytes end function pdfe.getversion( pdffile ) return , -- major -- minor end function pdfe.getnofobjects ( pdffile ) return -- nofobjects end function pdfe.getnofpages ( pdffile ) return -- nofpages end function pdfe.memoryusage ( pdffile ) return , -- bytes -- waste end \stoptyping \stopsubsection \startsubsection[title={The main structure}] For accessing the document structure you start with the so called catalog, a dictionary: \starttyping[option=LUA] function pdfe.getcatalog( pdffile ) return -- dictionary end \stoptyping The other two root dictionaries are accessed with: \starttyping[option=LUA] function pdfe.gettrailer ( pdffile ) return -- dictionary end function pdfe.getinfo ( pdffile ) return -- dictionary end \stoptyping \stopsubsection \startsubsection[title={Getting content}] A specific page can conveniently be reached with the next command, which returns a dictionary. \starttyping[option=LUA] function pdfe.getpage ( pdffile, pagenumber ) return -- dictionary end \stoptyping Another convenience command gives you the (bounding) box of a (normally page) which can be inherited from the document itself. An example of a valid box name is \type {MediaBox}. \starttyping[option=LUA] function pdfe.getbox ( pdffile, boxname ) return -- boundingbox end \stoptyping \stopsubsection \startsubsection[title={Getters}] Common values in dictionaries and arrays are strings, integers, floats, booleans and names (which are also strings) and these are also normal \LUA\ objects. In some cases a value is a userdata object and you can use this helper to get some more information: \starttyping[option=LUA] function pdfe.type ( value ) return type -- string end \stoptyping Stings are special because internally they are delimited by parenthesis (often \typ {pdfdoc} encoding) or angle brackets (hexadecimal or 16 bit \UNICODE). \starttyping[option=LUA] function pdfe.getstring ( object, key | index ) return -- decoded value end \stoptyping When you ask for more you get more: \starttyping[option=LUA] function pdfe.getstring ( object, key | index, more ) return , -- original -- hexencoded end \stoptyping Basic types are fetched with: \starttyping[option=LUA] function pdfe.getinteger ( , key | index ) return -- value end function pdfe.getnumber ( , key | index ) return -- value end function pdfe.getboolean ( , key | index ) return -- value end \stoptyping A name is (in the \PDF\ file) a string prefixed by a slash, like \typ [option=PDF] {<< /Type /Foo >>}, for instance keys in a dictionary or keywords in an array or constant values. \starttyping[option=LUA] function pdfe.getname ( , key | index ) return -- value end \stoptyping Normally you will use an index in an array and key in a dictionary but dictionaries also accept an index. The size of an array or dictionary is available with the usual \type {#} operator. \starttyping[option=LUA] function pdfe.getdictionary ( , key | index ) return -- dictionary end function pdfe.getarray ( , key | index ) return -- array end function pdfe.getstream ( , key | index ) return -- stream -- dictionary end \stoptyping These commands return dictionaries, arrays and streams, which are dictionaries with a blob of data attached. Before we come to an alternative access mode, we mention that the objects provide access in a different way too, for instance this is valid: \starttyping[option=LUA] print(pdfe.open("foo.pdf").Catalog.Type) \stoptyping At the topmost level there are \type {Catalog}, \type {Info}, \type {Trailer} and \type {Pages}, so this is also okay: \starttyping[option=LUA] print(pdfe.open("foo.pdf").Pages[1]) \stoptyping \stopsubsection \startsubsection[title={Streams}] Streams are sort of special. When your index or key hits a stream you get back a stream object and dictionary object. The dictionary you can access in the usual way and for the stream there are the following methods: \starttyping[option=LUA] function pdfe.openstream ( stream, decode) return okay end function pdfe.closestream ( stream ) -- no return values end function pdfe.readfromstream ( stream ) return str, size end function pdfe.readwholestream ( stream, decode) return str, size end \stoptyping You either read in chunks, or you ask for the whole. When reading in chunks, you need to open and close the stream yourself. The \type {decode} parameter controls if the stream data gets uncompressed. As with dictionaries, you can access fields in a stream dictionary in the usual \LUA\ way too. You get the content when you \quote {call} the stream. You can pass a boolean that indicates if the stream has to be decompressed. \stopsubsection \startsubsection[title={Low level getters}] In addition to the getters described before, there is also a bit lower level interface available. \starttyping[option=LUA] function pdfe.getfromdictionary ( , index ) return key, type, value, detail end function pdfe.getfromarray ( , index ) return type, value, detail end \stoptyping The \type {type} is one of the following: \startfourrows \ctxlua{moduledata.pdfe.codes("getfieldtypes")} \stopfourrows This list was acquired with: \starttyping[option=LUA] function pdfe.getfieldtypes ( ) return -- types end \stoptyping Here \type {detail} is a bitset with possible bits: \startfourrows \ctxlua{moduledata.pdfe.codes("getencodingvalues")} \stopfourrows This time we used: \starttyping[option=LUA] function pdfe.getencodingvalues ( ) return -- values end \stoptyping \stopsubsection \startsubsection[title={Getting tables}] All entries in a dictionary or table can be fetched with the following commands where the return values are a hashed or indexed table. \starttyping[option=LUA] function pdfe.dictionarytotable ( ) return -- hash end function pdfe.arraytotable ( ) return -- array end \stoptyping You can get a list of pages with: \starttyping[option=LUA] function pdfe.pagestotable( pdffile) return { { , -- dictionary , -- size , -- objectnumber }, ... } end \stoptyping \stopsubsection \startsubsection[title={References}] In order to access a \PDF\ file efficiently there is lazy evaluation of references so when you run into a reference as value or array entry you have to resolve it explicitly. An unresolved references object can be resolved with: \starttyping function pdfe.getfromreference( reference ) -- NEEDS CHECKING return , -- type , -- value -- detail \stoptyping So, as second value you can get back a new \type {pdfe} userdata object that you can query. \stopsubsection \stopsection \stopdocument