How to Create PDF programmatically using Report Services in VS 2019
I'm studying how to create PDF files of SSRS reports programmatically. This won't be easy.
I use VS 2019 and can create a SSRS report using "Report Server Project" template.
Is there a way to run a report, report data and create a PDF without having to click the
export button? (I code in C#).
ASP.NET
-
Lan Huang-MSFT 25,866 Reputation points • Microsoft Vendor
2023-05-15T09:19:45.6266667+00:00 Hi @Coreysan,
If your purpose is not to export pdf, you can try to write the result directly to the response, and then open the PDF document in the browser.
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="SSRSDemo.WebForm1" %> <%@ Register Assembly="Microsoft.ReportViewer.WebForms, Version=10.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a" Namespace="Microsoft.Reporting.WebForms" TagPrefix="rsweb" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <asp:ScriptManager ID="ScriptManager1" runat="server"> </asp:ScriptManager> <rsweb:ReportViewer ID="ReportViewer1" runat="server" Width="600"> </rsweb:ReportViewer> <asp:Literal ID="ltEmbed" runat="server" /> <asp:Button ID="btnPDF" Text="ShowPDF" runat="server" OnClick="ShowPDF" /> </form> </body> </html>
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { ShowSSRSReport(); } } private void ShowSSRSReport() { ReportViewer1.ProcessingMode = ProcessingMode.Local; ReportViewer1.LocalReport.ReportPath = "C:\\Users\\Administrator\\source\\repos\\Tutorial\\Report1.rdlc"; DataSet dsCustomers = GetData("select * from Tests"); ReportDataSource datasource = new ReportDataSource("DataSet1", dsCustomers.Tables[0]); ReportViewer1.LocalReport.DataSources.Clear(); ReportViewer1.LocalReport.DataSources.Add(datasource); } private DataSet GetData(string query) { string conString = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString; SqlCommand cmd = new SqlCommand(query); using (SqlConnection con = new SqlConnection(conString)) { using (SqlDataAdapter sda = new SqlDataAdapter()) { cmd.Connection = con; sda.SelectCommand = cmd; using (DataSet dsCustomers = new DataSet()) { sda.Fill(dsCustomers, "DataSet1"); return dsCustomers; } } } } protected void ShowPDF(object sender, EventArgs e) { Warning[] warnings; string[] streamIds; string contentType; string encoding; string extension; //Export the RDLC Report to Byte Array. byte[] bytes = ReportViewer1.LocalReport.Render("PDF", null, out contentType, out encoding, out extension, out streamIds, out warnings); if (bytes != null) { Response.ContentType = "application/pdf"; Response.AddHeader("content-length", bytes.Length.ToString()); Response.BinaryWrite(bytes); } }
Best regards,
Lan Huang
-
Coreysan 1,631 Reputation points
2023-05-15T16:22:08.3166667+00:00 Lan, thank you so much for responding. I have some experience writing SSRS, but your code here is a level up from what I know, so thank you - I will try to understand what you sent along.
Here's how I hope to use SSRS:
- A user will open the default Reporting Services page in Tile View.
- He will open an SSRS Report, so lets call it "SSRS_CreatePDF"
- There will be a single multi-select box for the filter, and the user will copy/paste a list of numbers. Lets say the user copies 100 numbers, where each number is 20 digits long
- The report will pass those numbers to a stored procedure in SQL Server.
- the PROC will read the first number, and output all the data for one single report.
(So far, all this is easy. I have all the code in place up to here.)
- For the one report, I would like to create a PDF on the fly, and send the file to a network shared file location.
- Finally, iterate through the rest of the numbers and repeat the process.
- The end result is that the PROC (or whatever mechanism) will have created 100 individual PDF files, and all 100 of them are delivered to a shared file destination where a vendor can pick them up via FTP.
I really wish I could simply create one multi-page PDF file with all 100 pages in it, but our vendor requested they be separated. I'm guessing that the vendor wants to be able to send individual PDF files to various departments on their end.
I have experience building SSRS reports that are available on our default SSRS Report Services server, but your ASP.NET example tells me I should write my own browser-interface for what I need. Is that right? Is there any way to continue using our default server, and compose a standard SSRS report that can create PDFs? From what I see in your example, I see the benefit in using a new ASP.NET Web form, because I can use the code behind to create PDFs in C#. That makes sense. Is that the best way to approach this?
Meantime, I will spend time learning your example, and see what I come up with. I really look forward
to hearing back from you! Again, thank you for helping me.
-
Lan Huang-MSFT 25,866 Reputation points • Microsoft Vendor
2023-05-16T10:03:44.5366667+00:00 Hi @Coreysan,
but your ASP.NET example tells me I should write my own browser-interface for what I need. Is that right? Is there any way to continue using our default server, and compose a standard SSRS report that can create PDFs?
Since your question is about Asp.net tags, I assume you want to create an Asp.net project to implement this functionality.But I read your explanation and it seems you don't want to implement it in ASP.NET. I think you can post about "SQL Server Reporting Services" and ask the SSRS guys for help.
Sign in to comment