Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

UsmanBasharmal's avatar

Opening a PDF file in another window with VueJS laravel using href

I want to create href link whenever a user click it it should show the pdf file in separate window. I have tried the below code but id did not work.

getfile(file) {  return "uploads/" + file; }
<tr v-for="DocumentType in DocumentTypes.data"v-bind:key="DocumentType.id">
 <td><a :href="getfile(DocumentType.file)">{{DocumentType.file}}</a> </td></tr>

if more details are required I will provide

there is no error in console.

any kind of help will be highly appreciated

0 likes
11 replies
Amaury's avatar

To open a link in a new tab:

<a href="#" @click.prevent="getfile(DocumentType.file)">{{ DocumentType.file }}</a>
getFile(file) {
    window.open("uploads/" + file, "_blank");
}
UsmanBasharmal's avatar

there is no controller

and the href and function you advised, when I click on it it does not show any url

UsmanBasharmal's avatar

What code do you want? I have provided enough for this function

Amaury's avatar

I think you should have a fully qualified url in the window.open(), something like:

 window.open("https://yourdomain.com/uploads/" + file], "_blank");
Amaury's avatar

You should use the Storage facade to store the file:


// ...
$upload_path = public_path('uploads');
$file = $request->file('file'); // name of the input file in the form
$filename = time() . '.' . $file->getClientOriginalExtension();
$path = 'uploads'
Storage::put(
    public_path('uploads') .'/'.$filename,
    file_get_contents($file->getRealPath())
);

// ...

And to retrieve it:

public function show(Request $request)
{
    // check . . .
    return response()->file(storage_path('uploads/yourFile' ));
}
UsmanBasharmal's avatar

I found the solution using my provided code but if you click the URL which is consist of an image it opens it in another page but if it is pdf file it does not open it it just download it but I want to view the pdf file too

Amaury's avatar

Use the provided Laravel methods:


return response()->file("path/to/file.pdf"); // to preview the PDF

return response()->download"path/to/file.pdf"); // to download the PDF
1 like

Please or to participate in this conversation.