locked
Unable to export to PDF in Firefox Quantum / Chrome RRS feed

  • Question

  • User-2081167373 posted

    Basically, on our website we have a report that lets you export to pdf, excel or just as html.

    ​Things work fine on older browsers (ESR 60), Edge and IE. New versions of Firefox and Chrome will not work with the pdf or excel option.

    Html works just fine, but the other two seem to download a temp file as getparms.aspx and then rename it - the two mentioned browsers no longer seem to allow that file to be downloaded (download failed) and renamed in the users temp folder (probably good for security). 

    From some googling, it seems it could be related to memorystream being used?  Most examples I have seen use embedded CR into the aspx page, which doesn't apply to this case.

    I have been trying a few different things, but everything has failed so far - I'm not a programmer, just trying to fix what was left behind.

    I am including the code for the page that generates the files

    Imports CrystalDecisions.Shared
    Imports CrystalDecisions.CrystalReports.Engine
    Imports CrystalDecisions.Web
    
    Partial Class IndexReport_GetParms
        Inherits System.Web.UI.Page
        Dim blayer As New BAL
    
    #Region " Page_Load "
    
        Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Dim PlantID As Integer = blayer.PlantId
            Dim rptName As String = Request.QueryString("ReportName")
            Dim format As String = Request.QueryString("Format")
            Try
    
                Me.CRSource.Report.FileName = "testreport.rpt"
                Me.CRSource.ReportDocument.SetDatabaseLogon("test", "test1")
                'Me.CRSource.ReportDocument.SetParameterValue(0, 0)
                Me.CRSource.ReportDocument.SetParameterValue(0, PlantID)
                'Me.CRSource.ReportDocument.SetParameterValue(2, rptName)
    
                Dim ms As IO.MemoryStream
                Select Case format
                    Case "PDF"
                        ms = Me.CRSource.ReportDocument.ExportToStream(ExportFormatType.PortableDocFormat)
    
                        Response.Clear()
                        Response.Buffer = True
    
                        Response.AddHeader("Accept-Header", ms.Length.ToString)
                        Response.ContentType = "application/pdf"
                        Response.OutputStream.Write(ms.ToArray(), 0, Convert.ToInt32(ms.Length))
    
                        Response.Flush()
                        Response.Close()
    
                        ms.Close()
                    Case "EXCEL"
                        ms = Me.CRSource.ReportDocument.ExportToStream(ExportFormatType.Excel)
    
                        Response.Clear()
                        Response.Buffer = True
    
                        Response.AddHeader("Accept-Ranges", ms.Length.ToString)
                        Response.ContentType = "application/vnd.ms-excel"
                        Response.OutputStream.Write(ms.ToArray(), 0, Convert.ToInt32(ms.Length))
                        Response.Flush()
                        Response.Close()
    
                        ms.Close()
                    Case "HTML"
                        ms = Me.CRSource.ReportDocument.ExportToStream(ExportFormatType.HTML40)
    
                        Response.Clear()
                        Response.Buffer = True
    
                        Response.AddHeader("Accept-Ranges", ms.Length.ToString)
                        Response.ContentType = "text/HTML"
                        Response.OutputStream.Write(ms.ToArray(), 0, Convert.ToInt32(ms.Length))
    
                        Response.Flush()
                        Response.Close()
    
                        ms.Close()
                End Select
    
            Catch ex As Exception
                Throw ex
            End Try
        End Sub
    
    #End Region
    
    
    End Class

    Ideas? 

    Update - Again, I'm sure related to how it tries to download the .aspx file, this is the error being thrown when trying to export to pdf

    "Resource interpreted as Document but transferred with MIME type application/pdf"

    I can change application/df to text/HTML and it will display - as gibberish of course.

    Monday, November 4, 2019 4:28 PM

Answers

  • User475983607 posted

    I'm surprised the code works given the bugs.

    I assume this line...

    Response.AddHeader("Accept-Header", ms.Length.ToString)

    should be

    Response.AddHeader("content-length", ms.Length.ToString)

    The content-length tells the browser (or client) how many bytes to expect. 

    You're missing the file name in the response.

    Response.AddHeader("Content-Disposition", "attachment;filename=""FileName.pdf""")

    Finally you want to Flush then End not Close.

    Response.Flush()
    Response.End()

    A quick Google search will yield tons of examples.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 4, 2019 10:46 PM

