Pdf not saving to memory stream

When writing text to pdf with html tags (purchase receipt - data is from a sql database). It is being saved to a memorystream, then the stream is written out using response.binarywrite. Works the first time, sometimes twice, then fails every time after that. I get an “Object reference not set to an instance of an object” the error says the source is ASPOSE.DBF


I stripped out all the database stuff, creating a stand alone version in an file called TestReceipt.aspx. Works file when this is invoked directly (View in Browser from Visual Studio). So I created a second page with an anchor tag calling TestReceipt.aspx. Again, it works once or twice, then Same problem occurs on the pdfGen.Save(ms) line. Same problem as the database version.

Code is as follows…

Imports Aspose.Pdf
Imports Aspose.Pdf.Text
Imports Aspose.Pdf.DOM
Imports System.IO
Imports System.Web
Imports System.Web.Services

Public Class TestReceipt
Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
Dim license As Aspose.Pdf.License = New Aspose.Pdf.License
license.SetLicense(“Aspose.Total.lic”)

Dim pdfGen As Aspose.Pdf.Generator.Pdf = New Aspose.Pdf.Generator.Pdf()
Dim section1 As Aspose.Pdf.Generator.Section = pdfGen.Sections.Add()

Dim margin As Aspose.Pdf.Generator.MarginInfo = New Aspose.Pdf.Generator.MarginInfo()
margin.Top = 50.0F
margin.Left = 65.0F
margin.Right = 10.0F
margin.Bottom = 50.0F

'Dim sec1 As Aspose.Pdf.Generator.Section = pdfGen.Sections.Add()
'SetFooter(sec1)
'
’ Get receipt data…
'
Dim pid As Integer = 12 ’ Payment ID
'Dim pid As Integer = 19
'Dim oDbf As New DBFunctions
'Dim cSql As String = “exec cr2_Receipts " & pid.ToString
'Dim dt As DataTable = oDbf.ExecDatatable(cSql)
Dim wrk As String = “”
Dim regid As String = “”
Dim memid As String = “”
Dim lu As String = “8888”

Dim htmlString As New StringBuilder(”“)
htmlString.Append(”
“)
htmlString.Append(”

TEST Conference

“)
htmlString.Append(”

Local Union: " & lu & “

”)
htmlString.Append(“
”)

Dim text1 As New Aspose.Pdf.Generator.Text(htmlString.ToString)
text1.TextInfo.IsFontEmbedded = True
section1.Paragraphs.Add(text1)
section1.PageInfo.Margin = margin
section1.IsLandscape = True
text1.IsHtmlTagSupported = True

htmlString.Clear()

Dim amt As Double = 0.0

Dim ii As Integer = 0
For ii = 0 To 2
'
’ Set title only on first item…
'
If ii = 0 Then
'SetTitle(sec1, dt)

htmlString.Append(“”)
'htmlString.Append(“
”)
htmlString.Append(“
”)
htmlString.Append(“
”)
htmlString.Append(“
”)
htmlString.Append(“
”)
htmlString.Append(“
”)
htmlString.Append(“
”)
htmlString.Append(“
”)
'htmlString.Append(“
”)
'htmlString.Append(“
”)
End If

amt = amt + 1
If regid <> “” Then regid &= “|”
If memid <> “” Then memid &= “|”

regid &= 9 + ii
memid &= 63 + ii

Dim member As String
If ii = 0 Then member = “Twiddy, J. Sidlow”
If ii = 1 Then member = “Finney, Charles”
If ii = 2 Then member = “Idiots, Morons R.”
htmlString.Append(“
”)
htmlString.Append(“
”)
htmlString.Append(“
”)
htmlString.Append(“
”)
htmlString.Append(“
”)
htmlString.Append(“
”)

Next
'htmlString.Append(“
NamePhoneEmailAmount

