Downloading PDF File without using viewer

Stimulsoft Reports.WEB discussion
Post Reply
beginner
Posts: 36
Joined: Thu Jun 18, 2015 5:01 am

Downloading PDF File without using viewer

Post by beginner »

Hi,

I want to let the user download the PDF file directly and without using viewer, please let me know how can I do this in ASP.NET MVC 5. Also how can I set the datasource and variables before exporting the document?

The report is in `mrt` format and my code looks like this:

Code: Select all

            StiReport report = new StiReport();
            report.Load(path);
            //report.Render();
            report.Compile();

            report.RegData("DataSource1", data);
            report.RegData("DataSource2", data2);

            foreach (KeyValuePair<string, string> param in repParams)
            {
                report[param.Key] = param.Value;
            }

Thanks in advance,
HighAley
Posts: 8430
Joined: Wed Jun 08, 2011 7:40 am
Location: Stimulsoft Office

Re: Downloading PDF File without using viewer

Post by HighAley »

Hello.

You could use one of the next examples of printing to PDF or HTML. In both cases the report will be rendered and sent to print automatically.

Code: Select all

// Print to PDF
public ActionResult Print()
{
StiReport report = new StiReport();
report.Load("D:\\ReportXmlAir.mrt");
report.Render(false);

MemoryStream stream = new MemoryStream();

StiPdfExportSettings settings = new StiPdfExportSettings();
settings.AutoPrintMode = StiPdfAutoPrintMode.Dialog;

StiPdfExportService service = new StiPdfExportService();
service.ExportPdf(report, stream, settings);

this.Response.Buffer = true;
this.Response.ClearContent();
this.Response.ClearHeaders();
this.Response.ContentType = "application/pdf";
//this.Response.AddHeader("Content-Disposition", "attachment; filename=\"report.pdf\"");
this.Response.ContentEncoding = Encoding.UTF8;
this.Response.AddHeader("Content-Length", stream.Length.ToString());
this.Response.BinaryWrite(stream.ToArray());
this.Response.End();

return View();
}


// Print to HTML
public ActionResult About()
{
StiReport report = new StiReport();
report.Load("D:\\ReportXmlAir.mrt");
report.Render(false);

MemoryStream stream = new MemoryStream();

StiHtmlExportSettings settings = new StiHtmlExportSettings();
StiHtmlExportService service = new StiHtmlExportService();
service.ExportHtml(report, stream, settings);

StreamReader reader = new StreamReader(stream);
stream.Position = 0;
string html = reader.ReadToEnd();
html = html.Replace("", "");

this.Response.Buffer = true;
this.Response.ClearContent();
this.Response.ClearHeaders();
this.Response.ContentType = "text/html";
//this.Response.AddHeader("Content-Disposition", "attachment; filename=\"report.html\"");
this.Response.ContentEncoding = Encoding.UTF8;
this.Response.AddHeader("Content-Length", html.Length.ToString());
this.Response.Write(html);
this.Response.End();

return View();
}
Thank you.
Post Reply