All replies

  • User475983607 posted

    I'm surprised the code works given the bugs.

    I assume this line...

    Response.AddHeader("Accept-Header", ms.Length.ToString)

    should be

    Response.AddHeader("content-length", ms.Length.ToString)

    The content-length tells the browser (or client) how many bytes to expect. 

    You're missing the file name in the response.

    Response.AddHeader("Content-Disposition", "attachment;filename=""FileName.pdf""")

    Finally you want to Flush then End not Close.

    Response.Flush()
    Response.End()

    A quick Google search will yield tons of examples.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Monday, November 4, 2019 10:46 PM
  • User-2081167373 posted

    It has worked perfectly fine for almost a decade, until recently.  This is just what is in the .aspx.vb file, so perhaps the items listed in the actual .aspx or the other file that loads this one.

    Everything was coded really weirdly as it takes two pages to output the report.  The one that passes the first part contains this:

    Partial Class RegulatoryReport_SetParms
        Inherits System.Web.UI.Page
        Dim blayer As New BAL
    
    #Region " btnView_Click "
    
        Protected Sub btnView_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnView.Click
            Try
                Dim report As String = Request.QueryString("Report")
                Dim rptFormat As String = Me.rblRptFormat.SelectedValue
    
                Response.Redirect("GetParms.aspx?Report=" & report & "&Format=" & rptFormat & "")
            Catch ex As Exception
                Throw ex
            End Try
        End Sub
    
    #End Region

    It seems the response.redirect could be where the issue is, since it tries to send the file as the .aspx instead of pdf.

    Monday, November 4, 2019 10:54 PM
  • User475983607 posted

    veekay

    It has worked perfectly fine for almost a decade, until recently.  This is just what is in the .aspx.vb file, so perhaps the items listed in the actual .aspx or the other file that loads this one.

    I assume the newer browser are less forgiving when the HTTP response does not follow standards.  Did you at least try to fix the highlighted bugs and test? 

    veekay

    Everything was coded really weirdly as it takes two pages to output the report.  The one that passes the first part contains this:

    Partial Class RegulatoryReport_SetParms
        Inherits System.Web.UI.Page
        Dim blayer As New BAL
    
    #Region " btnView_Click "
    
        Protected Sub btnView_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnView.Click
            Try
                Dim report As String = Request.QueryString("Report")
                Dim rptFormat As String = Me.rblRptFormat.SelectedValue
    
                Response.Redirect("GetParms.aspx?Report=" & report & "&Format=" & rptFormat & "")
            Catch ex As Exception
                Throw ex
            End Try
        End Sub
    
    #End Region

    It seems the response.redirect could be where the issue is, since it tries to send the file as the .aspx instead of pdf.

    It looks to me like GetParams.aspx acts like a report service.  You pass two parameters to the page, the report name and report format, and the aspx page returns a file to the client. That seems reasonable.

    Why do you think the redirect is the problem?  The initial code does not provide a file name in the response.  The browser will assume the current requested resource is the file name.  That resource happens to be GetParams.aspx.  What else does the browser have to determine the actual file name?

    Monday, November 4, 2019 11:11 PM
  • User-2081167373 posted

    The second code that I posted is actually for the first page, which is the Setparms - that in turns loads the Getparms page.  I figured the issue had to do with the second page, which is why I only posted that. 

    The file loads in the browser, it doesn't actually download anything - which is why it is odd how in the newer browsers it now tries to download the .aspx file. 

    With those code changes, I get an error when adding the -

    Response.AddHeader("Content-Disposition", "attachment;filename=""FileName.pdf""");

    BC30037: Character is not valid.

    and

    Response.AddHeader("content-length", ms.Length.ToString)

    BC30198: ')' expected.

    Monday, November 4, 2019 11:29 PM
  • User475983607 posted

    Sorry, just remove the ";" from the end of...

    Response.AddHeader("Content-Disposition", "attachment;filename=""FileName.pdf""")

    Semicolon is used in C# to designate the end of the line.

    Monday, November 4, 2019 11:33 PM
  • User-2081167373 posted

    Well hot damn, it creates the file and downloads it, which is a great start.

    The big question, is there a way to have the file load into the browser without having to be downloaded? 

    Monday, November 4, 2019 11:39 PM
  • User475983607 posted

    veekay

    Well hot damn, it creates the file and downloads it, which is a great start.

    The big question, is there a way to have the file load into the browser without having to be downloaded? 

    Change this..

    Response.AddHeader("Content-Disposition", "attachment;filename=""FileName.pdf""")

    to 

    Response.AddHeader("Content-Disposition", "inline;filename=""FileName.pdf""")

    Keep in mind, the browser must have a plugin installed or support the file type.  Usually, downloading is safer.

    Monday, November 4, 2019 11:45 PM
  • User-2081167373 posted

    You are amazing - so far it works perfectly - even the excel works using the same changes. 

    Now for modifying a couple hundred files.

    Monday, November 4, 2019 11:47 PM