Dynamic Pdf Download

Stimulsoft Reports.JS discussion
sivaguru
Posts: 2
Joined: Thu Jan 04, 2018 11:52 am

Dynamic Pdf Download

Post by sivaguru »

I am using Stimulsoft-Reports-PHP-2016.1-Pack version,at the same time design a template(sample_report). There give a option preview and download the pdf.In this same mrt file needs to connect with data-base and download in the form of pdf,so i am using java script.

<script src="/stimulsoft.reports.js" type="text/javascript"></script>
<div id="sample_report"/>
<script>

var report = new Stimulsoft.Report.StiReport();
// Load report from url
report.loadFile("http://atandra/sample_report.mrt");
// Render report
report.render();
var settings = new Stimulsoft.Report.Export.StiPdfExportSettings();
// Create an PDF service instance.
var service = new Stimulsoft.Report.Export.StiPdfExportService();

// Create a MemoryStream object.
var stream = new Stimulsoft.System.IO.MemoryStream();
// Export PDF using MemoryStream.
service.exportTo(report, stream, settings);

// Get PDF data from MemoryStream object
var data = stream.toArray();
// Get report file name
var fileName = String.isNullOrEmpty(report.reportAlias) ? report.reportName : report.reportAlias;
// Save data to file
Object.saveAs(data, fileName + ".pdf", "application/pdf");
</script>
in this code download the template but not data rendering.
I attached expected output pdf file(sample_report_with_data.pdf),sample html file,downloaded pdf image and those sample mrt.This will help to understand my problem,please tell some suggestions
Attachments
sample_report_with_data.pdf
(7.66 KiB) Downloaded 559 times
HighAley
Posts: 8430
Joined: Wed Jun 08, 2011 7:40 am
Location: Stimulsoft Office

Re: Dynamic Pdf Download

Post by HighAley »

Hello.

You should use the async methods.
Please, look at this code:

Code: Select all

var pdfSettings = new Stimulsoft.Report.Export.StiPdfExportSettings();
var pdfService = new Stimulsoft.Report.Export.StiPdfExportService();
var stream = new Stimulsoft.System.IO.MemoryStream();
report.renderAsync(function () {
    pdfService.exportToAsync(function () {
        var data = stream.toArray();
        var blob = new Blob([new Uint8Array(data)], { type: "application/pdf" });
        if (window.navigator && window.navigator.msSaveOrOpenBlob) {
            var fileName = (report.reportAlias == null || report.reportAlias.trim().length == 0) ? report.reportName : report.reportAlias;
            window.navigator.msSaveOrOpenBlob(blob, fileName + ".pdf");
        }
        else {
            var fileUrl = URL.createObjectURL(blob);
            window.open(fileUrl);
        }
    }, report, stream, pdfSettings);
}, false);
Thank you.
facsistemas
Posts: 13
Joined: Mon Jul 18, 2016 12:20 pm

Re: Dynamic Pdf Download

Post by facsistemas »

I am using the same code, but it does not bring any information from the mysql database

Can you help me ?

// Create a new report instance
var report = new Stimulsoft.Report.StiReport();
// Load report from url
report.loadFile("../arquivos/FacRelatorio/concessao/fac_Rel_BEN01006_toten.mrt");

report.dictionary.variables.getByName("Matri").valueObject = '264790';
report.dictionary.variables.getByName("Seq").valueObject = '5';

// Render report
report.render();


var pdfSettings = new Stimulsoft.Report.Export.StiPdfExportSettings();
var pdfService = new Stimulsoft.Report.Export.StiPdfExportService();
var stream = new Stimulsoft.System.IO.MemoryStream();
report.renderAsync(function () {
pdfService.exportToAsync(function () {
var data = stream.toArray();
var blob = new Blob([new Uint8Array(data)], { type: "application/pdf" });
if (window.navigator && window.navigator.msSaveOrOpenBlob) {
var fileName = (report.reportAlias == null || report.reportAlias.trim().length == 0) ? report.reportName : report.reportAlias;
window.navigator.msSaveOrOpenBlob(blob, fileName + ".pdf");
}
else {
var fileUrl = URL.createObjectURL(blob);
window.open(fileUrl);
}
}, report, stream, pdfSettings);
}, false);
Attachments
Report (14).pdf
(33.49 KiB) Downloaded 452 times
Alex K.
Posts: 6488
Joined: Thu Jul 29, 2010 2:37 am

Re: Dynamic Pdf Download

Post by Alex K. »

Hello,

Please try to check the last release build.

If the issue still present, please send us your report template with test data for analysis.

Thank you.
gegar
Posts: 31
Joined: Thu Dec 27, 2018 5:12 pm

Re: Dynamic Pdf Download

Post by gegar »

Hello

I can see my report by viewer.

But when I want to generate the PDF directly it is not working. The data does not show, only the page header.
Why?
My data is from Mysql

in viewer.php i load the data :
// Process SQL data source
viewer.onBeginProcessData = function (event, callback) {
<?php StiHelper::createHandler(); ?>
}


My code for PDF diretly is:
<?php
require_once 'stimulsoft/helper.php';
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Show report as PDF</title>
<link href="css/stimulsoft.viewer.office2013.whiteteal.css" rel="stylesheet">

