wilton.js  v202103141
 All Namespaces Functions
Functions
PDFDocument Namespace Reference

wilton/PDFDocument
Generate PDF documents. More...

Functions

Undefined addPage (Object options, Function|Undefined callback)
 Append page to the document.
Undefined destroy (Function|Undefined callback)
 Release memory allocated for this document.
Undefined drawImage (Object options, Function|Undefined callback)
 Draw image.
Undefined drawLine (Object options, Function|Undefined callback)
 Draw straight line.
Undefined drawRectangle (Object options, Function|Undefined callback)
 Draw rectangle.
String loadFont (String ttfPath, Function|Undefined callback)
 Load TrueType font from the specified TTF file.
Object PDFDocument (Function|Undefined callback)
 Create PDFDocument instance.
Undefined saveToFile (String path, Function|Undefined callback)
 Write this document contents into PDF file.
Undefined writeText (Object options, Function|Undefined callback)
 Write text to document.
Undefined writeTextInsideRectangle (Object options, Function|Undefined callback)
 Write text to document inside rectangle.

Detailed Description

This module allows to generate PDF documents.

Text, colors, lines and rectangles are supported.

TrueType font is required for PDF generation, it is read from a specified TTF file.

After use, PDF document instance may be destroyed manually using destroy() or it will be destroyed during the shutdown.

libharu is used for PDF generation, refer to its doc for the information about the coordinate system.

Usage example:

// create document
var doc = new PDFDocument();
// load font, font name is returned to be used for subsequent text operations
var someFont = doc.loadFont("path/to/someFont.ttf");
// add first page, more may be added after it
doc.addPage({
format: "A4",
orientation: "PORTRAIT"
});
// write text
doc.writeText({
text: "hello",
fontName: someFont,
fontSize: 14,
x: 20,
y: 500
});
// draw lines or rectangles
doc.drawLine({
beginX: 250,
beginY: 350,
endX: 150,
endY: 300,
lineWidth: 3
});
// write PDF to file
doc.saveToFile("test.pdf");
// free allocated memory
doc.destroy();

Function Documentation

Undefined PDFDocument::addPage ( Object  options,
Function|Undefined  callback 
)

Appends new page to the document and moves internal "write cursor" to this page. All subsequent content-related operation will be done to this page (until additional page will be appended).

Parameters
optionsObject configuration object, see possible options below
callbackFunction|Undefined callback to receive result or error
Returns
Undefined

Options

  • format String|Undefined Paper format, supported values: A3, A4, A5, B4, B5; either both format and orientation, or both width and height must be specified.
  • orientation String|Undefined Paper orientation, supported values: PORTRAIT, LANDSCAPE
  • width Number|Undefined Page width.
  • height Number|Undefined Page height.
Undefined PDFDocument::destroy ( Function|Undefined  callback)

Releases memory allocated for this document.

Parameters
callbackFunction|Undefined callback to receive result or error
Returns
Undefined
Undefined PDFDocument::drawImage ( Object  options,
Function|Undefined  callback 
)

Draws the specified image scaling it to the specified width and higth at the current (last added) page of the document.

Either imageHex or imagePath must be specified.

See libharu documentation for the information about the coordinate system.

Parameters
optionsObject configuration object, see possible options below
callbackFunction|Undefined callback to receive result or error
Returns
Undefined

Options

  • imageHex String|Undefined image data in hexadecimal
  • imagePath StringUndefined path to image file
  • imageFormat String file format of the specified image data, supported formats: PNG, JPEG
  • x Number x coordinate of the lower-left corner of the rectangle
  • y Number y coordinate of the lower-left corner of the rectangle
  • width Number Width of the rectangle in pt
  • height Number Height of the rectangle in pt
Undefined PDFDocument::drawLine ( Object  options,
Function|Undefined  callback 
)

Draws the straigth line with the specified lineWidth and color at the current (last added) page of the document.

See libharu documentation for the information about the coordinate system.

Parameters
optionsObject configuration object, see possible options below
callbackFunction|Undefined callback to receive result or error
Returns
Undefined

