Silent printing of reports | SQL Server Performance Forums

SQL Server Performance Forum – Threads Archive

Silent printing of reports

Hi all!<br /><br />I have a question regarding printing of reports. <br /><br />I want to print the report as a pdf supporting page breaks, but I want to do it silent, on the server, without opening acrobat reader. I just want to render the report as usual in the report viewer and then just press a button for saving it as a pdf and print it. I have the utilities necessary for printing a pdf, but I have problem with saving the report as a pdf without launching acrobat reader.<br /><br />I found one way to do this though. I can add an image to the report. Preferable an image of a button. In the image property page there is a tab named Navigation with a field named Jump to url. In this field I instruct the browser to open a new window with an aspx page I#%92ve made and I include all information needed in the url (in my case it is the report name, report folder, parameter names and parameter values. Like this:<br /><br />="http://localhost/WebApplication4/PrintReport.aspx?ReportName=" + Globals!ReportName + ":ReportFolder=MyReports<img src=’/community/emoticons/emotion-4.gif’ alt=’:p‘ />arameterNames=OwnerCode,Date_From,Status:" + "ParameterValues=" + Parameters!OwnerCode.Value + "," + Parameters!Date_From.Value + "," + Parameters!Status.Value<br /><br />In the newly opened aspx page I silent render the report and saves It as a pdf-file as I show in the code below.<br /><br />It works great, but in this case my problem is that the button image is printed as well. I want to hide that image from rendering. Is that possible?<br /><br />So, to summaries this I have two questions:<br />1.Is there a better way to silent print a report supporting page breaks like the pdf-format does then the dirty workaround I have tried?<br />2.Is there a way to programmatic control report items like images and fields so I e.g. can show and hide items as I want to?<br /><br />Kind Regards,<br />Mattias Torngren<br /><br /><br /><br /><br />public void Print()<br />{<br />LocalWebServices.ReportingService rs = new LocalWebServices.ReportingService();<br />rs.Credentials = System.Net.CredentialCache.DefaultCredentials;<br /> <br />// Render arguments<br />byte[] result = null;<br />string reportPath = "/MyReports/TheReport";<br />string format = "PDF";<br />string historyID = null;<br />string devInfo = null;<br /><br />// Prepare report parameter.<br />ParameterValue[] parameters = new ParameterValue[3];<br />parameters[0] = new ParameterValue();<br />parameters[0].Name = "OwnerCode";<br />parameters[0].Value = "Hugo";<br />parameters[1] = new ParameterValue();<br />parameters[1].Name = "Date_From";<br />parameters[1].Value = "2003-10-01";<br />parameters[2] = new ParameterValue();<br />parameters[2].Name = "Status";<br />parameters[2].Value = "All";<br /><br />DataSourceCredentials[] credentials = null;<br />string showHideToggle = null;<br />string encoding;<br />string mimeType;<br />Warning[] warnings = null;<br />ParameterValue[] reportHistoryParameters = null;<br />string[] streamIDs = null;<br />SessionHeader sh = new SessionHeader();<br />rs.SessionHeaderValue = sh;<br /><br />try<br />{<br />result = rs.Render(reportPath, format, historyID, devInfo, parameters, credentials, <br />showHideToggle, out encoding, out mimeType, out reportHistoryParameters, out warnings,<br />out streamIDs);<br />sh.SessionId = rs.SessionHeaderValue.SessionId;<br />Console.WriteLine("SessionID after call to Render: {0}", <br />rs.SessionHeaderValue.SessionId);<br />Console.WriteLine("Execution date and time: {0}",<br />rs.SessionHeaderValue.ExecutionDateTime);<br />Console.WriteLine("Is new execution: {0}",<br />rs.SessionHeaderValue.IsNewExecution);<br />}<br />catch (SoapException e)<br />{<br />Console.WriteLine(e.Detail.OuterXml);<br />}<br />// Write the contents of the report to an MHTML file.<br />try<br />{<br />FileStream stream = File.Create( @"C:TempTheReport.pdf", result.Length);<br />Console.WriteLine( "File created." );<br />stream.Write( result, 0, result.Length );<br />Console.WriteLine( "Result written to the file." );<br />stream.Close();<br />}<br />catch ( Exception e )<br />{<br />Console.WriteLine( e.Message );<br />}<br />}<br /><br /><br /><br /><br />/Mattias
Well, because no one replyed I better do it myself [<img src=’/community/emoticons/emotion-1.gif’ alt=’:)‘ />]. I have found a solution for this problem that works for our organisation. Here it comes:<br /><br />I added an image (of a printer) that the user of the report can click on. The image has a property called "Jump to url" that I’m using for redirect the user to another aspx page I have created. In the url I contain information such as the ReportFolder, ReportName, rendering format (pdf), all parameters name and all parameters value. In the report I have one parameter listing all available printers so the user can choose one before clicking on the print image.<br /><br />I have made one aspx page and one C# object called PrintReport. The aspx file takes care of the values (report folder, report name, parameters etc) I passed through the url, creates the PrintReport object and passes in all values to this object.<br /><br />PrintReport uses the PrintServices web-service to render the report in a pdf-format based on the parameters the user selected. Because I don’t want to print the report displaying the print image I have to hide it before rendering the pdf-file. I’m doing this through specifying a hide image condition in the print image properties. The condition I use is that if the printer parameter is sat to "HidePrintImage" then hide the print image. In my PrintReport object I first read the selected printer parameter and then sets it to "HidePrintImage" before I render the pdf-file. It works great.<br /><br />Then I print the pdf-file through invoking gsprint (comes with ghostview) with System.Diagnostic.Process.Start("gsprint.exe", -printer "TheNameOfThePrinter" "TheNameOfThePDFFile")<br /><br />One important thing to be aware of is that the PrintReport object must be in a COM+ environment and run as a user with the valid printers configured and with the right to print to them. If it’s not in a COM+ environment it runs as the ws_aspnet process as a SYSTEM user (or something like that) and no printers will be found and all sorts of strange errors will be the result.<br /><br />I think that’s all folks. Hope it can help somebody. And if someone comes up with a better solution than please post it here, cause I still think this is sort of a quick and dirty workaround, even if it works.<br /><br />Cheers,<br />/Mattias<br /><br /><blockquote id="quote"><font size="1" face="Verdana, Arial, Helvetica" id="quote">quote:<hr height="1" noshade id="quote"><i>Originally posted by MattiasT</i><br /><br />Hi all!<br /><br />I have a question regarding printing of reports. <br /><br />I want to print the report as a pdf supporting page breaks, but I want to do it silent, on the server, without opening acrobat reader. I just want to render the report as usual in the report viewer and then just press a button for saving it as a pdf and print it. I have the utilities necessary for printing a pdf, but I have problem with saving the report as a pdf without launching acrobat reader.<br /><br />I found one way to do this though. I can add an image to the report. Preferable an image of a button. In the image property page there is a tab named Navigation with a field named Jump to url. In this field I instruct the browser to open a new window with an aspx page I#%92ve made and I include all information needed in the url (in my case it is the report name, report folder, parameter names and parameter values. Like this:<br /><br />="http://localhost/WebApplication4/PrintReport.aspx?ReportName=" + Globals!ReportName + ":ReportFolder=MyReports<img src=’/community/emoticons/emotion-4.gif’ alt=’:p‘ />arameterNames=OwnerCode,Date_From,Status:" + "ParameterValues=" + Parameters!OwnerCode.Value + "," + Parameters!Date_From.Value + "," + Parameters!Status.Value<br /><br />In the newly opened aspx page I silent render the report and saves It as a pdf-file as I show in the code below.<br /><br />It works great, but in this case my problem is that the button image is printed as well. I want to hide that image from rendering. Is that possible?<br /><br />So, to summaries this I have two questions:<br />1.Is there a better way to silent print a report supporting page breaks like the pdf-format does then the dirty workaround I have tried?<br />2.Is there a way to programmatic control report items like images and fields so I e.g. can show and hide items as I want to?<br /><br />Kind Regards,<br />Mattias Torngren<br /><br /><br /><br /><br />public void Print()<br />{<br />LocalWebServices.ReportingService rs = new LocalWebServices.ReportingService();<br />rs.Credentials = System.Net.CredentialCache.DefaultCredentials;<br /> <br />// Render arguments<br />byte[] result = null;<br />string reportPath = "/MyReports/TheReport";<br />string format = "PDF";<br />string historyID = null;<br />string devInfo = null;<br /><br />// Prepare report parameter.<br />ParameterValue[] parameters = new ParameterValue[3];<br />parameters[0] = new ParameterValue();<br />parameters[0].Name = "OwnerCode";<br />parameters[0].Value = "Hugo";<br />parameters[1] = new ParameterValue();<br />parameters[1].Name = "Date_From";<br />parameters[1].Value = "2003-10-01";<br />parameters[2] = new ParameterValue();<br />parameters[2].Name = "Status";<br />parameters[2].Value = "All";<br /><br />DataSourceCredentials[] credentials = null;<br />string showHideToggle = null;<br />string encoding;<br />string mimeType;<br />Warning[] warnings = null;<br />ParameterValue[] reportHistoryParameters = null;<br />string[] streamIDs = null;<br />SessionHeader sh = new SessionHeader();<br />rs.SessionHeaderValue = sh;<br /><br />try<br />{<br />result = rs.Render(reportPath, format, historyID, devInfo, parameters, credentials, <br />showHideToggle, out encoding, out mimeType, out reportHistoryParameters, out warnings,<br />out streamIDs);<br />sh.SessionId = rs.SessionHeaderValue.SessionId;<br />Console.WriteLine("SessionID after call to Render: {0}", <br />rs.SessionHeaderValue.SessionId);<br />Console.WriteLine("Execution date and time: {0}",<br />rs.SessionHeaderValue.ExecutionDateTime);<br />Console.WriteLine("Is new execution: {0}",<br />rs.SessionHeaderValue.IsNewExecution);<br />}<br />catch (SoapException e)<br />{<br />Console.WriteLine(e.Detail.OuterXml);<br />}<br />// Write the contents of the report to an MHTML file.<br />try<br />{<br />FileStream stream = File.Create( @"C:TempTheReport.pdf", result.Length);<br />Console.WriteLine( "File created." );<br />stream.Write( result, 0, result.Length );<br />Console.WriteLine( "Result written to the file." );<br />stream.Close();<br />}<br />catch ( Exception e )<br />{<br />Console.WriteLine( e.Message );<br />}<br />}<br /><br /><br /><br /><br />/Mattias<br /><hr height="1" noshade id="quote"></blockquote id="quote"></font id="quote"><br /><br />/Mattias
]]>