views:

322

answers:

1

I am running a month-end process and want to have it automatically create some of the reports that need to be created at that time. I am using rdlc reports. Is there a way to automatically create a PDF from a RDLC report in the background?

+2  A: 

This is easy to do, you can render the report as a PDF, and save the resulting byte array as a PDF file on disk. To do this in the background, that's more a question of how your app is written. You can just spin up a new thread, or use a BackgroundWorker (if this is a WinForms app), etc. There, of course, may be multithreading issues to be aware of.

Warning[] warnings;
string[] streamids;
string mimeType;
string encoding;
string filenameExtension;

byte[] bytes = reportViewer.LocalReport.Render(
    "PDF", null, out mimeType, out encoding, out filenameExtension,
    out streamids, out warnings);

using (FileStream fs = new FileStream("output.pdf", FileMode.Create))
{
    fs.Write(bytes, 0, bytes.Length);
}
Matt Greer
So do I just use a generic ReportViewer (not one on any form)?
Mike Wills
Yes. In fact, you can just instantiate a `LocalReport` object (use its default constructor then set the `ReportPath` or `ReportEmbeddedResource` property) and use it on its own. It's very common to use a ReportViewer that's just in memory to take advantage of its export/render capabilities
Matt Greer