” & member & “999-999-9999bill_beers@ibew.org” & FormatCurrency(amt, 0) & “
”)
’ the end of it…
htmlString.Append(“
”)
htmlString.Append(“” & FormatCurrency(amt, 0) & “”)
'
’ Summary…
'
htmlString.Append(“
”)
'If dt(0)(“TransType”) = “CHK” Then
’ htmlString.Append(“Make Check To…Federal Reserve”)
’ htmlString.Append(“ ”)
’ wrk = dt(0)(“Payable2”)
’ wrk = wrk.Replace(vbCrLf, “
”)
’ htmlString.Append(“Send Check To…” & wrk & “”)
’ htmlString.Append(“ ”)
’ htmlString.Append(“Notice: Registration is not final until check is received.”)
'End If
Dim trantype As String = “CC”
Dim ccType As String = “2”
If trantype = “CC” Then
Select Case ccType
Case “1”
wrk = “Master Card”
Case “2”
wrk = “VISA”
Case “3”
wrk = “American Express”
Case “4”
wrk = “Discover Card”
End Select
htmlString.Append(“” & wrk & " ending in…1111“)
htmlString.Append(”Name on Card…J. Sidlow Twiddy“)
htmlString.Append(” “)
wrk = “a09f4be7-6fd7-44e4-8741-a8708eef7a22”
htmlString.Append(”Transaction ID…" & wrk & “”)
End If
htmlString.Append(“ ”)
htmlString.Append(“Transaction Date…09/04/2015”)
htmlString.Append(“”)

Dim text2 As New Aspose.Pdf.Generator.Text(htmlString.ToString)
section1.Paragraphs.Add(text2)
text2.IsHtmlTagSupported = True
text2.TextInfo.IsFontEmbedded = True

SetFooter(section1, “666”, “7777”)
'
’ Generate Output…
'
Try
Dim ms As New System.IO.MemoryStream()
pdfGen.Save(ms)
Response.Clear()
Response.ClearHeaders()
Response.ClearContent()
Response.Charset = “UTF-8”
Dim downloadBytes As Byte() = ms.ToArray()
Dim msec As String = DateTime.UtcNow.ToString
'Dim fmt As String = “ConfReceipt_LU” & lu & “" & dt(0)(“ConfID”) & "” & pid.ToString & “.pdf”
Dim fmt As String = “ConfReceipt_LU” & lu & “" & msec & "” & pid.ToString & “.pdf”
Response.AddHeader(“Content-Length”, ms.Length.ToString())
Response.AddHeader(“Content-Type”, “application/pdf”)
Response.AddHeader(“Content-Disposition”, [String].Format(“inline; filename={0}”, fmt))
Response.BinaryWrite(downloadBytes)
ms.Close()
ms = Nothing
downloadBytes = Nothing
Response.Flush()
Response.End()
Catch ex As Exception
Dim msg As String = ex.Message
Response.Write(ex.Message)
Response.Write(ex.StackTrace)
Response.End()
End Try
End Sub

Private Sub SetTitle(sec As Aspose.Pdf.Generator.Section)

Dim titletxt As Aspose.Pdf.Generator.Text = New Aspose.Pdf.Generator.Text
sec.Paragraphs.Add(titletxt)

Dim titleseg As Aspose.Pdf.Generator.Segment = titletxt.Segments.Add(“Test Conference”)
titleseg.TextInfo.FontSize = 20

End Sub

Private Sub SetFooter(sec As Aspose.Pdf.Generator.Section, regid As String, memid As String)

Dim footer As Aspose.Pdf.Generator.HeaderFooter = New Aspose.Pdf.Generator.HeaderFooter(sec)
sec.OddFooter = footer
sec.EvenFooter = footer

Dim tab1 As Aspose.Pdf.Generator.Table = New Aspose.Pdf.Generator.Table()
footer.Paragraphs.Add(tab1)
tab1.ColumnWidths = “233 233 233”

tab1.DefaultCellBorder = New Aspose.Pdf.Generator.BorderInfo(Aspose.Pdf.Generator.BorderSide.All, 0.0F)

'Dim txt As Aspose.Pdf.Generator.Text = New Aspose.Pdf.Generator.Text(“Page: ($p of $P )”)
Dim row1 As Aspose.Pdf.Generator.Row = tab1.Rows.Add()
row1.Cells.Add(“Page: ($p of $P )”)
row1.Cells(0).Alignment = Aspose.Pdf.Generator.AlignmentType.Left

Dim wrk As String = “Conf ID: 114 | PID: 12”
row1.Cells.Add(" ")
row1.Cells(1).Alignment = Aspose.Pdf.Generator.AlignmentType.Center

row1.Cells.Add(“Date: $D”)
row1.Cells(2).Alignment = Aspose.Pdf.Generator.AlignmentType.Right
'footer.Paragraphs.Add(tab1)

Dim row2 As Aspose.Pdf.Generator.Row = tab1.Rows.Add()

row2.Cells.Add(regid)
row2.Cells(0).Alignment = Aspose.Pdf.Generator.AlignmentType.Left
row2.Cells.Add(wrk)
row2.Cells(1).Alignment = Aspose.Pdf.Generator.AlignmentType.Center
row2.Cells.Add(memid)
row2.Cells(2).Alignment = Aspose.Pdf.Generator.AlignmentType.Right
'Dim txt2 As Aspose.Pdf.Generator.Text = New Aspose.Pdf.Generator.Text(“Date: $D”)
'footer.Paragraphs.Add(txt2)

End Sub

End Class


Hi Bill,


Thanks for contacting support.

I have tested the scenario using Aspose.Pdf for .NET 10.8.0 in VisualStudio 2010 project with .NET Framework 4.0 running over Windows 7(x64) and I am unable to notice any issue. I have performed multiple executions of project and did not notice any issue. Can you please try using the latest release and in case the issue still persists, please share some sample project and also please share some details regarding your working environment i.e. Operating System, VisualStudio version, .NET Framework version etc.

We are sorry for this inconvenience.

Just upgraded to lastest version (although I believe I had the lastest version). Still the same issue. I am using Windows 7 (x64), VisualStudio 2010 and .NET Framework 4.0. Did you call it repeatedly from a link or anchor tag? Example…


TEST RECEIPT

again it works the first time, then…

Object reference not set to an instance of an object.Aspose.Pdf at œ.’.(˜& ,  , IList ,  , , — ) at ..( , ™ , Int32 , — ) at ..( , ™ , Int32 , — ) at ..(String ) at œ..(String ) at .™.(Pdf , Section , Cell , Text , HeaderFooter , ž , ‰ ) at .™.(Pdf , Section , Table , Cell , Text , HeaderFooter , ž , ‰ ) at .›.(Pdf , Section , HeaderFooter , Table , Row , Cell , Text , ž , ‰ , Boolean ) at ..(Pdf , Section , ž ) at ..(Pdf ) at ..(‘ , Pdf ) at Aspose.Pdf.Generator.Pdf.Save(Stream stream) at ConfAdmin.TestReceipt.Page_Load(Object sender, EventArgs e) in C:\Users\Bill_Beers.IBEW02.DOM\Documents\Visual Studio 2010\Projects\ConfAdmin\ConfAdmin\Reports\TestReceipt.aspx.vb:line 147




Hi Bill,


Can you please share some sample project, so that we can again test the scenario in our environment. We are sorry for this inconvenience.

Well given the time crunch I have, it was way easier to just build a word template, mailmerge it with the database tables and write it out as a pdf. Much less work then bothering with pdf complexities.

Hi Bill,


As long as Aspose.Words fulfill your requirements, you may consider using Aspose.Words. In case you have any further query related to Aspose.Pdf, please do let us know.