none
Save SSRS reports to a PDF from .Net code without rendering it in a ReportViewer RRS feed

  • Question

  • Hi

    I have a requirement whereby I need to allow the users to select multiple reports from a list of reports and then print all the selected reports to a single PDF file together without rendering the reports on the .Net page. What I mean to say is, I am giving the users a list of reports with a checkbox against each report name. The user can select multiple reports by ticking the checkboxes. When the user clicks on the "Print Reports" button, all the selected reports must be generated in the backend and the content of all the reports must be saved to a single PDF file.

    I was successful in creating the PDF file but the problem is that only contents of one report are getting written to the file. I am taking byte arrays for each report, saving the content of the report into the byte array and combining the byte arrays into a single byte array at the end and writing the combined array into the PDF. While debugging, I can see that the combined array has the contents of all the reports, but the final PDF shows the contents of the last report only. I do not understand where I am going wrong.

            Microsoft.Reporting.WebForms.Warning warnings;
            string fileName = "test";
            string streamIds;
            string mimeType = string.Empty;
            string encoding = string.Empty;
            string extension = string.Empty;
            ReportViewer viewer = new ReportViewer();
            viewer.ProcessingMode = ProcessingMode.Remote;
            viewer.ServerReport.ReportServerUrl = new Uri("http://server/ReportServer_SQL2008R2");
            viewer.ServerReport.ReportPath = "/Reports/BalanceRangesReport";
    
            ReportViewer viewer1 = new ReportViewer();
            viewer1.ProcessingMode = ProcessingMode.Remote;
            viewer1.ServerReport.ReportServerUrl = new Uri("http://server/ReportServer_SQL2008R2");
            viewer1.ServerReport.ReportPath = "/Reports/GeographicBreakdownReport3";
            
            Byte[] bytes = viewer.ServerReport.Render("PDF");
            Byte[] bytes1 = viewer1.ServerReport.Render("PDF");
    
            Byte[] resultBytes = Combine(bytes, bytes1);
    
            Response.Buffer = true;
            Response.Clear();
            Response.ContentType = mimeType;
            Response.AddHeader("content-disposition", ("attachment; filename=" + fileName + ".") + extension);
            Response.BinaryWrite(resultBytes);
            Response.Flush();

    As mentioned earlier, the byte array resultbytes seems to have data from both the reports when we are debugging the application, but the saved PDF shows only the data from the second report. Please help.

    Thanks

    NModi


    • Edited by NModi Thursday, October 4, 2012 9:11 AM
    Thursday, October 4, 2012 9:10 AM

Answers

  • I don't think this question belongs to this Forum. Could you please post it in a forum specific to SSRS? You will get answers promptly and faster there I guess. Still, I don't want to dishearten you. At least, we can point you where you are going wrong.

    I guess when you Combine 2 byte arrays, it is equivalent to combining (or appending) 2 PDF files. It is not possible to append 2 PDF files like this using .NET. This is where you are going wrong.

    (1) You may be interested in appending 2 PDF files using some 3rd party or Open-source libraries for this task like iTextSharp. How to use this is available here.

    (2) If you don't want Solution-1, I guess you can achieve this using a kind of hidden ReportViewer control. This ReportViewer can render a main report where all your selected reports dynamically render their contents as subreports??? From this hidden ReportViewer control, you can render a single PDF file now.

    Please post this question in SSRS related forums. I believe you will find some experts there.

    • Edited by Rajesh_Kannan Thursday, October 4, 2012 10:27 AM
    • Marked as answer by NModi Thursday, October 4, 2012 12:10 PM
    Thursday, October 4, 2012 10:25 AM

