Preview PDF in the browser

Hi all,

I am trying to get a runtime binary string loaded to new tab as pdf. I am able to have it downloaded,but is there a way to have it previewed before downloading?

I have an equivalent in php which does this: \PDF::loadView(template,[data]) returns as object which gives me a pdf stream property.

1 Like

You’ll need to set headers so the browser knows it’s a pdf and to show it inline… eg:

show in browser:

Content-Type: application/pdf
Content-Disposition: inline; filename="filename.pdf"

download:

Content-Type: application/pdf
Content-Disposition: attachment; filename="filename.pdf"

see https://hexdocs.pm/plug/Plug.Conn.html#put_resp_header/3

alternatively for full control, you can use pdf.js - maybe combine/inline with a liveview etc https://mozilla.github.io/pdf.js/examples/index.html#interactive-examples

2 Likes