Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert gridview to pdf format

Status
Not open for further replies.

taree

Technical User
May 31, 2008
316
US
I am getting the below error message when I try to convert Gridview to pdf format. can someone help me.

thank you

here is the code
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

'Get the HTML from GridView1
Dim sw As New IO.StringWriter()
Dim htw As New HtmlTextWriter(sw)
gvSearch.RenderControl(htw)
Dim html As String = "<html><body>" + sw.ToString() + "</body></html>"

'Set up the response
Response.Clear()
Response.ContentType = "application/pdf"

'Create pdf document
Dim document As New iTextSharp.text.Document(iTextSharp.text.PageSize.A4, 80, 50, 30, 65)

'Create pdf writer, output directly to OutputStream
Dim writer As iTextSharp.text.pdf.PdfWriter = PdfWriter.GetInstance(document, Response.OutputStream)
document.Open()
'Create tempfile to hold the HTML:
Dim tempFile As String = Path.GetTempFileName()
Using tempwriter As New IO.StreamWriter(tempFile, False)
tempwriter.Write(html)
End Using

'Parse the HTML into the document
iTextSharp.text.html.HtmlParser.Parse(document, tempFile)

'Cleanup
document.Close()
writer.Close()

'Delete the tempfile:
File.Delete(tempFile)

writer = Nothing
document = Nothing
Response.[End]()


End Sub




Security Exception
Description: The application attempted to perform an operation not allowed by the security policy. To grant this application the required permission please contact your system administrator or change the application's trust level in the configuration file.

Exception Details: System.Security.SecurityException: Request for the permission of type 'System.Security.Permissions.FileIOPermission, mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' failed.

Source Error:


Line 281:
Line 282: 'Parse the HTML into the document
Line 283: iTextSharp.text.html.HtmlParser.Parse(document, tempFile)
Line 284:
Line 285: 'Cleanup

 
iTextSharp is the entity you want to be asking.

by the looks of it the pdfwriter writes to a file and then reads the file to the response stream.

usually these libraries dump temp files to the temp directory or local user directory.

the writer is attempting to write to a location it cannot access. if a setting exists have the writer export to the App_Data directory (this should exist in the directory structure). aspnet users will need read/write permission to the folder.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
thank you Jason for the reply. What do I need to do in order for the file to be sorted in App_Data directory. I checked the path for the file it goes to the temp folder.

thank you
 
i have no idea. you'll need to talk with users/support of iTextSharp.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thank you Jason! I will do that
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top