Printing and File Generation in Calculations

A task calculation can generate a print report as PDF, merge or convert files, attach them to a case, and read or modify attachments in the DMS. This article summarises the file and print functions available in TAS 5.19.12 – from the lib library and from the standalone storage, zip and excel objects. General lib functions are covered in the article Calculations API Reference: lib library; the print templates themselves are described in the administrator article Print Templates.

Generating prints to the DMS

FunctionDescription
lib.savePrintForm(id, docType, fileName?)Creates the print report id in the docType format ('pdf', 'html' or 'doc') and saves it as an attachment in the DMS.
lib.printToFantomPdf(printName, fileName, forceNewPrint=false)Creates a PDF of the print report and saves it to the DMS; forceNewPrint=true forces rendering via Puppeteer (new prints of the Print – React type).
// Po schválení objednávky vygeneruje finální PDF a přiloží ho k případu
await lib.printToFantomPdf('Purchase order confirmation', 'PO-' + vars['OrderNumber'].getValue() + '.pdf');

Converting and modifying files

FunctionDescription
lib.libreConvert(srcFile, outFile, filter?)Converts a file on disk using LibreOffice (it must be installed on the server), e.g. .doc → .pdf.
lib.libreConvertDmsFile(fileVariable, outFormat='pdf', filter?)Same as above, but the source is an attachment-type variable; the result is returned/attached depending on the template implementation.
lib.libreMacroConvert(srcFile, outFile, macro) / libreMacroConvertDmsFile(fileVariable, outFormat, macro)Conversion using a custom LibreOffice macro.
lib.pdfToText(fileName)Converts the content of a PDF file to text.
lib.modifyPdfAddImage(identifier, pngPath, newFile=false, options)Inserts a PNG image (e.g. a stamp or a QR code from lib.createQrCodeBase64) into an existing PDF; options specify the page, position (x, y), scale and rotation. Without newFile, the image is added as a revision of the existing file.
lib.setDocumentLogicalType(fileName, logicTypeKey)Changes the logical type of a document (see the document type list in the DMS administration).
lib.getFileType(fileName)Returns the file type based on its extension (DMS_FILE_TYPE table).
lib.translateFileTemplate(srcFile, outFile)Replaces variables in a text file and saves the result to another file.
lib.zfoLinkAttachments(id, nameProp?, contentProp?)Extracts the files embedded in a .zfo file and attaches them to the case; returns their IDs separated by commas.
lib.xmlGetContents(id)Returns the content of an XML file stored in the DMS.
await lib.libreConvert('/tmp/uploads/quote.docx', '/tmp/uploads/quote.pdf', 'writer_pdf_Export');

Merging PDFs and working with case attachments

FunctionDescription
lib.dmsMergePdfs(inFileNames, outFileName, isOrdered=false)Merges selected PDF files of the case into one; isOrdered=true preserves the order given by the inFileNames array.
lib.storeAttachment(fileName, content, convertToBase64=true, lastRevisionId?)Saves a new file to the DMS and returns its ID.
lib.storeAttachmentAsRevision(fileName, content, convertToBase64, revisionFileName)Saves the file as a new revision of the existing file revisionFileName.
lib.exportAttachment(identifier, exportPath, encoding='binary')Copies a case attachment to an absolute path on the server disk.
lib.copyFile(srcPath, dstPath) / lib.moveFile(fromPath, toPath)Copies/moves a file on disk (outside the DMS); moveFile overwrites an existing target.
lib.addPublicFileToAttachment(publicFileName, dmsAttachmentName)Attaches a public file (from the configured public folder) to the case as an attachment.
lib.addMailNotifsAttachment(dmsfName)Adds a file from the DMS as an attachment of an email notification task.
lib.copySharedFileLink(iprocId, varName)Copies into the current case a link to a file from a shared Document list variable of another case.
lib.getDMSFiles(nameFilter='*', glue='|', attrNames?) / getDMSFileNames(...) / getDMSFileIds(...)Find case attachments by a name filter (* = all) and return the specified attributes, names or IDs respectively, joined by the glue character.
lib.countFiles(variable)Number of files in an attachment-type variable (values separated by :).
lib.getFileContents(pathToFile, encoding='base64')Content of a file from an absolute path on disk (the folder must be allowed in the configuration).
const quotePdf = await lib.dmsMergePdfs(['quote-1.pdf', 'quote-2.pdf'], 'quote-combined.pdf', true);
await lib.addMailNotifsAttachment('quote-combined.pdf');

