Tutorials

Using Renditions

Updated: February 9, 2017 Page Information Edit on GitHub

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.

3 months ago Solen Guitter NXDOC-1087: Use new previous/next navigation
4 months ago GitHub Update review date
5 months ago Andrew Goodricke content-review-lts2016 label added
5 months ago Karin Touchie NXDOC-922: Correct English in Getting Started doc
6 months ago Solen Guitter Add missing navigation to previous/next steps in Getting Started with the Nuxeo Platform
6 months ago manonlumeau Moved Getting Started, update parents
7 months ago Solen Guitter 25
7 months ago Solen Guitter 24
7 months ago Arnaud Kervern 23
7 months ago Solen Guitter 22
7 months ago Solen Guitter 21
7 months ago Solen Guitter 20
7 months ago Solen Guitter 19
7 months ago Bertrand Chauvin 18
7 months ago Bertrand Chauvin 17 | fix excerpt
7 months ago Bertrand Chauvin 16 | Reverted from v. 14
7 months ago Bertrand Chauvin 15 | fix code
7 months ago Bertrand Chauvin 14 | fix rendition doc name
8 months ago Arnaud Kervern 13
8 months ago Solen Guitter 12
8 months ago Solen Guitter 11
8 months ago Solen Guitter 10
9 months ago Bertrand Chauvin 9
9 months ago Bertrand Chauvin 8
9 months ago Bertrand Chauvin 6
9 months ago Bertrand Chauvin 7
9 months ago Bertrand Chauvin 5
9 months ago Bertrand Chauvin 4
9 months ago Bertrand Chauvin 3
9 months ago Bertrand Chauvin 2
9 months ago Bertrand Chauvin 1
History: Created by Bertrand Chauvin