Options

  • beginX Number x coordinate of the beginning of the line
  • beginY Number y coordinate of the beginning of the line
  • endX Number x coordinate of the end of the line
  • endY Number y coordinate of the end of the line
  • lineWidth Number|Undefined Line width in pt, default value: 1
  • color Object|Undefined Line color in RGB format, default value: black
    • r Number Red element as float, must be in [0, 1]
    • g Number Green element as float, must be in [0, 1]
    • b Number Blue element as float, must be in [0, 1]
Undefined PDFDocument::drawRectangle ( Object  options,
Function|Undefined  callback 
)

Draws the rectangle with the specified lineWidth and color at the current (last added) page of the document.

See libharu documentation for the information about the coordinate system.

Parameters
optionsObject configuration object, see possible options below
callbackFunction|Undefined callback to receive result or error
Returns
Undefined

Options

  • x Number x coordinate of the lower-left corner of the rectangle
  • y Number y coordinate of the lower-left corner of the rectangle
  • width Number Width of the rectangle in pt
  • height Number Height of the rectangle in pt
  • lineWidth Number|Undefined Line (rectangle border) width in pt, default value: 1
  • color Object|Undefined Line (rectangle border) color in RGB format, default value: black
    • r Number Red element as float, must be in [0, 1]
    • g Number Green element as float, must be in [0, 1]
    • b Number Blue element as float, must be in [0, 1]
String PDFDocument::loadFont ( String  ttfPath,
Function|Undefined  callback 
)

Loads TrueType font from the specified TTF file. Loaded font will be added to the current document.

Multiple fonts can be loaded for the single document and used later for the text operations.

Returns the name of the loaded font.

Parameters
ttfPathString path to TTF file
callbackFunction|Undefined callback to receive result or error
Returns
String loaded font name
Object PDFDocument::PDFDocument ( Function|Undefined  callback)

Creates PDFDocument instance, at least one page must be added to created document before writing content to it.

Parameters
callbackFunction|Undefined callback to receive result or error
Returns
Object PDFDocument instance
Undefined PDFDocument::saveToFile ( String  path,
Function|Undefined  callback 
)

Writes this document contents into PDF file on the specified path.

Parameters
pathString File system path for the PDF file to write
callbackFunction|Undefined callback to receive result or error
Returns
Undefined
Undefined PDFDocument::writeText ( Object  options,
Function|Undefined  callback 
)

Writes specified text to the current (last added) page of the document at the specified coordinates.

Text is processed using UTF-8 encoding.

See libharu documentation for the information about the coordinate system.

Parameters
optionsObject configuration object, see possible options below
callbackFunction|Undefined callback to receive result or error
Returns
Undefined

Options

  • text String Text contents to write
  • fontName String Name of the font to use for the specified text, font must already loaded for this document, name should be obtained from loadFont()
  • fontSize Number Font size (in pt) to use for the specified text
  • x Number x coordinate of the texts beginning
  • y Number y coordinate of the texts beginning
  • color Object|Undefined Text color in RGB format, default value: black
    • r Number Red element as float, must be in [0, 1]
    • g Number Green element as float, must be in [0, 1]
    • b Number Blue element as float, must be in [0, 1]
Undefined PDFDocument::writeTextInsideRectangle ( Object  options,
Function|Undefined  callback 
)

Writes specified text to the current (last added) page of the document placing it inside the specified rectangle. Text lines that do not fit inside the rectangle won't be dislayed.

Text is processed using UTF-8 encoding.

See libharu documentation for the information about the coordinate system.

Parameters
optionsObject configuration object, see possible options below
callbackFunction|Undefined callback to receive result or error
Returns
Undefined

Options

  • text String Text contents to write
  • fontName String Name of the font to use for the specified text, font must already loaded for this document, name should be obtained from loadFont()
  • fontSize Number Font size (in pt) to use for the specified text
  • left Number left coordinate of the rectangle
  • top Number top coordinate of the rectangle
  • right Number right coordinate of the rectangle
  • bottom Number bottom coordinate of the rectangle
  • align String Text alignment inside the rectangle, supported values: LEFT, RIGHT, CENTER, JUSTIFY
  • color Object|Undefined Text color in RGB format, default value: black
    • r Number Red element as float, must be in [0, 1]
    • g Number Green element as float, must be in [0, 1]
    • b Number Blue element as float, must be in [0, 1]