storage – DMS and files on disk

The storage object is lower-level than the lib functions above and works directly with DMS entities and the file system.

MethodReturnsDescription
saveToDms(filePath, fileName)Promise<number | undefined>Saves a file from disk to the DMS as a case attachment and returns its ID.
getDmsEntityByName(dmsfName, searchRevisions=false)Promise<DmsFile | null>Finds a file by name within the current case.
getFromSubAndMainByName(name, searchRevisions=false)Promise<DmsFile | null>Same as above, but also searches subprocesses and the main case.
getDmsContent(id, encoding)Promise<string | Buffer>Content of a file from the DMS.
getDmsAbsolutePath(id)stringAbsolute path of the file on the server disk.
exportDmsFile(id, destination)–Copies a file from the DMS to the selected folder.
dmsFileExists(id) / fileExists(file)Promise<boolean>Checks whether a DMS file, or a file/folder on disk, exists.
dmsDeletePermanently(id)–Permanently deletes a file from the DMS.
getAttachmentsSize(ids)–Size of the file(s) in bytes.
createDir(folder) / removeFolder(dir) / copyDir(dirName, newDirName)–Working with folders on the server disk.
getDmsFolder(folderName, parentFolderId?) / createDmsFolder(...) / setDmsFileFolder(id, folderName, parentId?)–Working with folders (DMS tags) inside the DMS.
getCsvFile(filename, options?)Promise<object[]>Loads a stored .csv file as an array of objects.
setDmsTagValue(tagId, value, files=[])Promise<void>Sets the value of a DMS attribute (tag) for the specified documents.
const path = storage.getDmsAbsolutePath(vars['Quote'].getValue());
await lib.libreConvert(path, path.replace('.docx', '.pdf'));

zip – archives

MethodDescription
zip.addDmsFile(dmsfId) / addDmsFiles(dmsfId[])Adds one or more files from the DMS to the zip archive being built.
zip.storeZip()Saves the archive to the temporary folder and returns its path (getTempFilePath()).
zip.getTempFilePath() / getRandomTempFileName()Path to the current temporary file, or a new random temporary file, respectively.
zip.clearTemp()Deletes the temporary archive.
zip.unzipDmsFile(dmsFileId)Extracts a zipped file from the DMS and attaches its contents to the case.
zip.gzip(data) / zip.gunzip(data)Compression/decompression of a buffer (gzip).
await zip.addDmsFiles(lib.getDMSFileIds('invoice-*').split('|').map(Number));
const zipPath = zip.storeZip();
await lib.storeAttachment('invoices.zip', await storage.getDmsContent(zipPath));

excel – XLSX workbooks

MethodDescription
excel.workbook()New empty workbook instance.
excel.jsonToSheet(data, options?)Creates a worksheet from an array of objects (JSON).
excel.appendWorksheet(workbook, worksheet, name)Adds a worksheet to the workbook under the specified name.
excel.setCell(worksheet, origin='A1', cellValue)Sets the value of a specific cell (e.g. {t: 's', v: 'Celkem'}).
excel.writeToFile(workbook, fileName, options?)Saves the workbook to a file on disk.
excel.writeToDms(workbook, fileName, options?)Saves the workbook to a temporary file and attaches it directly to the case in the DMS.
excel.read(data, options) / readFile(filePath, options) / fromData(...) / fromFile(...)Load an existing XLSX from data/a file into a workbook object.
excel.utils()Returns the helper functions of the xlsx library (see its documentation).
const rows = vars['Items'].getDR() ? JSON.parse(vars['Items'].getDR('itemName')) : [];
const sheet = excel.jsonToSheet(rows.map((name, i) => ({ item: name })));
const wb = excel.workbook();
excel.appendWorksheet(wb, sheet, 'Items');
await excel.writeToDms(wb, 'order-items.xlsx');

Related: Calculations API Reference: lib library, Print Templates, Task Calculations.

Documents in Calculations — Complete Guide

DOCX document creation

Syca (opens in a new tab)

Powered by HelpDocs (opens in a new tab)