Skip to content

Instantly share code, notes, and snippets.

@sonukapoor
Created March 4, 2009 14:25
Show Gist options
  • Save sonukapoor/73851 to your computer and use it in GitHub Desktop.
Save sonukapoor/73851 to your computer and use it in GitHub Desktop.
Public Class RenderUnit
' 1 inch = 72 points
Public WidthPoints As Integer
Public HeightPoints As Integer
Public Items As IEnumerable
Public FileName As String
End Class
Public Interface IPDFLayout
Function Layout() As RenderUnit
End Interface
Public Interface IPDFLayoutHandler
Function GeneratePDF(ByVal renderUnit As RenderUnit) As String
End Interface
Public Interface IDataSource
Function GetData() As IEnumerable
End Interface
Public Class PDFGenerator
Private ReadOnly layout As IPDFLayout
Private ReadOnly layoutHandler As IPDFLayoutHandler
Private ReadOnly dataSource As IDataSource
Public Sub New(ByVal layout As IPDFLayout, ByVal layoutHandler As IPDFLayoutHandler, ByVal dataSource As IDataSource)
Me.layout = layout
Me.layoutHandler = layoutHandler
Me.dataSource = dataSource
End Sub
Public Function Generate() As String
Dim dataItems As IEnumerable = dataSource.GetData()
Dim renderUnit As RenderUnit = layout.Layout()
Return layoutHandler.GeneratePDF(renderUnit)
End Function
End Class
Public Class USCanadaLayout
Implements IPDFLayout
Public Function Layout() As RenderUnit Implements IPDFLayout.Layout
' I am supposed to hardcode this into the function here?
Dim heightInches As Integer = 4
Dim widthInches As Integer = 6
Dim renderUnit As New RenderUnit
renderUnit.HeightPoints = heightInches * 72
renderUnit.WidthPoints = widthInches * 72
renderUnit.FileName = String.Concat(System.Guid.NewGuid.ToString().Replace("-", ""), ".pdf")
Return renderUnit
End Function
End Class
Public Class MannequinLayoutHandler
Implements IPDFLayoutHandler
Public Function GeneratePDF(ByVal renderUnit As RenderUnit) As String Implements IPDFLayoutHandler.GeneratePDF
' generate pdf here based on the renderUnit argument and return the generated filename
End Function
End Class
Public Class MannequinDataSource
Implements IDataSource
Public Function GetData() As System.Collections.IEnumerable Implements IDataSource.GetData
' Note: How do I proceed here, if the data is coming from the client (js array via ajax)?
End Function
End Class
' Is this correct? Quite a few interfaces, no?
Dim layout As IPDFLayout = New USCanadaLayout()
Dim layoutHandler As IPDFLayoutHandler = New MannequinLayoutHandler()
Dim dataSource As IDataSource = New MannequinDataSource()
Dim generator As New PDFGenerator(layout, layoutHandler, dataSource)
generator.Generate()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment