OpenOutputFile in DynaPdfMBS

Hi, I have a problem with a method of the DynaPdf MBS library, in particular with the “OpenOutputFile” method. in practice, this method in most cases works correctly but, occasionally, it does not give a positive result!

I believe a temporary file, each time with a different name. Despite everything, sometimes it returns False, as if it couldn’t open the file (I simulate the situation using a breakpoint, going to open the file from the outside, for example with a pdf reader… in this case the exception always throws it ).
Anyone have an idea? a bug?

(Xojo 2019 r3.1 version, DynaPdfMBS version 21.1, but I’m not sure about that… 50531KB)

Well, if you overwrite an existing file, it may be open by some other app like the pdf viewer or the indexing tool from the OS.

Your app can pick another file name automatically and try again.
Or you create PDF in memory and later write it out with binary stream.

No, I’m pretty sure the file is new (I use a timestamp to generate its name, eg: yyyymmaaHHMMSS.pdf)

If I understand correctly, I create the file right from a document in memory (I carry over a portion of the code)

if pdfDoc.HaveOpenDoc then
  if f.IsWriteable then
    if not pdfDoc.OpenOutputFile(f) then
      app.logger.Logga("Impossibile scrivere il file PDF! [Utils_ScriviFile]", True)
      MsgBox("Impossibile scrivere il file PDF! [Utils_ScriviFile]")
    end if
    if myFlag then 
      call pdfDoc.CloseFileEx(pwdToApply, "XXXXXXXXX", 0, 0)
      call pdfDoc.CloseFile 
    end if
    MsgBox("errore in [Utils_ScriviFile]: f.IsWriteable = False")
  end if
  MsgBox("errore in [Utils_ScriviFile]: pdfDoc.HaveOpenDoc = False")  
end if

Are you creating new FolderItem instances? 2019 uses the older FolderItem system APIs where the reference is able to follow files you relocate. If you are not creating a new FolderItem instance, you may be accessing a file you aren’t expecting.

The framework has a function to help with this. Use GetTemporaryFolderItem to create a reference to a unique FolderItem in the temporary folder.

Does that overwrite an existing PDF file even if it is open in Preview or Adobe?

No, each time it’s a different file, named after a timestamp

do you mean if the folder where the file will go already exists? yes, the folder exists, it’s just the file that is created inside it, each time with a different name. it’s a folder that I use specifically for these temporary pdfs. Before calling the procedure, I clean up the folder among other things, in order to avoid the accumulation of files.

Unfortunately I keep having problems all the time. By the way I think this msgbox is launched directly from the implementation of the method (it would be useful to have confirmation of this)

You can prevent the dialog to show if you change Error event in your DynaPDFMBS subclass.

Also you can assign TraceFile property to log all calls on DynaPDF.
And you may want to log the path and check yourself if the file is invalid or the file is in use.

Thank you. I later noticed that the error window is born in the error event. I will also try with your suggestion, even if the file I create I’m sure doesn’t exist (and first I also check if “fld.IsWriteable”)

unfortunately I can not understand how it is possible that, sometimes, you can not write the file with OpenOutputFile.

The filename is always different (a timestamp going down to seconds, with a random trailing part).

I say “sometimes”, as the function works correctly in most cases (therefore, I would exclude the problem of permissions on the folder: either you have them or you don’t).