B4J Question Using PowerShellConvertToPdf with JRDC2 (SOLVED)

LGS

Member
Licensed User
Longtime User
Hello everyone
I am creating a pdf file in a Handle(JRDC2) using PowerShellConvertToPdf

PostString is sent from B4A:
Dim j As HttpJob
j.Initialize("", Me)
j.PostString(link,dataToPdf)
Wait For (j) JobDone(j As HttpJob)
If j.Success Then
   Log("<" & j.GetString & ">")
End If

In the Handle(req As ServletRequest, resp As ServletResponse):
try
   WD.PowerShellConvertToPdf(FileIn,FileOut, False)
   resp.Write("Created PDF")
catch
   Log(LastException)
   resp.Write("Not Created PDF")
End Try

Ok, so far so good. The pdf file is created
B4A immediately shows::
Log("<" & j.GetString & ">") => "<Created PDF>"

But on the server, PowerShellConvertToPdf takes about 3 seconds to create the pdf file.
So I understand that when B4A receives in j.GetString "<Pdf Created>", in reality the file does not yet exist on the server.

What I need?
Validate that the pdf file has been created successfully

What have I tried to do?
Use Wait for:
'I changed this:
WD.PowerShellConvertToPdf(FileIn,FileOut, False)
resp.Write("Created PDF")

'For this
Wait For (WD.PowerShellConvertToPdf(FileIn,FileOut, False)) Complete (Success As Boolean)
If Success Then
    resp.Write("Pdf Creado")
Else
    resp.Write("Pdf No Creado")
End If

Result in B4A:
Log("<" & j.GetString & ">") => "<>"

Second try::
WD.PowerShellConvertToPdf(FileIn,FileOut, False)
Dim exists As Boolean = False
Do While exists = False
    If File.Exists(dir, name) Then
        exists = True
        Log("already created")
    Else
        Sleep(400)
        Log("wait")
    End If
Loop
resp.Write("Created PDF")

Result in B4A:
Log("<" & j.GetString & ">") => "<>"

How can I validate in the Handle that the pdf file was created successfully, so that B4A receives "Pdf Created"?

I appreciate any ideas or suggestions.
 
Solution
Hello everyone
I am creating a pdf file in a Handle(JRDC2) using PowerShellConvertToPdf

PostString is sent from B4A:
Dim j As HttpJob
j.Initialize("", Me)
j.PostString(link,dataToPdf)
Wait For (j) JobDone(j As HttpJob)
If j.Success Then
   Log("<" & j.GetString & ">")
End If

In the Handle(req As ServletRequest, resp As ServletResponse):
try
   WD.PowerShellConvertToPdf(FileIn,FileOut, False)
   resp.Write("Created PDF")
catch
   Log(LastException)
   resp.Write("Not Created PDF")
End Try

Ok, so far so good. The pdf file is created
B4A immediately shows::
Log("<" & j.GetString & ">") => "<Created PDF>"

But on the server, PowerShellConvertToPdf takes about 3 seconds to create the pdf file.
So I understand that when B4A receives in j.GetString "<Pdf Created>", in reality the file does not yet exist on the server.

What...

William Lancee

Well-Known Member
Licensed User
Longtime User
I tried a few things.
This waits for completion.

B4X:
    Dim wd As WordUtils
    wd.Initialize
    Wait For (wd.PowerShellConvertToPdf("C:\Users\willi\Desktop\WDocs\Jazz.docx", "C:\Users\willi\Desktop\Test.pdf", False)) Complete (Success As Boolean)

A very handy tool to create PDFs!
 
Upvote 0

teddybear

Well-Known Member
Licensed User
Hello everyone
I am creating a pdf file in a Handle(JRDC2) using PowerShellConvertToPdf

PostString is sent from B4A:
Dim j As HttpJob
j.Initialize("", Me)
j.PostString(link,dataToPdf)
Wait For (j) JobDone(j As HttpJob)
If j.Success Then
   Log("<" & j.GetString & ">")
End If

In the Handle(req As ServletRequest, resp As ServletResponse):
try
   WD.PowerShellConvertToPdf(FileIn,FileOut, False)
   resp.Write("Created PDF")
catch
   Log(LastException)
   resp.Write("Not Created PDF")
End Try

Ok, so far so good. The pdf file is created
B4A immediately shows::
Log("<" & j.GetString & ">") => "<Created PDF>"

But on the server, PowerShellConvertToPdf takes about 3 seconds to create the pdf file.
So I understand that when B4A receives in j.GetString "<Pdf Created>", in reality the file does not yet exist on the server.

What I need?
Validate that the pdf file has been created successfully

What have I tried to do?
Use Wait for:
'I changed this:
WD.PowerShellConvertToPdf(FileIn,FileOut, False)
resp.Write("Created PDF")

'For this
Wait For (WD.PowerShellConvertToPdf(FileIn,FileOut, False)) Complete (Success As Boolean)
If Success Then
    resp.Write("Pdf Creado")
Else
    resp.Write("Pdf No Creado")
End If

Result in B4A:
Log("<" & j.GetString & ">") => "<>"

Second try::
WD.PowerShellConvertToPdf(FileIn,FileOut, False)
Dim exists As Boolean = False
Do While exists = False
    If File.Exists(dir, name) Then
        exists = True
        Log("already created")
    Else
        Sleep(400)
        Log("wait")
    End If
Loop
resp.Write("Created PDF")

Result in B4A:
Log("<" & j.GetString & ">") => "<>"

How can I validate in the Handle that the pdf file was created successfully, so that B4A receives "Pdf Created"?

I appreciate any ideas or suggestions.
Try this code.

B4X:
'Modify your handle class'

Sub Handle(req As ServletRequest, resp As ServletResponse)
    ConvertToPdf(req ,resp)
    StartMessageLoop
End Sub

Sub ConvertToPdf(req As ServletRequest,resp As ServletResponse)
    ......
    ......
    WD.PowerShellConvertToPdf(FileIn,FileOut, False)
    resp.Write("Created PDF")

    'For this
    Wait For (WD.PowerShellConvertToPdf(FileIn,FileOut, False)) Complete (Success As Boolean)
    If Success Then
        resp.Write("Pdf Creado")
    Else
        resp.Write("Pdf No Creado")
    End If
    StopMessageLoop
End Sub
 
Upvote 0
Solution

OliverA

Expert
Licensed User
Longtime User
Purely out of curiosity, what was wrong with #2?
The StartMessageLoop / StopMessageLoop was missing. In a server handler (which executes in its own thread), any type of Sleep(), Wait For, etc. will exit the thread prematurely. To avoid that, use StartMessageLoop to capture the events and use StopMessageLoop when finished (allowing the thread to finish)
 
Upvote 0

LGS

Member
Licensed User
Longtime User
Purely out of curiosity, what was wrong with #2?
William, as Oliver says:
It was necessary to include StartMessageLoop / StopMessageLoop

Oliver, thanks for the explanation of the topic.

teddybear thanks for guiding me to the solution

Thanks code's brothers šŸ¤
 
Upvote 0
Top