Display PDF file

I am trying to display a PDF file that I have on disk using this:

  <h4>PDF File:</h4>
   <%= @data %>

However, the page looks like gibberish. Same if I use <%= Phoenix.HTML.raw @data %>

in your controller set the appropriate content type and then stream the payload into the conn.

1 Like


I just added the put_resp_content_type line. Now I don’t see gibberish,
but rather PDF viewer. However, there is an error message:“This PDF document may not display properly”. I do know that the PDF file is OK. Do I need to do something else to stream?

  {:ok, body} -> conn
               |> put_resp_content_type("application/pdf")
               |> render("pdf.html", data: body)

No, more like this:

{:ok, body} -> Enum.into(body, put_resp_content_type(conn, "application/pdf"))

But thats in theory… I never actually used the fact that Plug.Conn is a Collectable.

Thanks, I’ll look into that. I think more arguments are required by Enum.into( )

@jxxcarlson are you trying to send them a pdf file? or render it inline with html? If you’re just sending them a file then use https://hexdocs.pm/plug/Plug.Conn.html#send_file/5

1 Like

I’m trying to display the pdf file in a web page. But sending the file directly could work also.

To render it in the middle of the page you’ll need to leverage some javascript https://stackoverflow.com/questions/291813/recommended-way-to-embed-pdf-in-html


Your send file solution works great! Thanks!!