PDF EXPORT

Hi, Im hoping someone can help me. I want to do what i thought was simple... I have an asp.net web page (vb) which I want to use to run a report and produce a PDF on one button click, There is a requirement that there will be no user intervention. only that they click the button and the pdf is created and saved to a folder on the server.
I have used the following code which seems to run without error until it comes to export the PDF it then errors with a "Object reference not set to an instance of an object." error
the code without the data selection is as below...

Dim fastreport1 As New WebReport
fastreport1.Report.Load(Request.PhysicalApplicationPath + "/reports/harvest_doc1.frx")
fastreport1.RegisterData(dt, dt.TableName)
fastreport1.Prepare()
Dim export1 As New FastReport.Export.Pdf.PDFExport
fastreport1.Report.Export(export1, "d:\result.pdf")

Comments

  • edited 6:28AM
    Dim response As New HttpResponseMessage(HttpStatusCode.OK)
    Using report As New FastReport.Report
        report.Load(frxFile)
        report.Report.RegisterData(dataTable, dataTable.TableName)
        report.Prepare()
    
        Using ms As New System.IO.MemoryStream
            Using pdfExport As New FastReport.Export.Pdf.PDFExport
                pdfExport.Title = "My Report"
                pdfExport.Name = "My_Report"
                pdfExport.EmbeddingFonts = False
                report.Export(pdfExport, ms)
            End Using
    
            ms.Position = 0
            response.Content = New ByteArrayContent(ms.ToArray())
            response.Content.Headers.ContentLength = ms.Length
            response.Content.Headers.ContentType = New System.Net.Http.Headers.MediaTypeHeaderValue("application/pdf")
            response.Content.Headers.ContentDisposition = New ContentDispositionHeaderValue("inline") With {.FileName = "My_Report.pdf"}
            Return response
        End Using
    End Using
    
  • ipong wrote: »
    Dim response As New HttpResponseMessage(HttpStatusCode.OK)
    Using report As New FastReport.Report
        report.Load(frxFile)
        report.Report.RegisterData(dataTable, dataTable.TableName)
        report.Prepare()
    
        Using ms As New System.IO.MemoryStream
            Using pdfExport As New FastReport.Export.Pdf.PDFExport
                pdfExport.Title = "My Report"
                pdfExport.Name = "My_Report"
                pdfExport.EmbeddingFonts = False
                report.Export(pdfExport, ms)
            End Using
    
            ms.Position = 0
            response.Content = New ByteArrayContent(ms.ToArray())
            response.Content.Headers.ContentLength = ms.Length
            response.Content.Headers.ContentType = New System.Net.Http.Headers.MediaTypeHeaderValue("application/pdf")
            response.Content.Headers.ContentDisposition = New ContentDispositionHeaderValue("inline") With {.FileName = "My_Report.pdf"}
            Return response
        End Using
    End Using
    
    Hi
    Thank you for your detailed reply, it is much apreciated.. Can ypu explain to me the use of HttpResponseMessage(HttpStatusCode.OK)
    Iadded the line and Iwas required to create a class, after creating this I get errors at each response.Content line.
    I have not had a lot of experience dealing with this.
    I have attatched a file of my code
  • edited 6:28AM
    i copied the code from webapi project.

    https://docs.microsoft.com/en-us/aspnet/web.../action-results
    http://www.c-sharpcorner.com/UploadFile/03...e-in-webapi166/

    code in html page
    <script type="text/javascript">
        var buttonPDF = function (event) {
            window.open("/api/webgl/fastreportPDF?id=1", "_blank");
        }
    </script>
    

    code in webapi
        <HttpGet>
        Public Function fastreportPDF(ByVal id As Integer) As HttpResponseMessage
            Dim response As New HttpResponseMessage(HttpStatusCode.OK)
            .....
            Return response
        End Function
    
  • edited 6:28AM
    pdf export using HttpHandler

    html page
    <script type="text/javascript">
        var btnPDF = function (event) {
            window.open("ReportPDF.ashx?ID=COA", "_blank");
        }
    </script>
    

    web.config
    <?xml version="1.0"?>
    <configuration>
        <system.web>
            <httpHandlers>
                <add path="FastReport.Export.axd" verb="*" type="FastReport.Web.Handlers.WebExport"/>
                <add path="ReportPDF.ashx" verb="*" type="MyWebSite.PDFHandler"/>
            </httpHandlers>
        </system.web>
        <system.webServer>
            <handlers>
                <add name="FastReportHandler" path="FastReport.Export.axd" verb="*" type="FastReport.Web.Handlers.WebExport"/>
                <add name="PDFReportHandler" path="ReportPDF.ashx" verb="*" type="MyWebSite.PDFHandler"/>
            </handlers>
        </system.webServer>
    </configuration>
    

    pdfhandler.vb
    Imports System.Web
    
    Public Class PDFHandler
        Implements IHttpHandler
    
        Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
            Get
                Return False
            End Get
        End Property
    
        Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
            'Dim id As String = context.Request.QueryString("ID")
            
            Using report As New FastReport.Report
                report.Load(context.Server.MapPath("~/App_Data/report.frx"))
                report.Report.RegisterData(dt, dt.TableName)
                report.Report.GetDataSource(dt.TableName).Enabled = True
                report.Prepare()
    
                Using ms As New System.IO.MemoryStream
                    Using pdfExport As New FastReport.Export.Pdf.PDFExport
                        report.Export(pdfExport, ms)
                        ms.Position = 0
                        context.Response.ContentType = "Application/PDF"
                        context.Response.AddHeader("Content-Disposition", "inline; filename=report.pdf")
                        ms.CopyTo(context.Response.OutputStream)
                    End Using
                End Using
            End Using
        End Sub
    End Class
    
  • ipong wrote: »
    pdf export using HttpHandler

    html page
    <script type="text/javascript">
        var btnPDF = function (event) {
            window.open("ReportPDF.ashx?ID=COA", "_blank");
        }
    </script>
    

    web.config
    <?xml version="1.0"?>
    <configuration>
        <system.web>
            <httpHandlers>
                <add path="FastReport.Export.axd" verb="*" type="FastReport.Web.Handlers.WebExport"/>
                <add path="ReportPDF.ashx" verb="*" type="MyWebSite.PDFHandler"/>
            </httpHandlers>
        </system.web>
        <system.webServer>
            <handlers>
                <add name="FastReportHandler" path="FastReport.Export.axd" verb="*" type="FastReport.Web.Handlers.WebExport"/>
                <add name="PDFReportHandler" path="ReportPDF.ashx" verb="*" type="MyWebSite.PDFHandler"/>
            </handlers>
        </system.webServer>
    </configuration>
    

    pdfhandler.vb
    Imports System.Web
    
    Public Class PDFHandler
        Implements IHttpHandler
    
        Public ReadOnly Property IsReusable() As Boolean Implements IHttpHandler.IsReusable
            Get
                Return False
            End Get
        End Property
    
        Public Sub ProcessRequest(ByVal context As HttpContext) Implements IHttpHandler.ProcessRequest
            'Dim id As String = context.Request.QueryString("ID")
            
            Using report As New FastReport.Report
                report.Load(context.Server.MapPath("~/App_Data/report.frx"))
                report.Report.RegisterData(dt, dt.TableName)
                report.Report.GetDataSource(dt.TableName).Enabled = True
                report.Prepare()
    
                Using ms As New System.IO.MemoryStream
                    Using pdfExport As New FastReport.Export.Pdf.PDFExport
                        report.Export(pdfExport, ms)
                        ms.Position = 0
                        context.Response.ContentType = "Application/PDF"
                        context.Response.AddHeader("Content-Disposition", "inline; filename=report.pdf")
                        ms.CopyTo(context.Response.OutputStream)
                    End Using
                End Using
            End Using
        End Sub
    End Class
    

    thank you very much for all your help
    Regards
    Duncan

Leave a Comment

Rich Text Editor. To edit a paragraph's style, hit tab to get to the paragraph menu. From there you will be able to pick one style. Nothing defaults to paragraph. An inline formatting menu will show up when you select text. Hit tab to get into that menu. Some elements, such as rich link embeds, images, loading indicators, and error messages may get inserted into the editor. You may navigate to these using the arrow keys inside of the editor and delete them with the delete or backspace key.