Server-Side Rendering and Exporting (2025.1 Version)

Stimulsoft Reports.PHP discussion
Post Reply
servitux
Posts: 18
Joined: Thu Feb 29, 2024 3:29 pm

Server-Side Rendering and Exporting (2025.1 Version)

Post by servitux »

Hello

I have installed the new version 2025.1 and everything is much easier than before. I find the new classes and ways of rendering very useful, although they still use JS instead of PHP.

All the examples on the web have worked for me, both the viewer, the export to PDF in the client-side, and the direct print.

The only one I can't get to work is the server-side rendering. It always returns a nodejs error: "RequestError: Unexpected token '@', "@\x13�,$n�<��<���" is not valid JSON"

I attach a code example, and the result returned by the render function (which is the result of the nodejs object), and comments in any lines for add more information.

The following example runs ok in client-side with viewer, with report export and with report direct print, with the same report file and the sames SQL's datasources.

Code: Select all

Route::any('test', function () {
  \Stimulsoft\StiLicense::setPrimaryKey(file_get_contents(resource_path('reports/license/license.key')));

  $nodejs = new \Stimulsoft\StiNodeJs();
  $nodejs->binDirectory = dirname(config('node.path')); //this path is c:\Program Files\node

  /*$result = $nodejs->installNodeJS();
  if ($result)
    $result = $nodejs->updatePackages();*/ //I've ran this block before, and result is "succesful"

  $report = new \Stimulsoft\Report\StiReport();
  $report->engine = \Stimulsoft\Report\Enums\StiEngineType::ServerNodeJS;
  $report->node = $nodejs; // is necessary???

  $report->onPrepareVariables = function (\Stimulsoft\Events\StiVariablesEventArgs $args) {
    //do something
  };
  
  $report->onBeginProcessData = function(\Stimulsoft\Events\StiDataEventArgs $args) 
  {
    //do something
  };

  $report->onEndProcessData = function(\Stimulsoft\Events\StiDataEventArgs $args)
  {
    //do something
  };

  $report->process();
  $report->load(file_get_contents(resource_path('reports/sales/order/infOrder010.mrt')));

  $result = $report->render();  //this return false, with this message: "RequestError: Unexpected token '@', "@\x13�,$n�<��<���" is not valid JSON"
  if ($result) {
    // After successfully building a report, calling the document export method. Export is also performed using Node.js engine
    // This method will return the byte data of the exported document, or save it to a file
    $filename = Storage::disk('private')->path("/tmp/Order.pdf");
    $result = $report->exportDocument(\Stimulsoft\Export\Enums\StiExportFormat::Pdf, null, false, $filename);

    if ($result !== false) {
        $bytes = strlen($result);
        $message = "The exported document takes $bytes bytes in $filename.";
    }
    else {
        // If there is a export error, you can display the error text
        $message = $report->nodejs->error;
    }
  }
  else {
      // If there is a build error, you can display the error text
      $message = $report->nodejs->error;
  }

  echo $message;
 
});
Thanks for your replies.
Lech Kulikowski
Posts: 7023
Joined: Tue Mar 20, 2018 5:34 am

Re: Server-Side Rendering and Exporting (2025.1 Version)

Post by Lech Kulikowski »

Hello,

Please check the samples:
https://github.com/stimulsoft/Samples-Reports.PHP

If the issue is still present, please send us a sample project that reproduces the issue for analysis.

Thank you.
servitux
Posts: 18
Joined: Thu Feb 29, 2024 3:29 pm

Re: Server-Side Rendering and Exporting (2025.1 Version)

Post by servitux »

Hello again.

Attached is a report designed with version 2025.1 of the Windows Stimulsoft Designer.

The report should be hosted in the “resources” path, inside a new folder called “reports”.

NodeJs version: v22.13.1
Laravel version: 10.10
Stimulsoft PHP library version: 2025.1.4

The eloquent User model is the one that comes with Laravel, without modifications.

The connection is against MySql 9, with the root user. The SQL is "SELECT * FROM users WHERE id > 0"