<!-- Stimusloft Reports.JS -->
<script src="scripts/stimulsoft.reports.js" type="text/javascript"></script>

</head>
<body>
Load 'SimpleList' report template, render and export it.
<br><br>
<?php
$options = StiHelper::createOptions();
$options->handler = "Y-handler.php";
$options->timeout = 30;
StiHelper::initialize($options);
?>

<script type="text/javascript">
// Manage export settings on the server side

// Create a new report instance
var report = new Stimulsoft.Report.StiReport();
// Load report from url
report.loadFile("reports/other.mrt");
// Render report
report.render();

var settings = new Stimulsoft.Report.Export.StiPdfExportSettings();
// Create an PDF service instance.
var service = new Stimulsoft.Report.Export.StiPdfExportService();

// Create a MemoryStream object.
var stream = new Stimulsoft.System.IO.MemoryStream();
// Export PDF using MemoryStream.
service.exportTo(report, stream, settings);

// Get PDF data from MemoryStream object
var data = stream.toArray();
// Get report file name
var fileName = String.isNullOrEmpty(report.reportAlias) ? report.reportName : report.reportAlias;
// Save data to file
Object.saveAs(data, fileName + ".pdf", "application/pdf");
</script>

<a href="#" onclick="saveReportPdf()">Save PDF report to file</a><br>
<br><br>
<div id="htmlContainer"></div>
</body>
</html>


Please someone has a complete example of how to do it to see the data in the database.

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

Re: Dynamic Pdf Download

Post by Lech Kulikowski »

Hello,

Please send us a sample project which reproduces the issue for analysis.

Thank you.
gegar
Posts: 31
Joined: Thu Dec 27, 2018 5:12 pm

Re: Dynamic Pdf Download

Post by gegar »

Hello

Here the code:

viewer-pdf.php

<?php
if (session_id() == "") session_start(); // Init session data
ob_start(); // Turn on output buffering

require_once 'stimulsoft/helper.php';
?>

<!DOCTYPE html>
<html>
<head>
<style>
.loader {
border: 16px solid #f3f3f3;
border-radius: 50%;
border-top: 16px solid red;
border-right: 16px solid blue;
border-bottom: 16px solid yellow;
width: 120px;
height: 120px;
-webkit-animation: spin 2s linear infinite;
animation: spin 2s linear infinite;
}

@-webkit-keyframes spin {
0% { -webkit-transform: rotate(0deg); }
100% { -webkit-transform: rotate(360deg); }
}

@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
</style>
</head>
<body>

<h2>Generate PDF file</h2>

<div class="loader"></div>


<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Company</title>

<!-- Report Office2013 style -->
<link href="css/stimulsoft.viewer.office2013.whiteteal.css" rel="stylesheet">

<!-- Stimusloft Reports.JS -->
<script src="scripts/stimulsoft.reports.js" type="text/javascript"></script>

<?php
$options = StiHelper::createOptions();
$options->handler = "Y-handler.php";
$options->timeout = 30;
StiHelper::initialize($options);
?>
<script type="text/javascript">

function onLoad() {
// Load and show report
var report = new Stimulsoft.Report.StiReport();
report.loadFile("reports/reportname.mrt");

// Process SQL data source
report.onBeginProcessData = function (event, callback) {
<?php StiHelper::createHandler(); ?>
}

report.renderAsync(function() {
var pdfData = report.exportDocument(Stimulsoft.Report.StiExportFormat.Pdf);

var blob = new Blob([new Uint8Array(pdfData)], { type: "application/pdf" });

if (window.navigator && window.navigator.msSaveOrOpenBlob) {
// Internet Explorer does not support the output of blob data, only save as PDF file
var fileName = String.isNullOrEmpty(report.reportAlias) ? report.reportName : report.reportAlias;
window.navigator.msSaveOrOpenBlob(blob, fileName + ".pdf");
} else {
// Show the new tab with the blob data
var fileURL = URL.createObjectURL(blob);
window.open(fileURL,"_self");
};
});
}


</script>
</head>
<body onload="onLoad();">

<div id="viewerContent"></div>
</body>
</html>

Save the report in the same tab.

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

Re: Dynamic Pdf Download

Post by Lech Kulikowski »

Hello,

Please send us a sample project which reproduces the issue for analysis.

Thank you.
dost_mehdi
Posts: 1
Joined: Wed Sep 11, 2019 3:33 pm

Re: Dynamic Pdf Download

Post by dost_mehdi »

Hi there

I have also this problem on version 2018.3.3 and 2019.3.3 on chrome #77 on windows 10
it works fine on chrome #76 but as soon as update to #77 the print as PDF is not working .
i googled for some hours and finally find out that the problem is a deprecated feature on chrome.

https://bugs.chromium.org/p/chromium/is ... ?id=800767

and the demo of problem : https://demo.stimulsoft.com/#Net/Insurance-Light

thank you
HighAley
Posts: 8430
Joined: Wed Jun 08, 2011 7:40 am
Location: Stimulsoft Office

Re: Dynamic Pdf Download

Post by HighAley »

Hello.

We are working on the issue with Chrome 77.
We will let you know when we get any result.

Thank you.

Ticket reference: #913
Post Reply