Merge Document

Purpose

This API will allow you to generate documents and merge them.

Quick Reference

PropertyValue
MethodPOST
Request URLhttps://{api.office-integrator_domain}/writer/officeapi/v1/document/merge
Content-Typemultipart/form-data

Parameters

Request Parameters

ParameterValueMandatory/OptionalDescription
apikey423s******MandatoryUniquely identifies the web application in which the Writer editor is integrated.

Body Parameters

ParameterData TypeMandatory/OptionalDescription
output_formatStringMandatorySupported formats are zdoc, pdf or docx. Specify a format in which you would like to download the merged document.
file_content or file_urlFile or StringMandatoryBelow are the methods to provide the input file that needs to be merged with the data depending on its location.
file_content - if the input file is from your local drive or desktop.
file_url - if the input file is from a publicly accessible Web URL.
You can provide the data in any one of the below formats
merge_dataJSONObjectMandatory (any one)Pass a String value through 'data' key as JSON Array.
merge_data_csv_contentFileMandatory (any one)Provide the required csv content in your API request body.
merge_data_json_contentFileMandatory (any one)Provide the required json content in your API request body.
merge_data_csv_urlURLMandatory (any one)Provide the required csv url in your API request body.
merge_data_json_urlURLMandatory (any one)Provide the required json url in your API request body.
passwordStringOptionalSpecify a password if you would like to protect the merged document.

Notes and Limits

  • The output_format parameter supports zdoc, pdf, and docx formats.
  • You must provide the input file using either file_content (local file) or file_url (publicly accessible URL).
  • You must provide the merge data in any one of the following formats: merge_data, merge_data_csv_content, merge_data_json_content, merge_data_csv_url, or merge_data_json_url.
  • The merged document will be returned as downloadable bytes in the response.
  • For possible error codes, refer to: Error Codes
  • For other server-side SDK sample code, refer to: Server-Side SDKs

Sample Request

Copiedcurl --location --request POST "https://api.office-integrator.com/writer/officeapi/v1/document/merge" \
  --form "apikey=423s******" \
  --form "output_format=pdf" \
  --form "password=***" \
  --form "file_content=@" \
  --form "merge_data={\"data\":[{\"name\":\"Amelia\",\"email\":\"amelia@zylker.com\"}]}"
Copiedimport * as SDK from "@zoho-corp/office-integrator-sdk";
import { readFileSync, writeFileSync } from 'fs';
const __dirname = import.meta.dirname;

class MergeAndDownload {

    //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", "2ae438cf864488657cc*******") //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.");
    }

    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 parameters = new SDK.V1.MergeAndDownloadDocumentParameters();

            //Either use url as document source or attach the document in request body use below methods
            parameters.setFileUrl("https://demo.office-integrator.com/zdocs/OfferLetter.zdoc");
            parameters.setMergeDataJsonUrl("https://demo.office-integrator.com/data/candidates.json");

            var fileName = "OfferLetter.zdoc";
            var filePath = __dirname + "/sample_documents/OfferLetter.zdoc";
            var fileStream = readFileSync(filePath);
            var streamWrapper = new SDK.StreamWrapper(fileName, fileStream, filePath);
            
            parameters.setPassword("***");
            parameters.setOutputFormat("pdf");
            parameters.setFileContent(streamWrapper);

            // var jsonFileName = "candidates.json";
            // var jsonFilePath = __dirname + "/sample_documents/candidates.json";
            // var jsonFileStream = readFileSync(jsonFilePath);
            // var jsonStreamWrapper = new SDK.StreamWrapper(jsonFileName, jsonFileStream, jsonFilePath);

            // parameters.setMergeDataJsonContent(jsonStreamWrapper);

            // var csvFileName = "csv_data_source.csv";
            // var csvFilePath = __dirname + "/sample_documents/csv_data_source.csv";
            // var csvFileStream = readFileSync(csvFilePath);
            // var csvStreamWrapper = new SDK.StreamWrapper(csvFileName, csvFileStream, csvFilePath);

            // parameters.setMergeDataCsvContent(csvStreamWrapper);

            // parameters.setMergeDataCsvUrl("https://demo.office-integrator.com/data/csv_data_source.csv");
            // parameters.setMergeDataJsonUrl("https://demo.office-integrator.com/zdocs/json_data_source.json");

            // var mergeData = new Map();

            // parameters.setMergeData(mergeData);

            var responseObject = await sdkOperations.mergeAndDownloadDocument(parameters);

            if(responseObject != null) {
                console.log("\nStatus Code: " + responseObject.statusCode);
    
                let writerResponseObject = responseObject.object;
    
                if(writerResponseObject != null) {
                    if(writerResponseObject instanceof SDK.V1.FileBodyWrapper) {
                        var convertedDocument = writerResponseObject.getFile();

                        if (convertedDocument instanceof SDK.StreamWrapper) {
                            var outputFilePath = __dirname + "/sample_documents/merge_and_download.pdf";

                            writeFileSync(outputFilePath, convertedDocument.getStream());
                            console.log("\nCheck merged output file in file path - ", outputFilePath);
                        }
                    } 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);
        }
    }
}

MergeAndDownload.execute();

Sample Response

The document will be downloaded as bytes.