Laravel and Vue.js are a powerhouse duo for modern web development. Combining Laravel’s robust backend capabilities with Vue.js’s reactive front end, you can create engaging, dynamic web applications. In this blog post, we’ll walk through creating an application that allows users to load PDFs into a viewable format right in the browser.
Prerequisites
Before diving in, ensure you have the following installed:
- PHP (>=7.3)
- Composer
- Node.js and npm
- Laravel CLI
Step 1: Setting Up Laravel and Vue.js
Install Laravel
First, create a new Laravel project:
bash
composer create-project --prefer-dist laravel/laravel pdf-app
cd pdf-app
Install Vue.js
Next, install Vue.js:
bash
npm install vue
npm install vue-loader vue-template-compiler
Configure Webpack
Add Vue.js support in webpack.mix.js:
js
const mix = require('laravel-mix');
mix.js('resources/js/app.js', 'public/js')
.vue()
.postCss('resources/css/app.css', 'public/css', [
require('postcss-import'),
require('tailwindcss'),
require('autoprefixer'),
]);
Create app.js in resources/js:
js
import { createApp } from 'vue';
import App from './components/App.vue';
createApp(App).mount('#app');
Step 2: Creating the Vue Component for PDF Viewing
Install PDF.js
PDF.js is a powerful library for rendering PDFs in a browser. Install it using npm:
bash
npm install pdfjs-dist
Create the Vue Component
Create App.vue in resources/js/components:
vue
<template>
<div>
<input type="file" @change="loadPdf" />
<canvas ref="pdfCanvas"></canvas>
</div>
</template>
<script>
import pdfjsLib from 'pdfjs-dist';
export default {
methods: {
loadPdf(event) {
const file = event.target.files[0];
if (file.type !== 'application/pdf') return;
const reader = new FileReader();
reader.onload = async (e) => {
const pdfData = new Uint8Array(e.target.result);
const pdf = await pdfjsLib.getDocument({ data: pdfData }).promise;
const page = await pdf.getPage(1);
const viewport = page.getViewport({ scale: 1.5 });
const canvas = this.$refs.pdfCanvas;
const context = canvas.getContext('2d');
canvas.height = viewport.height;
canvas.width = viewport.width;
const renderContext = {
canvasContext: context,
viewport: viewport
};
page.render(renderContext);
};
reader.readAsArrayBuffer(file);
}
}
};
</script>
Step 3: Blade Template and Routes
Update Blade Template
Create welcome.blade.php in resources/views:
html
<!DOCTYPE html>
<html>
<head>
<title>PDF Viewer</title>
<link rel="stylesheet" href="{{ mix('css/app.css') }}">
</head>
<body>
<div id="app"></div>
<script src="{{ mix('js/app.js') }}"></script>
</body>
</html>
Set Up Route
Add the following to routes/web.php:
php
Route::get('/', function () {
return view('welcome');
});
Step 4: Compile Assets and Run the App
Compile Assets
Use the following command to compile the assets:
bash
npm run dev
Run the Laravel Server
Finally, start the Laravel development server:
bash
php artisan serve
Open your browser and navigate to http://127.0.0.1:8000. You should see the file input and PDF viewer which is to use.
Conclusion
With these steps, you have a fully functional Laravel Vue.js application that can load and display PDFs. This is just the beginning—feel free to expand and customize your application further!
Fuel my creative spark with a virtual coffee! Your support keeps the ideas percolating—grab me a cup at Buy Me a Coffee and let’s keep the magic brewing!
Hello to all, the contennts existing at this web page are in fact remarkable for peoplke
knowledge, well, keep uup thhe good work fellows.
My web blog: WPS Office PDF转Word
It’s awesome in upport of me to have a webb site,
which is helpful in favor of my know-how. thanks
admin
my blog post signal官网
Generally I do not learn article on blogs, however
I would like to say that this write-up very compelled
mee tto take a look at and do it! Your writing style hhas been surprised me.
Thanks, quite great post.
Feel free to surf to mmy homepage telegram 中文版 安装
It’s actually a cool and useful piece oof information. I am satisfied that you
simply shared this helpful info wiuth us. Please keep us up to date like
this. Thanks for sharing.
Stop byy my blog; 搜狗输入法个性化皮肤设置
Awesome article.
Feel free to visit my homepage; Julienne
Generally I doo not learn aricle oon blogs, but I would like to
say that this write-up very pressured me to check out and do
it! Yoour writing tasste has been amazed me. Tank you, quite nice
post.
Also visit mmy weeb ssite … LDPlayer低CPU占用优化指南
You could certainly see your skills in the article you write.
The world hopes for more passionate writers such as you
who are not afraid to say how they believe. Always go after your heart.
Also visit my web-site – 雷电模拟器无限多开方法
Hey there would you mind letting me know which hosting company you’re
using? I’ve loaded youhr blo iin 3 compketely different
browsers andd I must say this blog loads a lot faster then most.
Can youu suggest a good hosting provider at a fair price?
Thanks, I appreciate it!
Also visit my homepage :: viber官网
Please check here https://apnahive.com/why-hostinger-is-the-best-web-hosting-service-for-your-needs/ it will help you
Hi, just wanted to mention, Iliked this article. It was funny.
Keep on posting!
Also viskt my blog post – WPS Office macOS版下载