Open/Download PDF in Vue from Rails API

Kenneth Teh
1 min readJul 6, 2020

--

I’m surprised this isn’t a more common topic… or maybe I did some very poor Google searches! I was trying to use my Rails backend to generate a PDF and render it in a new tab from my Vue app(without downloading).

In my Rails API controller:

I was using PrawnPDF to create a rendered PDF string, but you can get the same result with something like WickedPDF too.

In VueJS (or any other front-end JS with axios):

(if you want to open the file in a new tab)

If you want to download the file instead, replace link.target = '_blank' with link.download = ‘file.pdf' .

References

https://gist.github.com/javilobo8/097c30a233786be52070986d8cdb1743

--

--

Kenneth Teh
Kenneth Teh

Written by Kenneth Teh

Software Engineer primarily working with Rails and Vue.JS... sometimes DevOps and shell stuff too

Responses (1)