Preview Document
Purpose
To open a document in preview or read-only mode in Zoho Writer.
HTTP Request URL
https://{api.office-integrator_domain}/writer/officeapi/v1/document/preview
Request Parameters
Parameter | Data Type | Description |
Mandatory Parameters | ||
apikey | 423s***** | Uniquely identifies the web application in which the Writer editor is integrated. |
document or url | File or String | Method of providing the input file depending on its location. document - if the input file is from your local drive or desktop. url - if the input file is from a publicly accessible Web URL. |
Optional Parameters | ||
lang | String | Enables the editor interface to open in Writer supported languages. Default value: en (English) |
document_info | { "document_name":"New" } | Set a display name for the document. |
permissions | { "document.print":true, } | Define document permissions for user. Option to allow or revoke document "Print" access. |
Preview Document - Error Codes
Code | Description |
1831 | Error occurred. Parameter value is either incorrect or invalid. |
1852 | File format you're trying to import is not supported. |
1868 | Invalid parameter name for uploaded content. |
For a full list of error handling cases in Preview Document, refer here.
Sample Request
Copiedcurl --request POST \
--url 'https://api.office-integrator.com/writer/officeapi/v1/document/preview' \
--header 'content-type: multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW' \
--form document=@/Users/username/Documents/Sample.docx \
--form 'apikey=423s*****' \
--form lang=en
Copiedconst Levels = require("zoi-nodejs-sdk/routes/logger/logger").Levels;
const Constants = require("zoi-nodejs-sdk/utils/util/constants").Constants;
const APIKey = require("zoi-nodejs-sdk/models/authenticator/apikey").APIKey;
const Environment = require("zoi-nodejs-sdk/routes/dc/environment").Environment;
const LogBuilder = require("zoi-nodejs-sdk/routes/logger/log_builder").LogBuilder;
const UserSignature = require("zoi-nodejs-sdk/routes/user_signature").UserSignature;
const InitializeBuilder = require("zoi-nodejs-sdk/routes/initialize_builder").InitializeBuilder;
const fs = require("fs");
const StreamWrapper = require("zoi-nodejs-sdk/utils/util/stream_wrapper").StreamWrapper;
const PreviewResponse = require("zoi-nodejs-sdk/core/com/zoho//officeintegrator/office_integrator_sdk/preview_response").PreviewResponse;
const PreviewParameters = require("zoi-nodejs-sdk/core/com/zoho//officeintegrator/office_integrator_sdk/preview_parameters").PreviewParameters;
const PreviewDocumentInfo = require("zoi-nodejs-sdk/core/com/zoho//officeintegrator/office_integrator_sdk/preview_document_info").PreviewDocumentInfo;
const InvaildConfigurationException = require("zoi-nodejs-sdk/core/com/zoho//officeintegrator/office_integrator_sdk/invaild_configuration_exception").InvaildConfigurationException;
const OfficeIntegratorSDKOperations = require("zoi-nodejs-sdk/core/com/zoho//officeintegrator/office_integrator_sdk/office_integrator_sdk_operations").OfficeIntegratorSDKOperations;
class PreviewDocument {
//Include zoi-nodejs-sdk package in your package json and the execute this code.
static async initializeSdk() {
let user = new UserSignature("john@zylker.com");
let environment = new Environment("https://api.office-integrator.com", null, null);
let apikey = new APIKey("2ae438cf864488657cc9754a27daa480", Constants.PARAMS);
let logger = new LogBuilder()
.level(Levels.INFO)
.filePath("./app.log")
.build();
let initialize = await new InitializeBuilder();
await initialize.user(user).environment(environment).token(apikey).logger(logger).initialize();
console.log("\nSDK initialized successfully.");
}
static async execute() {
//Initializing SDK once is enough. Calling here since code sample will be tested standalone.
//You can place SDK initializer code in your application and call once while your application start-up.
await this.initializeSdk();
try {
var sdkOperations = new OfficeIntegratorSDKOperations();
var previewParameters = new PreviewParameters();
previewParameters.setUrl("https://demo.office-integrator.com/zdocs/Graphic-Design-Proposal.docx");
// var fileName = "Graphic-Design-Proposal.docx";
// var filePath = __dirname + "/sample_documents/Graphic-Design-Proposal.docx";
// var fileStream = fs.readFileSync(filePath);
// var streamWrapper = new StreamWrapper(fileName, fileStream, filePath)
//var streamWrapper = new StreamWrapper(null, null, filePath)
// previewParameters.setDocument(streamWrapper);
var previewDocumentInfo = new PreviewDocumentInfo();
//Time value used to generate unique document everytime. You can replace based on your application.
previewDocumentInfo.setDocumentName("Graphic-Design-Proposal.docx");
previewParameters.setDocumentInfo(previewDocumentInfo);
var permissions = new Map();
permissions.set("document.print", false);
previewParameters.setPermissions(permissions);
var responseObject = await sdkOperations.createDocumentPreview(previewParameters);
if(responseObject != null) {
//Get the status code from response
console.log("\nStatus Code: " + responseObject.statusCode);
//Get the api response object from responseObject
let writerResponseObject = responseObject.object;
if(writerResponseObject != null) {
console.log("\nPreview URL : " + writerResponseObject.getPreviewUrl());
//Check if expected PreviewResponse instance is received
if(writerResponseObject instanceof PreviewResponse) {
console.log("\nDocument ID - " + writerResponseObject.getDocumentId());
console.log("\nDocument session ID - " + writerResponseObject.getSessionId());
console.log("\nDocument preview URL - " + writerResponseObject.getPreviewUrl());
console.log("\nDocument delete URL - " + writerResponseObject.getDocumentDeleteUrl());
console.log("\nDocument session delete URL - " + writerResponseObject.getSessionDeleteUrl());
} else if (writerResponseObject instanceof InvaildConfigurationException) {
console.log("\nInvalid configuration exception. Exception json - ", writerResponseObject);
} else {
console.log("\nRequest not completed successfullly");
}
}
}
} catch (error) {
console.log("\nException while running sample code", error);
}
}
}
PreviewDocument.execute();
Sample Response
Copied{
"preview_url": "https://api.office-integrator.com/writer/officeapi/v1/document/d26aa7b8029bf4f8d34c7dd0013939237c7426dd7b014b7c5cd9fa07cdd21f0e304693ffbf26ba091b4f8d5350ec9bb8/preview",
"session_id": "d26aa7b8029bf4f8d34c7dd0013939237c7426dd7b014b7c5cd9fa07cdd21f0e304693ffbf26ba091b4f8d5350ec9bb8",
"document_id": "1349",
"session_delete_url": "https://api.office-integrator.com/writer/officeapi/v1/sessions/d26aa7b8029bf4f8d34c7dd001393923ec7a56cea9631855c708129cbd74491775f200eb5821ba0193ab987c514887c4",
"document_delete_url": "https://api.office-integrator.com/writer/officeapi/v1/documents/1349"
}