B4J Question Can't delete PDF file

Harris

Expert
Licensed User
Longtime User
Either NextReports or ABPDFBox is holding the file open. I can't delete the files when the app is running.
When the app is terminated, the files can be deleted.

B4X:
Dim PDF As ABPDFBox
PDF.Initialize("mysecretdonationkey" )

Try
        'Run the report and export the output report file
        repexec.reportPrepAndRun(inputlist)   ' generates the report and writes to disk
        Log(" file: "&dir&"\"&fname)
    Catch
        LogDebug("Error: Loading Reports : " & LastException.Message)
    End Try
  
    Sleep(3000)
    If File.Exists(pdfFolder,fn&".pdf") Then
        If fprinter <> "" Then  ' if a printer has been assigned - print te report
            PDF.Print ( pdfFolder, fn&".pdf", fprinter, 1, True)
          
            Sleep(1000)
        Else
            Log("  _____----______ No Printer defined") 
        End If
    Else
        Log(" File PDF not found: "&fn&".pdf")

    End If     

    UpdatePrinted


    If fprinter <> "" Then   ' if a printer is assigned, delete the file....
        Sleep(5000)
        DelPDF( fn&".pdf")

    End If
  
End Sub

Sub DelPDF( pdffile As String)
  
    If File.Exists(pdfFolder,pdffile) Then
      
        Dim del As Boolean =  File.Delete(pdfFolder, pdffile)
      
        If del Then
           Log(" DELETED File PDF -: "&pdffile)
        Else
            Log(" File was NOT DELETED PDF -: "&pdffile)  '  this is returned - files are locked and can't be deleted.
        End If     
    
    Else
        Log(" Can not delete File PDF - not found: "&pdffile)
    End If
      
End Sub
 
Last edited:

Harris

Expert
Licensed User
Longtime User
The file IS found. It can't be deleted (while the app is running).
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
If you are unable to solve it (as the problem is probably in one of the libraries) then you can copy the file to File.DirTemp before you open it and open it there.

You can also try to delete all the temporary files created by your app when the app starts:
B4X:
For Each f As String In File.ListFiles(File.DirTemp)
 If f.StartsWith("MyAppTempPDF_") Then File.Delete(File.DirTemp, f)
Next
 
Upvote 0

Harris

Expert
Licensed User
Longtime User
You can also try to delete all the temporary files created by your app when the app starts:
Since the app runs on the server, it never (rarely) gets shut down.
Now that I am on my server, I will determine which lib has it locked... and hopefully we can get it fixed...

Thanks
 
Upvote 0

Harris

Expert
Licensed User
Longtime User
Ok, commented out the print with ABMPDFBox component.
Files are (can be) deleted after NextReports creates them.
Therefore, the problem lies within ABMPDFBox lib.

Any chance you can look into this @alwaysbusy ? No big hurry, yesterday would have been fine...;)
My concern is that it may be consuming resources by not releasing the handle to the file.
Approx. 20 reports are produced each day - without an app shutdown for months.

Thanks
 
Upvote 0
Top