Skip to content

Convert Excel to PDF via Script ​

Background ​

Currently, there is an attachment field in the data table that contains Excel files. We need to convert these Excel files to PDF by clicking a button and upload the converted PDF files to the corresponding field.

Implementation Principle ​

Use the script interface informat.storage.convertFormat(sourcePath, targetPath, setting) to convert Excel to PDF, then upload and save the converted file to the corresponding data table field path.

Reference documentation: Document Format Conversion

Implementation Steps ​

We can implement this function through the following steps:

  • Create a call automation button in the form
  • Create a code snippet step in the automation
  • Finally, click convert and verify the conversion results

Implementation Code ​

javascript
const record = automatic.getVar("record");

const pdfFileId = informat.utils.randomUUID().replaceAll("-", "") + ".pdf";

const filePath = informat.storage.getFilePath("tab", "pdf", pdfFileId);
informat.storage.convertFormat(record.excel.path, filePath, {
  fileType: "xlsx",
  outputtype: "pdf",
  title: "example.pdf",
});
// Define attachment object
const targetAttachment = {
  id: pdfFileId,
  name: "example.pdf",
  path: filePath,
};
// Update pdf attachment field
informat.table.update("tab", { id: record.id, pdf: targetAttachment });

Summary ​

Through the above example, we can simply convert Excel to PDF with a few parameter configurations.
Based on the above example, we can also extend it to convert Excel to PNG, convert docx files to PDF, etc.