Differences from the Haru API

LuaHPDF is a relatively transparent binding to the Haru free PDF library, so it should be straightforward to write Lua scripts using the Haru documentation. The differences that you will encounter are summarized here.

Names of functions and constants

The names of functions and constants in the Haru library begin with the prefix HPDF_. In LuaHPDF, this prefix has been removed and the remainder of the name is used as a key in the hpdf namespace. For example, HPDF_GetFont is named hpdf.GetFont in LuaHPDF. The integer, float and string manifest constants defined in hpdf_consts.h are treated similarly. For example, the constant defined by the line

#define HPDF_ENABLE_PRINT        4

is available as hpdf.ENABLE_PRINT.

Buffers and callbacks

Currently, bindings to the HPDF_NewEx, HPDF_SetErrorHandler and HPDF_ReadFromStream functions have not been implemented because of memory buffer and callback issues.


In hpdf_types.h, the Haru library defines several enumerations such as the following.

typedef enum _HPDF_PageMode {
} HPDF_PageMode;

In LuaHPDF, a function expecting an argument of type HPDF_PageMode should be passed a string whose value is the same as the enumeration identifier with the leading HPDF_ removed. That is, the argument should be the string "PAGE_MODE_USE_NONE", "PAGE_MODE_USE_OUTLINE", "PAGE_MODE_USE_THUMBS", or "PAGE_MODE_FULL_SCREEN". An error occurs if the passed-in argument isn't a string with one of these values.

This approach is used for all Haru library enumerations.


Structures in the Haru library are implemented as tables in LuaHPDF. For example, the CMYK color structure is defined in hpdf_types.h as follows.

typedef struct _HPDF_CMYKColor {
  HPDF_REAL   c;
  HPDF_REAL   m;
  HPDF_REAL   y;
  HPDF_REAL   k;

In LuaHPDF, this structure is mapped to a Lua table containing the fields c, m, y, and k.

Modified function interfaces

The following changes in function arguments and return values have been made in LuaHPDF.

Various functions accept arguments and return values of type HPDF_BOOL. In these cases, a Lua boolean is used. Note that the value 0, since it is neither nil nor false, is interpreted as true. The manifest constants hpdf.TRUE and hpdf.FALSE are defined as the Lua booleans true and false, respectively.