B4J Question How to open a new tab by viewing a pdf file using B4J as Web Server

Gabino A. de la Gala

Well-Known Member
Licensed User
Longtime User
I am using bj4 as a web server.
I have a web page with three buttons and every time I click on one of these buttons I launch the EjecutarTWP(params) process.

This process, what it does is to execute an external application using JShell that generates a pdf file with the desired results.

What I would like to achieve is that after generating the pdf file it will show it to me in an additional browser tab.

B4X:
Sub EjecutarTWP(Parametros As Map) As String
    Dim jsEjecutarTWP As Shell
    jsEjecutarTWP.Initialize("jsEjecutarTWP", FileRun, params)
    jsEjecutarTWP.WorkingDirectory = Main.settings.Get("PathMyrtusF")
    jsEjecutarTWP.Run(-1)
    Return Parametros.Get("FicheroDestino")
End Sub

Sub jsEjecutarTWP_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
    If Success And ExitCode = 0 Then
        Log("Success")
        Log(StdOut)
        Dim j As HttpJob
        j.Initialize("j", Me)
        j.PostString("http://127.0.0.1:51042/login_example/members/downloadpdf", "FicheroDestino=" & ParamsAux.Get("FicheroDestino") & "&CarpetaDestino=" & ParamsAux.Get("CarpetaDestino"))
        StartMessageLoop
    Else
        Log("Error: " & StdErr)
    End If
End Sub

Sub JobDone(j As HttpJob)
    If j.success Then
        Log(j.Success)       
    Else
        Log(j.errorMessage)
    End If
    j.Release
    StopMessageLoop
End Sub
 

Gabino A. de la Gala

Well-Known Member
Licensed User
Longtime User
This is the server code, right?

Why are you making a http request from the server?

In my land there is a saying: The one you don't know as the one who doesn't see ...

They are blind sticks that I am giving to see if I find a way to open a new tab in the client's browser showing the content of the generated PDF.

Is there any way to do it from the server side?

From the html part I do something similar with the following script
B4X:
<script>
    $( document ).ready(function() {
        // Configuro lo correspondiente a la pulsación del botón 1
        $("#btn1").click(function(e) {
            e.preventDefault();
            $.ajax({
                type: "POST",
                url: "membersHandler",
                data: jQuery.param({ Opcion : "btn1", field2 : "hello2"}), 
               success: function(result)
               {
                   if (result.success) {
                        //window.location  = 'showpdf.html';
                        // Con esta opción hago que el pdf lo abra en una pestaña nueva...
                        setTimeout(function(){
                            window.open(result.PDFGenerado);
                        }, 1000);
                   }
                   else {
                        alert(result.errorMessage);
                   }
               },
                error: function (xhr, ajaxOptions, thrownError) {
                    alert(thrownError);
                }
            });
        });
    });
    </script>

But there are times that it takes to generate the pdf longer than expected and then it gives a 404 error.

That's why I thought it would be more reliable to open the pdf in the _Completed event of the server.
 
Upvote 0

rraswisak

Active Member
Licensed User
Longtime User
in html file usually i call target="_blank" for the url to be opened in new tab, don't know if this will work on jquery as well in your case. so you can try to change this:

B4X:
window.open(result.PDFGenerado);

to

window.open(result.PDFGenerado, '_blank');
 
Upvote 0
Top