KNOWLEDGE BASE
Log In    |    Knowledge Base    |    4D Home
Tech Tip: Dealing with VP EXPORT DOCUMENT's asynchronicity
PRODUCT: 4D | VERSION: 20 | PLATFORM: Mac & Win
Published On: September 10, 2024

When working with View Pro and multiple View Pro documents in an application, it may be desirable to export multiple documents into PDFs at once. However when doing so, an issue may arise where the resulting PDF's would have the wrong content, or be multiple copies of the same document, despite being named differently. This is because VP EXPORT DOCUMENT (as well as VP IMPORT DOCUMENT) are asynchronous commands. They must be dealt with in a different way for the export to work correctly, as the export must be completed before starting the next one.

To work around this, one way is to have the creation of the next PDF be put in the callback method of the VP EXPORT DOCUMENT so that it would create the PDFs sequentially. Using a loop wouldbe the wrong logic in this case, as it would not allow the export to finish cleanly, but using signals or timers are viable options to emulate a synchronous, block style of coding.

To achieve this, the View Pro document would be imported into a offscreen VP area using VP Run offscreen area. In the OffScreenArea class, the onEvent function would handle the cases of importing and exporting, with a signal or timer set to each case to ensure that the commands are completed before running the next document.

Class constructor($param_o : Object)
  This.srcPath:=$param_o.srcPath
  This.isWaiting:=False
  var $pdfName_t : Text
  $pdfName_t:=File(This.srcPath; fk platform path).name+".pdf"
  This.desPath:=File(This.srcPath; fk platform path).parent.platformPath+$pdfName_t
  This.autoQuit:=False

// This function will be called on each event for the offscreen area
Function onEvent()
 Case of
  : (FORM Event.code=On VP Ready)
     VP IMPORT DOCUMENT(This.area; This.srcPath)
     This.isWaiting:=True
     SET TIMER(60)
  : (FORM Event.code=On VP Range Changed)
    If (This.isWaiting)
      SET TIMER(60)
    End if
  : (FORM Event.code=On Timer)
    SET TIMER(0)
    VP EXPORT DOCUMENT(This.area; This.desPath; New object("format"; vk 4D View Pro format; "formula"; Formula(ACCEPT)))
 End case


To learn more about VP Run offscreen area, please refer to the documentation below:
https://developer.4d.com/docs/ViewPro/commands/vp-run-offscreen-area