Open/Download PDF in Vue from Rails API
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