PDF Documents from out the Application

Is there a way in elixir were Pdf documents which are outside the Elixir App Directory to render to frontend ??

If you are using Phoenix, you can define a public static path to the location of the PDF documents as explained on the following thread.

Edit:
But I just noticed you are the one that started that thread. :sweat_smile:

2 Likes

There is a way to do it… but You need a controller.

In this controller, You can use send_file or send_download and use the pdf path, which could be outside of Phoenix.

You can use this route path as the pdf source.

1 Like

I have tried using send_file or send_download method but the file is atomically downloading

def view_pdf(conn, %{"id" => id}) do

        conn =  export(conn, id) 

        page = %{first: "my", last: "pdf"}
        render(conn, "view_pdf.html", page: page,  )

  end

  def export(conn, _params) do

    filename = "D:/coi.pdf"

    # Send file

    conn

    |> put_resp_header("content-disposition", ~s(attachment; filename="#{filename}"))

    |> send_file(200, filename)

  end

my template where i want my pdf to appear

                <div class="form-group">
                    <div id="slider">
                        <div id="box">
                        <embed src="<%=  @conn %>"/>
                        </div>

                   </div>

You need to use the route to export as the src of embed.

Something like

<embed src="<%= Routes.to_your_path(@conn, ...) %>" />
1 Like

to be exact the issue is that the file is being downloaded in the browser when i route to the view_pdf function

You need to have and additional route to get the downloadable pdf… and use this route in the view_pdf template.

That means two routes, but your code is trying to mix both.

Am kind of confused about what you really mean asking if you can illustrate exactly

Add a route get_pdf, with the send_file, or send_download.

Use this route in the view_pdf template, as src of the embed.

1 Like

If I understand correctly, you need two routes.

  • One to download the PDF file Routes.my_path(conn, :get_pdf)
  • Another to view the PDF in a template Routes.my_path(conn, :view_pdf)

The thing here is that the :get_pdf action just send the file and you don’t have to add a render/3 in its response pipeline.

In the other hand the :view_pdf action will render your HTML template in which you are embeding the PDF document.

<embed src="<%= Routes.my_path(@conn, :get_pdf) %>" />

You can ever add a download link if you want:

<%= link "Download me", to: Routes.my_path(@conn, :get_pdf) %>

I would like to add also that it is possible also to add a static path that point to a location outside your Phoenix project, if of course you know it at compile time.

I hope this is helpful.

3 Likes

i have done that but the file is being downloaded not rendered

def get_pdf(conn, _params) do

    filename = "D:/coi.pdf"
  
    # Send file

    conn

    |> put_resp_header("content-disposition", ~s(attachment; filename="#{filename}"))

    |> send_file(200, filename)

  end

and my template

<embed src="<%= Routes.user_path(@conn, :get_pdf) %>" />

i want the pdf docs to be outside of the application directory

its has worked …i was missing this |

put_resp_header("content-type", "application/pdf")

thanks for the help

2 Likes