none
Save SSRS Report as pdf using Reporting Services

    Question

  • I've been trying to convert a SSRS Report to PDF and save to
    my local drive using the Reporting Web Services. Though I'm able to
    generate the corresponding pdf file but the contents of the file are
    missing. I've checked that the report I'm trying to convert is not an
    empty one. Only the header section is present there within the generated
    pdf files. Below is the code I'm using:

    protected void GeneratePDFFromReport(object sender, EventArgs e)
        {        
    	RS2005.ReportingService2005 rs;        
    	RE2005.ReportExecutionService rsExec; 
           
    	// Create a new proxy to the web service        
    	rs = new RS2005.ReportingService2005();        
    	rsExec = new RE2005.ReportExecutionService();      
      
    	// Authenticate to the Web service using Windows credentials        
    	rs.Credentials = new System.Net.NetworkCredential("username", "password", "domain");        		
    	rsExec.Credentials = new System.Net.NetworkCredential("username", "password", "domain");        	
    	//rsExec.Credentials = System.Net.CredentialCache.DefaultCredentials;        
    	rs.Url = "http://servername/reportserver/reportservice2005.asmx";        
    	rsExec.Url = "http://servername/reportserver/reportexecution2005.asmx";
    
            string historyID = null;        
    	string deviceInfo = null;        
    	string format = "PDF";        
    	Byte[] results;        
    	string encoding = String.Empty;        
    	string mimeType = "application/pdf";        
    	string extension = String.Empty;        
    	RE2005.Warning[] warnings = null;        
    	string[] streamIDs = null;        
    
    	// Path of the Report - XLS, PDF etc.        
    	string fileName = @"C:\Report\MemberReport.pdf";        
    	// Name of the report - Please note this is not the RDL file.       
     	string _reportName = @"/ReportFolder/ReportName";        
    	string _historyID = null;        
    	bool _forRendering = false;        
    	RS2005.ParameterValue[] _values = null;        
    	RS2005.DataSourceCredentials[] _credentials = null;        
    	RS2005.ReportParameter[] _parameters = null;     
       
    	try        
    	{            
    		_parameters = rs.GetReportParameters(_reportName, _historyID, _forRendering, _values, _credentials);
    		RE2005.ExecutionInfo ei = rsExec.LoadReport(_reportName, historyID);            
    		results = rsExec.Render(format, deviceInfo, out extension, out encoding, out mimeType, out warnings, out streamIDs);   
    		Guid siteID = SPContext.Current.Site.ID;   
    		GuidwebID = SPContext.Current.Site.OpenWeb().ID;            
    		stringbudgetName = string.Empty;            
    		try   
    		{    
    			SPSecurity.RunWithElevatedPrivileges(delegate()    
    			{     
    				using(SPSitesite = newSPSite(siteID))     
    				{      
    					using(SPWebweb = site.OpenWeb(webID))      
    					{       
    						web.AllowUnsafeUpdates = true;                            
    						web.Files.Add(web.Url + "/Shared Documents/SharepointMemberExpenseReport_" + DateTime.Now.ToShortDateString().Replace('/','-') + ".pdf", results, true);       
    						web.AllowUnsafeUpdates = false;      
    					}     
    				}    
    			});   
    		}   
    		catch{ }   
    
    		try            
    		{                
    			FileStream stream = File.Create(fileName, results.Length);                
    			stream.Write(results, 0, results.Length);                
    			stream.Close();            
    		}            
    		catch { }            
    
    		results = null;        
    	}        
    	catch (Exception ex)        
    	{            
    		throw ex;        
    	}    
    }

     

    Any help would highly be appreciated. Thanks.




    Tuesday, December 17, 2013 7:59 PM

Answers

  • use this code, save the byte array into format of pdf

        public static byte[] ExportReportInPDF(ReportViewer ReportViewerCompetency)
            {
                Warning[] warnings;
                string[] streamids;
                string mimeType;
                string encoding;
                string filenameExtension;
    
                byte[] bytes;
                if (ReportViewerCompetency.ProcessingMode == ProcessingMode.Local)
                {
                    bytes = ReportViewerCompetency.LocalReport.Render("PDF", null, out mimeType,
                     out encoding, out filenameExtension, out streamids, out warnings);
                }
                else
                {
                    bytes = ReportViewerCompetency.ServerReport.Render("PDF", null, out mimeType,
                     out encoding, out filenameExtension, out streamids, out warnings);
                }
                return bytes;
            }
        }



    Mark ANSWER if this reply resolves your query, If helpful then VOTE HELPFUL
    INSQLSERVER.COM Mohammad Nizamuddin

    Wednesday, December 18, 2013 5:32 PM

All replies

  • wouldn't it be easier to just pull the PDF using the built-in PDF writer? you can just form the URL correctly to have SSRS output a PDF

    Scott Brickey
    MCTS, MCPD, MCITP
    www.sbrickey.com

    Tuesday, December 17, 2013 8:24 PM
  • Hi Scott, could you please elaborate further?
    Wednesday, December 18, 2013 5:09 PM
  • use this code, save the byte array into format of pdf

        public static byte[] ExportReportInPDF(ReportViewer ReportViewerCompetency)
            {
                Warning[] warnings;
                string[] streamids;
                string mimeType;
                string encoding;
                string filenameExtension;
    
                byte[] bytes;
                if (ReportViewerCompetency.ProcessingMode == ProcessingMode.Local)
                {
                    bytes = ReportViewerCompetency.LocalReport.Render("PDF", null, out mimeType,
                     out encoding, out filenameExtension, out streamids, out warnings);
                }
                else
                {
                    bytes = ReportViewerCompetency.ServerReport.Render("PDF", null, out mimeType,
                     out encoding, out filenameExtension, out streamids, out warnings);
                }
                return bytes;
            }
        }



    Mark ANSWER if this reply resolves your query, If helpful then VOTE HELPFUL
    INSQLSERVER.COM Mohammad Nizamuddin

    Wednesday, December 18, 2013 5:32 PM