Friday, March 23, 2012

PDF creation automation

hello

is it possible to generate PDF files from reports programmatically ?

Not quite sure what you mean

but you CAN export a report to PDF format, as well as subscribe to a report with PDF format (auto-sent to your e-mail, or to a network location)

|||

Hi,

yep you can do this. As the Reporting Services exposes a webservice interface you can call the Render() method on the Reporting Services.

See this sample: http://msdn2.microsoft.com/en-us/library/aa258532(SQL.80).aspx

HTH, Jens K. Suessmeyer.

http://www.sqlserver2005.de

|||Yes it is, although you might have to go to great lengths to accomplish it.

And if you do it enough, it might be worth your while to create a class to speed things along. We took this approach when trying to auto-export a report to a PDF file with our system.

The following code was adapted from Chris O'Connor's Channel9 blog (http://channel9.msdn.com/ShowPost.aspx?PostID=87032)

First off, we created a separate Reporting Service Exporter class to use from anywhere inside our enterprise system. This required adding a web reference, which we added to the Objects portion of our project. Import that namespace, and create the class:

Imports Objects.ReportingService

Public Class ReportingServiceExporter

#Region " Properties "

Public Property ReportPath() As String
Get
Return Me.m_sReportPath
End Get
Set(ByVal Value As String)
Me.m_sReportPath = Value
End Set
End Property

Public Property FilePath() As String
Get
Return Me.m_sFilePath
End Get
Set(ByVal Value As String)
Me.m_sFilePath = Value
End Set
End Property

#End Region

#Region " Constructors "

#End Region

#Region " Declarations "
'File types to export to
'HTML 3.2 and 4.0 were removed for simplicity's sake
Public Enum ReportExportFileType
XML
NULL
CSV
IMAGE
PDF
MHTML
EXCEL
HTMLOWC
End Enum

Private Params As New Hashtable
Private m_sReportPath As String = String.Empty
Private m_sFilePath As String = String.Empty

#End Region

#Region " Functions "

Private Function LoadParams() As ReportingService.ParameterValue()

Dim array(Params.Count - 1) As ReportingService.ParameterValue
Dim i As Integer = 0

'Since the Parameter array uses a key-value pair, use a DictionaryEntry to hold the necessary information to construct the parameter array
For Each de As DictionaryEntry In Params
Dim pValue As New ParameterValue
pValue.Name = de.Key.ToString()
pValue.Value = de.Value.ToString()
array(i) = pValue
i += 1
Next

Return array

End Function

Public Function ExportReport(ByVal outputFormat As ReportExportFileType) As Boolean
'Renders the report to the file type specified by outputFormat
'Requires the ReportPath() and FilePath() properties to be set prior to the function call

Dim objReportingService As ReportingService.ReportingService
Dim results As Byte()
Dim fs As System.IO.FileStream
Dim sOutputFormat As String = outputFormat.ToString()

'REQUIRED for method call - but not used
Dim DeviceInfo As String = Nothing
Dim HistoryId As String = Nothing
Dim ShowHideToggle As String = Nothing
Dim RSCredentials As DataSourceCredentials() = Nothing

'OUTPUT variables
Dim RSParamsUsed As ParameterValue()
Dim RSWarnings As Warning()
Dim Encoding As String
Dim MimeType As String
Dim StreamIds As String()



Try

objReportingService = New ReportingService.ReportingService
objReportingService.Url = "http://REPORTSERVER/reportserver/ReportService.asmx" 'This maps to the location of the ReportingServices Web Service; available (by default) in the reportserver folder on your Report Server
objReportingService.Timeout = 600000 'Report will run up to ten minutes before timing out
objReportingService.Credentials = System.Net.CredentialCache.DefaultCredentials

'Renders the report to the results byte array
results = objReportingService.Render(m_sReportPath, sOutputFormat, HistoryId, DeviceInfo, LoadParams(), _
RSCredentials, ShowHideToggle, Encoding, MimeType, RSParamsUsed, RSWarnings, StreamIds)

'Delete any existing file with the same file name
System.IO.File.Delete(m_sFilePath)

'Write to the specified file
fs = System.IO.File.OpenWrite(m_sFilePath)
fs.Write(results, 0, results.Length)
fs.Close()

objReportingService.Dispose()
objReportingService = Nothing
'GC.Collect()

Return True

Catch ex As Exception
DisplayError(ex)
MsgBox("Report Export Failed")
Return False

Finally
objReportingService = Nothing
results = Nothing
fs = Nothing
'GC.Collect()
End Try
End Function

Public Sub AddParameter(ByVal name As String, ByVal value As Object)
Params.Add(name, value)
End Sub

#End Region

End Class

Next, from wherever the event that triggers the export is located, you need to create the Exporter object and set it's parameters, then call the ExportReport function to render the report:

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e as System.EventArgs) Handles Button1.Click
Dim exportSucceeded As Boolean

'Prepare the report for exporting
'The ReportingServiceExporter FilePath and ReportPath properties MUST be set before exporting
Dim exporter as New ReportingServiceExporter

'Target output file path - change the extension to whatever format you need to export to
exporter.FilePath = "C:\temp\report.pdf"

'Path to the report on the server, starting from //REPORTSERVER/reportserver, where the web reference inside the ReportingServiceExporter is loaded from; make sure you don't type the .rdl extension
exporter.ReportPath = "/Reporting Services/ReportName"

'Next, simply add the parameters that the report requires
exporter.AddParameter("Param1", value)
exporter.AddParameter("Param2", value)
exporter.AddParameter("Param3", value)

'Finally, call the ExportReport method with the enumerated type of the format
exportSucceeded = exporter.ExportReport(ReportingServiceExporter.ReportExportFileType.PDF)

That's pretty much all there is to it. Hope this helps.
|||thanks for your suggestions.

the idea is replacing an existing solution which creates PDF files from Business Objects automatically.
The datasource contains information for different individuals, and the automatic PDF creation process creates one file per individual.

No comments:

Post a Comment