0

Controller:

    <HttpGet>
Function downloadpdf() As FileContentResult

    Dim data As MyData = Me.Getdata()
    Dim sb As StringBuilder = New StringBuilder()

    If data IsNot Nothing Then
        If data.HasItems Then
            sb.AppendLine()
            sb.AppendLine("ID,Description)
            For Each item In data.Items
                sb.AppendLine(String.Format("{0},{1}", item.Id, item.Description))
            Next
        End If
    End If
    Dim bytes as Byte() = New UTF8Encoding().GetBytes(sb.ToString())
    Response.Clear()
    Response.AddHeader("Content-Disposition", "inline; filename=mypdf.pdf")
    Response.AddHeader("Content-Type", "application/pdf")
    Response.ClearHeaders()
    Response.AddHeader("Content-Length", bytes.Length.ToString())
    Return File(bytes, "application/pdf", "mypdf.pdf")

End Function

View:

 <a href="@Url.Action("downloadpdf", "home")">PDF</a>

When clicked the link, it prompt to download the file, then it display this:

Adobe Acrobat Reader could not open 'mypdf.pdf' because it is either not a supported file type or because the file has been damaged (for example, it was sent as an email attachment and wasn't correctly decoded).

And the pdf doesn't open. Any suggestion or any other way to do this?

Cheers

1 Answer 1

2

This is not the way you create a PDF. You are just returning a text file with a PDF extension.

You will need a third party tool to create a PDF. I use ABCPdf (Paid, I'm not affiliated to them), but there are several open source tools to choose from.

Related: .NET server based PDF generation

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.