More languages
Some test text!
More languages
Sample JavaScript code for using Apryse SDK to generate a PDF from an Office document template and a JSON string. Does not require any external dependencies or MS Office licenses. Learn more about our JavaScript PDF Library and Office Template Generation.
Get Started Samples DownloadTo run this sample, get started with a free trial of Apryse SDK.
//---------------------------------------------------------------------------------------
// Copyright (c) 2001-2024 by Apryse Software Inc. All Rights Reserved.
// Consult legal.txt regarding legal and license information.
//---------------------------------------------------------------------------------------
//------------------------------------------------------------------------------
// The following sample illustrates how to use the PDF::Convert utility class
// to convert MS Office files to PDF and replace templated tags present in the document
// with content supplied via json
//
// For a detailed specification of the template format and supported features,
// see: https://docs.apryse.com/documentation/core/guides/generate-via-template/data-model/
//
// This conversion is performed entirely within the PDFNet and has *no*
// external or system dependencies -- Conversion results will be
// the same whether on Windows, Linux or Android.
//
// Please contact us if you have any questions.
//------------------------------------------------------------------------------
const { PDFNet } = require('@pdftron/pdfnet-node');
const PDFTronLicense = require('../LicenseKey/LicenseKey');
((exports) => {
'use strict';
exports.runOfficeTemplateTest = () => {
const inputPath = '../TestFiles/';
const outputPath = inputPath + 'Output/';
const inputFilename = 'SYH_Letter.docx'
const outputFilename = 'SYH_Letter.pdf'
const main = async () => {
PDFNet.addResourceSearchPath('../Resources');
try {
const options = new PDFNet.Convert.OfficeToPDFOptions();
const json = JSON.stringify({
'dest_given_name': 'Janice N.',
'dest_street_address': "187 Duizelstraat",
'dest_surname': 'Symonds',
'dest_title': 'Ms.',
'land_location': '225 Parc St., Rochelle, QC ',
'lease_problem': 'According to the city records, the lease was initiated in September 2010 and never terminated',
'logo': { 'image_url': inputPath + 'logo_red.png', 'width' : 64, 'height': 64 },
'sender_name': 'Arnold Smith'
});
// Create a TemplateDocument object from an input office file.
const templateDoc = await PDFNet.Convert.createOfficeTemplateWithPath(inputPath + inputFilename, options);
// Fill the template with data from a JSON string, producing a PDF document.
const pdfdoc = await templateDoc.fillTemplateJson(json);
// Save the PDF to a file.
await pdfdoc.save(outputPath + outputFilename, PDFNet.SDFDoc.SaveOptions.e_linearized);
// And we're done!
console.log('Saved ' + outputFilename);
} catch (err) {
console.log(err);
}
console.log('Done.');
};
PDFNet.runWithCleanup(main, PDFTronLicense.Key).catch(function (error) {
console.log('Error: ' + JSON.stringify(error));
}).then(function () { return PDFNet.shutdown(); });
};
exports.runOfficeTemplateTest();
})(exports);
// eslint-disable-next-line spaced-comment
//# sourceURL=OfficeTemplateTest.js