5

So, here I've got a locally stored file named "its_me.pdf" in the assets folder.

I'm trying to reference a download to the PDF using an HTML tag

<a href="../assets/its_me.pdf" download>PDF</a>

It is a real PDF file, if I go double click on the file manually I can see it display and it's real. However, when I go to my application on: http://localhost:4200/its_me (name of route in which it lives), and click on the link, I get a "Failed - No File" error.

Error

ssc-hrep3
  • 15,024
  • 7
  • 48
  • 87
  • Possible duplicate of [How to reference static assets within vue javascript](https://stackoverflow.com/questions/47313165/how-to-reference-static-assets-within-vue-javascript) – Phil Apr 04 '19 at 00:12
  • Also see https://github.com/vuejs/vue-cli/issues/1298#issuecomment-389113736 – Phil Apr 04 '19 at 00:13

2 Answers2

8

Based on @AkashBhave answer I was able to get to work this way. In my script tag:

data () {
  return {
  publicPath: process.env.BASE_URL
  }
}

then in my template.

  <a:href="`${publicPath}whatever.pdf`" download="download">PDF</a>

Alternatively with webpack, in your vue.config.js you add this;

chainWebpack: config => {
config.module
  .rule("pdf")
  .test(/\.pdf$/)
  .use("file-loader")
  .loader("file-loader");
}

then in the script tag;

data () {
  return {
   pdfLink: require("@/assets/whatever.pdf"),
  }
}

Finally, in the template;

   <a :href="pdfLink" download="download">PDF</a>
Jonathan Akwetey Okine
  • 1,295
  • 2
  • 15
  • 18
4

Relative imports should work by default with Vue. Try putting your PDF file into the /public folder of your application. You can then reference the file using string interpolation, like so:

<link rel="icon" href="<%= BASE_URL %>its_me.pdf">

More information is available at https://cli.vuejs.org/guide/html-and-static-assets.html#interpolation

If that doesn't work, something might be wrong with your Webpack or build configuration.

AkashBhave
  • 749
  • 4
  • 12