Dynamic Pdf Download

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

Dynamic Pdf Download

Post by sivaguru » Thu Jan 04, 2018 12:32 pm

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 291 times
HighAley
Posts: 7864
Joined: Wed Jun 08, 2011 7:40 am
Location: Stimulsoft Office

Re: Dynamic Pdf Download

Post by HighAley » Fri Jan 05, 2018 10:25 am

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: 12
Joined: Mon Jul 18, 2016 12:20 pm

Re: Dynamic Pdf Download

Post by facsistemas » Sun Mar 11, 2018 10:33 pm

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 208 times
Alex K.
Posts: 6486
Joined: Thu Jul 29, 2010 2:37 am

Re: Dynamic Pdf Download

Post by Alex K. » Mon Mar 12, 2018 9:42 pm

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.
Post Reply