The web.php attached file contains the same example code for print, viewer and server-side rendering. Print and Viewer runs ok, server-side rendering returns this error message: "RequestError: Unexpected token '~', "~��r\x01l�\x1C�" is not valid JSON"

Thanks
Attachments
stimulsoft example.zip
(3.67 KiB) Downloaded 10 times
Lech Kulikowski
Posts: 7023
Joined: Tue Mar 20, 2018 5:34 am

Re: Server-Side Rendering and Exporting (2025.1 Version)

Post by Lech Kulikowski »

Hello,

We need some additional time to investigate the issue, we will let you know about the result as soon as possible.

Thank you.
#16591
Lech Kulikowski
Posts: 7023
Joined: Tue Mar 20, 2018 5:34 am

Re: Server-Side Rendering and Exporting (2025.1 Version)

Post by Lech Kulikowski »

Hello,

We analyzed the project, the problem turned out to be complex:
1. Node.js client requests did not pass cookies, which are necessary for Laravel framework, so the error “419 - Page expired” was always returned - fixed, cookies are passed in Node.js requests, as well as CSRF-Token headers, which are necessary for operation.

2. It was not possible to learn more about the cause of the error, because no additional data was passed in the above case - fixed, now additional information is passed to errorStack.

3. For some unknown reason, in some cases artisan server hung or looped when requesting data from itself, Node.js script had to timeout, but also looped. The timeout operation has been fixed. In your case this error is not present, but if it is - you can start an auxiliary server for requests, and configure its URL, for example, like this:
php artisan serve
main server http://localhost:8000

php artisan serve --port 8001
an additional server http://localhost:8001

in the code to set the URL for the requests, in your case:
$report->handler->url = "http://127.0.0.1:8001/pdf_test";

In summary, the adjusted code to generate the report in your example will look like this:

Code: Select all

Route::any('pdf_test', function () {
    $report = new \Stimulsoft\Report\StiReport();
    //$report->handler->url = "http://127.0.0.1:8001/pdf_test";
    $report->javascript->relativePath = '../';
    $report->engine = \Stimulsoft\Report\Enums\StiEngineType::ServerNodeJS;

    $report->onPrepareVariables = function (\Stimulsoft\Events\StiVariablesEventArgs $args) {
        //do something
    };

    $report->onBeginProcessData = function(\Stimulsoft\Events\StiDataEventArgs $args)
    {
        switch ($args->dataSource)
        {
            case "users":
            $query = \App\Models\User::where('id', '>', 0);
            break;
        }

        $args->queryString = $query->toRawSql();
    };

    $report->onEndProcessData = function(\Stimulsoft\Events\StiDataEventArgs $args)
    {
        //do something
    };

    $report->process();
    $report->load(file_get_contents(resource_path('reports/users.mrt')));

    $result = $report->render();

    if ($result) {
        // After successfully building a report, calling the document export method. Export is also performed using Node.js engine
        // This method will return the byte data of the exported document, or save it to a file
        $filename = Storage::disk('private')->path(Host::getCurrentHostName()."/tmp/Pedido_30.pdf");
        $result = $report->exportDocument(\Stimulsoft\Export\Enums\StiExportFormat::Pdf, null, false, $filename);

        $message = $result !== false ? $filename : $report->nodejs->error;
    }
    else
        $message = $report->nodejs->error;

    echo $message;
    //dd($report->nodejs->errorStack);
});
Fixes will be available in the next release 2025.1.5 in the next week.

Thank you.
servitux
Posts: 18
Joined: Thu Feb 29, 2024 3:29 pm

Re: Server-Side Rendering and Exporting (2025.1 Version)

Post by servitux »

Hi.

I'm glad the ticket helped solve a problem.

We are waiting for new version soon.
Lech Kulikowski
Posts: 7023
Joined: Tue Mar 20, 2018 5:34 am

Re: Server-Side Rendering and Exporting (2025.1 Version)

Post by Lech Kulikowski »

Hello,

You are welcome.
Post Reply