Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Opening a PDF file in another window with VueJS

Tags:

pdf

vue.js

vuejs2

In my previous Angular app I was able to open my resume in another window like such:

<a class="link popup" href="javascript:void(0);" onclick="javascipt:window.open('./../../assets/Resume_Christopher_Kade.pdf');">Resume</a>

While rewriting my website with Vue, I noted that it did not work as intended, after changing it to:

<a class="link popup" href="javascript:void(0);" v-on:click="openPdf()">Resume</a>

With openPdf() in my component's methods:

openPdf () {
    javascipt:window.open('./../assets/Resume_Christopher_Kade.pdf');
}

When clicking the link, a new page opens to the following URL:

http://localhost:8080/assets/Resume_Christopher_Kade.pdf

while showing an empty route on my screen instead of displaying the pdf in the browser's pdf viewer.

This issue might be related to the way I handle routes in Vue:

export default new Router({
  mode: 'history',
  routes: [    
    {
      path: '/',
      name: 'Home',
      component: Home
    },
    {
      path: '/work',
      name: 'Work',
      component: Work
    },
    {
      path: '/posts',
      name: 'Posts',
      component: Posts
    },
    { path: '*', component: Home }
  ]
})

Why isn't it as straightforward as in Angular? Am I missing something?

like image 713
Christopher Avatar asked Jan 30 '18 09:01

Christopher


1 Answers

Did you figure it out?

My solution is for projects created with Vue CLI 3 running locally (I haven't built my project yet).

My issue was similar to yours. I just wanted <a> link that opened up my pdf file on a new tab but my url concatenated a single hash to my path and redirected me to my home page. And it was a surprisingly easy fix:

<a href="./resume.pdf" target="_blank">resume</a>

Just a dot, forward slash, and my file name. And my file is located under root > public folder.

Relative Path Imports

When you reference a static asset using relative path (must start with .) inside JavaScript, CSS or *.vue files, the asset will be included into webpack's dependency graph...

https://cli.vuejs.org/guide/html-and-static-assets.html#relative-path-imports

like image 151
Leesa Avatar answered Nov 04 '22 02:11

Leesa