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

ParameterData TypeDescription
Mandatory Parameters
apikey423s*****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
langStringEnables 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

CodeDescription
1831Error occurred. Parameter value is either incorrect or invalid.
1852File format you're trying to import is not supported.
1868Invalid 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
Copiedimport * as SDK from "@zoho-corp/office-integrator-sdk";
import { readFileSync } from 'fs';
const __dirname = import.meta.dirname;

class PreviewDocument {

    static async execute() {
        
        //Initializing SDK once is enough. Calling here since code sample will be tested standalone. 
        //You can place SDK initializer code in you application and call once while your application start-up. 
        await this.initializeSdk();

        try {
            var sdkOperations = new SDK.V1.V1Operations();
            var previewParameters = new SDK.V1.PreviewParameters();

            //Either use url as document source or attach the document in request body use below methods
            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 = readFileSync(filePath);
            // var streamWrapper = new SDK.StreamWrapper(fileName, fileStream, filePath);

            // previewParameters.setDocument(streamWrapper);

            var previewDocumentInfo = new SDK.V1.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 SDK.V1.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 SDK.V1.InvalidConfigurationException) {
                        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);
        }
    }

    //Include office-integrator-sdk package in your package json and the execute this code.

    static async initializeSdk() {

        // Refer this help page for api end point domain details -  https://www.zoho.com/officeintegrator/api/v1/getting-started.html
        let environment = await new SDK.DataCenter.Production("https://api.office-integrator.com");

        let auth = new SDK.AuthBuilder()
                        .addParam("apikey", "2ae438cf864488657cc975*******") //Update this apikey with your own apikey signed up in office integrator service
                        .authenticationSchema(await new SDK.V1.Authentication().getTokenFlow())
                        .build();

        let tokens = [ auth ];

        //Sdk application log configuration
        let logger = new SDK.LogBuilder()
            .level(SDK.Levels.INFO)
            //.filePath("<file absolute path where logs would be written>") //No I18N
            .build();

        let initialize = await new SDK.InitializeBuilder();

        await initialize.environment(environment).tokens(tokens).logger(logger).initialize();

        console.log("SDK initialized successfully.");
    }
}

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" 
}