« Disabled Plugins in F… | Home | Check version of File… »

Normalize PDF orientation with MBS Plugins and DynaPDF

A client got scanned PDFs and they are sometimes showing rotated in FileMaker's container fields. The reason is simple: They are scanned and marked to be rotated. But preview in FileMaker ignores the orientation metadata (see issue 137094).
 
To fix this for the client, we wrote the following script. If first page is rotated, we assume this is a PDF which needs to be normalized to have 0° rotation. So we loop over all pages, rotate the pages with a non zero orientation to have one and add them to the new PDF: 
 
 # Clear current PDF document

Set Variable [ $pdf ; Value: MBS("DynaPDF.New") ] 

# Load PDF from container

Go to Record/Request/Page [ First ]

Set Variable [ $r ; Value: MBS("DynaPDF.OpenPDFFromContainer"; $pdf; Generate Previews::InputPDF) ] 

# Check if first page is rotated

Set Variable [ $rotation ; Value: MBS("DynaPDF.GetImportOrientation"; $pdf; 1) ] 

// Show Custom Dialog [ "Rotation is " & $rotation ] 

If [ $rotation ≠ 0 ] 

# Rotate all pages to 0° if needed

Set Variable [ $page ; Value: 1 ] 

Set Variable [ $pageCount ; Value: MBS( "DynaPDF.GetImportPageCount"; $PDF ) ] 

Loop

# Check current page rotatations

Set Variable [ $rotation ; Value: MBS("DynaPDF.GetImportOrientation"; $pdf; $page) ] 

If [ $rotation = 0 ] 

# import as is

Set Variable [ $r ; Value: MBS( "DynaPDF.ImportPDFPage"; $PDF; $page ) ] 

Else

# import and rotate

Set Variable [ $template ; Value: MBS( "DynaPDF.ImportPageAsTemplate"; $PDF; $page ) ] 

Set Variable [ $template ; Value: MBS( "DynaPDF.RotateTemplate"; $pdf; $template; -$Rotation ) ] 

# now put on a new page:

Set Variable [ $r ; Value: MBS( "DynaPDF.AppendPage"; $PDF) ] 

Set Variable [ $w ; Value: MBS( "DynaPDF.GetTemplWidth"; $pdf; $template ) ] 

Set Variable [ $h ; Value: MBS( "DynaPDF.GetTemplHeight"; $pdf; $template ) ] 

Set Variable [ $r ; Value: MBS( "DynaPDF.SetPageWidth"; $PDF; $w) ] 

Set Variable [ $r ; Value: MBS( "DynaPDF.SetPageHeight"; $PDF; $h) ] 

Set Variable [ $r ; Value: MBS( "DynaPDF.PlaceTemplate"; $PDF; $template; 0; 0; $w; $h) ] 

Set Variable [ $r ; Value: MBS( "DynaPDF.EndPage"; $PDF) ] 

End If

# next page

Set Variable [ $page ; Value: $page + 1 ] 

Exit Loop If [ $page > $pageCount ] 

End Loop

# Save PDF

Set Field [ Generate Previews::OutputPDF ; MBS("DynaPDF.Save"; $pdf; GetAsText ( Generate Previews::InputPDF )) ] 

End If

Set Variable [ $r ; Value: MBS("DynaPDF.Release"; $pdf) ] 


The resulting PDF has all pages in 0° orientation, so the fact that FileMaker doesn't handle this, is no longer a problem and the PDF shows nicely.
Of course this may not work, if the pages are not marked with correct orientation metadata! 
 
Requires MBS FileMaker Plugin + DynaPDF Lite. With DynaPDF Pro, the resulting PDFs can include the preview JPEG to show a preview in a non-interactive container on Windows, see DynaPDF.GeneratePreview.
 
The same can be done in Xojo, see /DynaPDF/Rotate rotated PDF back example project.
Claris FileMaker Plugin
09 03 18 - 14:21