All replies

  • I don't think this question belongs to this Forum. Could you please post it in a forum specific to SSRS? You will get answers promptly and faster there I guess. Still, I don't want to dishearten you. At least, we can point you where you are going wrong.

    I guess when you Combine 2 byte arrays, it is equivalent to combining (or appending) 2 PDF files. It is not possible to append 2 PDF files like this using .NET. This is where you are going wrong.

    (1) You may be interested in appending 2 PDF files using some 3rd party or Open-source libraries for this task like iTextSharp. How to use this is available here.

    (2) If you don't want Solution-1, I guess you can achieve this using a kind of hidden ReportViewer control. This ReportViewer can render a main report where all your selected reports dynamically render their contents as subreports??? From this hidden ReportViewer control, you can render a single PDF file now.

    Please post this question in SSRS related forums. I believe you will find some experts there.

    • Edited by Rajesh_Kannan Thursday, October 4, 2012 10:27 AM
    • Marked as answer by NModi Thursday, October 4, 2012 12:10 PM
    Thursday, October 4, 2012 10:25 AM
  • Hi Rajesh

    Thanks for suggestions. I did post this to SSRS forums and was suggested that I move my question to .Net forum.

    When I am talking about combining 2 byte arrays, at that point of time there is no PDF at all. It is a simple concatenation of 2 arrays and creating a third array. And this third array is used to write contents to a PDF file. So as such the PDF file is being written to only once. So I do not see any merging of PDF files happening here.

    I would like to avoid using Solution 1. And I did not quite understand as to how to render the reports as a subreport of a hidden ReportViewer control. Could you please explain this in a little more detail.

    Thanks

    NModi

    • Edited by NModi Thursday, October 4, 2012 10:44 AM
    Thursday, October 4, 2012 10:41 AM
  • I am very sad to know that you have been redirected from SSRS forums.

    (1) Concatenation of byte arrays - What is it?

    You render the 1st report and 2nd report as 

    Byte[] bytes = viewer.ServerReport.Render("PDF");

    Byte[] bytes1 = viewer1.ServerReport.Render("PDF");

    Now, you have 2 complete valid PDF files in 2 different byte arrays. Assume you write them now into 2 different responses, it is perfect, right?

    Now, you combine these 2 different byte arrays, it means you append 2 complete PDF files. PDF files will have their own structure, right? What happens when you binary-append them.

    A simple exercise: Open 2 PDF files in "Notepad". Go to the end of the file - do you notice the text "EOF"? Copy the contents of 2nd Notepad and paste into at the end of 1st Notepad. Save it. Open the 1st PDF now in Acrobat Reader. Could you understand what you see?

    In my opinion, this is what going wrong in your case. You are tampering the PDF structures by appending 2 PDF file byte contents.

    (2) Rendering as subreports - What is it?

    If you are asking about hidden ReportViewer control - I mentioned to keep your ReportViewer control's Visible property to false.

    If you are asking about main and subreports, it is beyond the scope of this answer. Please refer here.

    Thursday, October 4, 2012 11:14 AM
  • Hi Rajesh

    Thanks for your suggestions. I am able to work this out using iTextSharp dll and merging the pdf files and thereafter allowing the user to download it and save it to user specified location.

    NModi

    Thursday, October 4, 2012 12:10 PM
  • Hi,

    I am following the same process. But getting error that 

    The path of the item '------' is not valid. The full path must be less than 260 characters long.

    what is the reason ? Its urgent for me.

    Thnks in Advance !


    Soumya Das

    Thursday, October 18, 2012 9:40 AM
  • It means that the number of characters in the path you have mentioned exceeds the system defined maximum length of 260 characters. Please refer here:

    PathTooLongException

    The specified path, file name, or both exceed the system-defined maximum length. For example, on Windows-based platforms, paths must be less than 248 characters, and file names must be less than 260 characters.

    Try changing your filename or path where you store your PDF. I hope it should solve this.

    • Marked as answer by NModi Thursday, October 18, 2012 12:30 PM
    • Unmarked as answer by NModi Thursday, October 18, 2012 12:30 PM
    Thursday, October 18, 2012 10:47 AM
  • Thanks Rajesh,

    Now It is working for me.


    Soumya Das

    Thursday, October 18, 2012 12:24 PM
  • How did you do it can you please share
    Thursday, June 18, 2015 7:18 PM