Custom function to optimize PDF in FileMaker via DynaPDF
This little function can be called anywhere to optimize a PDF. Of course you should initialize DynaPDF in startup script of your solution. But than you can call OptimizePDF function everywhere and pass in a PDF container value. The PDF is optimized and returned.
If you like add further parameters to decide what optimization to do. The example be low uses a good default setting to reduce PDFs for email delivery.
# Custom Function OptimizePDF(InputPDF)
#
# Optimizes PDF and returns either new one or old one in case of error.
Let ([
PDF = MBS("DynaPDF.New");
OpenResult = MBS("DynaPDF.OpenPDFFromContainer"; PDF; InputPDF);
OpenError = MBS("IsError");
ImportResult = MBS("DynaPDF.ImportPDFFile"; PDF);
ImportError = MBS("IsError");
Options = "FlattenLayers DeleteInvPaths ScaleImages NewLinkNames DeletePrivateData IgnoreZeroLineWidth DeleteAlternateImages DeleteThumbnails";
OptimizeResult = MBS("DynaPDF.Optimize"; PDF; Options; 50; 50; 50; 150; 150; 150; "JPEG"; "JPEG"; "JPEG");
OptimizeError = MBS("IsError");
OutputPDF = MBS("DynaPDF.Save"; PDF; GetAsText(InputPDF));
SaveError = MBS("IsError");
ReleaseError = MBS("DynaPDF.Release"; PDF);
Result = If(
// all okay?
OpenError = 0 and ImportError = 0 and OptimizeError = 0 and SaveError = 0 and MBS( "Container.GetSize"; OutputPDF; "PDF " ) > 0;
// than return new PDF
OutputPDF;
// return old PDF
InputPDF)
];
result )