ftos.files.pdf.optimize
Starting with v24.3.0, this is renamed from optimizePDF to ftos.files.pdf.optimize.
Reduces the size of a PDF file and returns its content using the Base64 encoding.
The function can process either the path to a PDF file saved on the server or the actual file content encoded using the Base64 scheme.
The resulting optimized PDF is based on a number of modifications, such as:
- Removal of unused objects
- Merging identical resource streams
- Removal of unused resource streams
- Removal of embedded fonts
- Removal of private information (page piece information)
- Merging identical pages
- Image compression
- Setting a maximum resolution for images
This is a business logic method for business service components.
Syntax
function ftos.files.pdf.optimize(source: string): string;
| Parameter | Description |
|---|---|
source
|
Either:
|
Return Value
Returns a string value that contains the optimized file encoded using the Base64 scheme.
Examples
In this example, on the client side:
- We include a button called optimize on the upload form.
- The button calls the optimizePdfs endpoint and sends it the entity name, record ID, and the content of the attachments file attribute as input parameters. For more information see the callActionByNameAsync and formData Client SDK documentation.
- We use the newFilesAttribute value returned from the server automation script to update the attachments attribute.
The client-side script includes the following code:
$('#optimize').on('click', function (event) {
ebs.callActionByNameAsync('optimizePdfs', {'entityName': formData.entityName, 'recordId': formData.id, 'filesAttribute': formData.model.attachments})
.then (function (result) {
formData.model.attachments = result.UIResult.Data.newFilesAttribute;
})
});
On the server side:
- We retrieve the content of the attachments attribute using the filesAttribute input parameter.
- For each file:
- We optimize the content.
- We delete the original file (see ftos.files.recycle for details).
- We save the optimized file under the same name (see ftos.files.upload for details).
- We capture the real name of the newly optimized file.
- We use the newFilesAttribute variable to rebuild the content of the attachments attribute with the updated real names of the optimized files.
- We use newFilesAttribute to update the attachments attribute:
- In the database (see ftos.data.update for details).
- On the client's form, by sending it back as a response to the client call (see ftos.context.response.setData for details) to be updated accordingly (see the client-side code above).
The optimizePdfs endpoint runs a script that includes the following code:
let filesAttribute = JSON.parse(ftos.context.data.filesAttribute);
let newFilesAttribute = [];
filesAttribute.forEach(prepare);
function prepare(myPdf){
let name = myPdf.Name;
let realName = myPdf.RealName;
let content = ftos.files.pdf.optimize(myPdf.RealName);
ftos.files.recycle(realName);
realName = ftos.files.upload(name, content).realName;
newFilesAttribute.push({"Name": name, "RealName": realName})
}
ftos.data.update(ftos.context.data.entityName, ftos.context.data.recordId, {'attachments' : JSON.stringify(newFilesAttribute)});
ftos.context.response.setData({'newFilesAttribute' : JSON.stringify(newFilesAttribute)});