Skip to main content

Overview

You can use ONLYOFFICE Document Builder to generate documents from .js script files via the command line (CLI). To launch it, run:

docbuilder.exe mydocument.js

Use the full path to the executable if running from a different folder. Replace mydocument.js with the path to your script file.

Visit the Script file section for more information about file structure and syntax rules.

Server integration

For examples of using Document Builder CLI on servers with different programming languages (C#, Node.js, PHP, Ruby), see the document-builder-integration repository.

builder vs builderJS

There are two objects available for working with documents. Both provide access to CDocBuilder methods, but they differ in how they handle JavaScript variables.

ObjectDescriptionJS variables
builderLine-by-line parser. Best for simple scripts with static values.Wrap with jsValue: "jsValue(myVar)"
builderJSNative JavaScript wrapper. Best for dynamic values and complex logic.Use directly: myVar

Example

const outputPath = "result.docx";

// builder - requires jsValue wrapper
builder.CreateFile("docx");
builder.SaveFile("docx", "jsValue(outputPath)");
builder.CloseFile();

// builderJS - use variables directly
builderJS.CreateFile("docx");
builderJS.SaveFile("docx", outputPath);
builderJS.CloseFile();
Incorrect
const outputPath = "result.docx";
builder.SaveFile("docx", outputPath); // Will not work!
builderJS.SaveFile("docx", "jsValue(outputPath)"); // Will not work!

Working with multiple files

Document Builder works with one file at a time. To work with multiple files in a script, close the current file before opening another:

builderJS.CreateFile("docx");
// ... work with first document
builderJS.SaveFile("docx", "first.docx");
builderJS.CloseFile();

builderJS.CreateFile("xlsx");
// ... work with second document
builderJS.SaveFile("xlsx", "second.xlsx");
builderJS.CloseFile();

OpenTmpFile

The builderJS object provides an additional OpenTmpFile method beyond CDocBuilder. To open another file for manipulation (document comparison, mail merge, etc.) without affecting the main document:

const tmpFile = builderJS.OpenTmpFile("document.docx");

The returned object has the following methods:

NameDescription
IsValidReturns true if the temporary file is valid.
GetBinaryReturns Uint8Array with the doct/pptt/xlst binary content.
GetFolderReturns the path to the temporary folder with the file contents.
CloseCloses the file and removes the temporary folder contents.
GetImageMapReturns a dictionary with imageId -> imagePath pairs for inserting into the document.

Passing arguments

You can pass arguments from the command line to your script using the --argument flag. Arguments are passed as a JSON object and accessed via the global Argument object.

See Using command line arguments for details and examples.

GlobalVariable

Use the global GlobalVariable object to share data between documents. Variables persist after closing a file and can be accessed in subsequently opened files.

See Sharing data between files for details and examples.