Getting Started

Using Renditions

Updated: November 15, 2024

Learn how to convert an office file and an image to other formats and sizes.

Converting an Office File into a PDF File

Goal

Convert a file from an office format into PDF format.

Prerequisites

Procedure

  1. Create a file called downloadPDFRendition.js with the following content. Be sure to replace the filePath variable with the path to the folder where the PDF rendition should be downloaded.

    #!/usr/bin/env node
    // fs and path NodeJS modules are required to handle file download
    let fs = require('fs');
    let path = require('path');
    let Nuxeo = require('nuxeo');
    let nuxeo = new Nuxeo({
        auth: {
            method: 'basic',
            username: 'sconnor',
            password: 'sconnor'
        }
    });
    // Change this value to indicate in which folder the PDF rendition file should be downloaded
    // Don't forget the trailing slash
    let filePath = '/tmp/';
    // Which contract to fetch a rendition for - No need to change if you followed our instructions so far
    let contractToFetch = '/default-domain/workspaces/North America/awesome-tech/awesome-contract';
    // Further calls will return all document properties
    nuxeo.schemas('*');
    // First we'll display the renditions list
    // to give you an idea of what you could fetch out of the box
    nuxeo.repository().fetch(contractToFetch)
        .then(contract => {
            return contract.fetchRenditions();
        })
        .then(renditions => {
            console.log('\nThe following renditions can be called on the contract:\n');
            console.log(renditions);
            console.log('\nWe\'ll ask for a PDF rendition.\n');
        })
        .catch(error => {
            console.log('Apologies, an error occurred while fetching the renditions list.');
            console.log(error);
            return;
        });
    nuxeo.repository().fetch(contractToFetch)
        .then(contract => {
            return contract.fetchRendition('pdf');
        })
        .then(response => {
            // Download the rendition and store it on the disk
            try {
                let stats = fs.statSync(filePath);
                if (!stats.isDirectory()) {
                    console.log(filePath + ' is not a folder.\nPlease check the filePath variable (currently set to: ' + filePath + ')\nand make sure you have the proper rights on that folder.');
                    return;
                }
                const writable = fs.createWriteStream(path.join(filePath, 'contract.pdf'));
                response.body.pipe(writable);
                console.log('The PDF rendition has been downloaded.');
                console.log(`You can take a look at it here: ${path.join(filePath, 'contract.pdf')}`)
            } catch (error) {
                console.log(`The folder where the rendition should be downloaded cannot be accessed.\nPlease check the filePath variable (currently set to: ${filePath})\nand make sure you have write access on that folder.`);
                console.log(error);
                return;
            }
            return nuxeo.repository().fetch(contractToFetch);
        })
        .then(contract => {
            console.log(`Notice that the contract's file remains in its initial format:`);
            console.log(contract.properties['file:content']);
        })
        .catch(error => {
            console.log('Apologies, an error occurred while retrieving the contract.');
            console.log(error);
            return;
        });
    
    
  2. Save and run:

    $ node downloadPDFRendition.js
    

Note: The contract's file stored on the Nuxeo server remains in its initial format. The rendition is applied on the fly so that only the downloaded file is converted.

Learn more

 

Fetching Resized Pictures

Goal

The marketing department wants to use the companies' logos stored in the portfolios for testimonials. They need all these images to have the same dimensions. Use a rendition to cover this need.

Prerequisites

Procedure

  1. Create a file called downloadResizedLogo.js with the following content. Be sure to replace the filePath variable with the path to the folder where the resized logo should be downloaded.

    #!/usr/bin/env node
    // fs and path NodeJS modules are required to handle file download
    let fs = require('fs');
    let path = require('path');
    let Nuxeo = require('nuxeo');
    let nuxeo = new Nuxeo({
        auth: {
            method: 'basic',
            username: 'sconnor',
            password: 'sconnor'
        }
    });
    // Change this value to indicate in which folder the image rendition file should be downloaded
    // Don't forget the trailing slash
    let filePath = '/tmp/';
    // Which portfolio to fetch a rendition for - No need to change if you followed our instructions so far
    let portfolioToFetch = '/default-domain/workspaces/North America/Money Bank';
    // Size of the company logo rendition you want to obtain
    // You can change "Medium" to "Thumbnail", "Small" or "OriginalJpeg" to see the difference for yourself
    let renditionSize = 'Medium';
    // Init some variables that will be used for the file name
    let companyName;
    let fileFormat;
    // Further calls will return all document properties
    nuxeo.schemas('*');
    nuxeo.repository().fetch(portfolioToFetch)
        .then(portfolio => {
            // Store the company name to use it in the file name later
            companyName = portfolio.properties['dc:title'];
            // Display the different image sizes
            console.log('\nThe following image sizes can be downloaded:\n');
            portfolio.properties['picture:views'].forEach(currentPictureView => {
                console.log(currentPictureView.title);
                console.log(currentPictureView.info);
                // Store the rendition format to generate the filename later
                if (currentPictureView.title === renditionSize) {
                    fileFormat = currentPictureView.info.format.toLowerCase();
                }
            });
            console.log('\nWe\'ll ask for a ' + renditionSize + '-sized company logo in the ' + fileFormat + ' format.');
            return portfolio.fetchRendition(renditionSize);
        })
        .then(response => {
            // Download the rendition and store it on the disk
            try {
                var stats = fs.statSync(filePath);
                if (!stats.isDirectory()) {
                    console.log(`${filePath} is not a folder.\nPlease check the filePath variable (currently set to: ${filePath})\nand make sure you have the proper rights on that folder.`);
                    return;
                }
                let renditionFilePath = path.join(filePath, `${companyName}-${renditionSize}.${fileFormat}`);
                const writable = fs.createWriteStream(renditionFilePath);
                response.body.pipe(writable);
                console.log(`The ${renditionSize} sized company logo has been downloaded!`);
                console.log(`You can take a look at it here: ${renditionFilePath}`)
            } catch (error) {
                console.log('The folder where the rendition should be downloaded cannot be accessed.\nPlease check the filePath variable (currently set to: ' + filePath + ')\nand make sure you have write access on that folder.');
                console.log(error);
                return;
            }
        })
        .catch(error => {
            console.log('Apologies, an error occurred while fetching the rendition.');
            console.log(error);
            return;
        });
    
    
  2. Save and run:

    $ node downloadResizedLogo.js
    

Note: The same principles can be applied on videos as well, to fetch them in various formats and quality options. Pictures and video renditions are stored in the document directly in order to boost performances.