Top Related Projects
Quick Overview
vue-pdf is a Vue.js component for rendering PDF files in web applications. It provides a simple way to display PDF documents within Vue projects, offering various customization options and features for interacting with PDF content.
Pros
- Easy integration with Vue.js projects
- Supports both single-page and multi-page PDF rendering
- Offers customizable controls for zooming, page navigation, and rotation
- Provides events for tracking user interactions and document loading progress
Cons
- Requires additional setup for server-side rendering (SSR) environments
- Limited built-in annotation support
- May have performance issues with very large PDF files
- Depends on external PDF.js library, which can increase bundle size
Code Examples
- Basic usage of vue-pdf component:
<template>
<pdf src="path/to/document.pdf"></pdf>
</template>
<script>
import pdf from 'vue-pdf'
export default {
components: {
pdf
}
}
</script>
- Rendering a specific page and setting scale:
<template>
<pdf
:src="pdfUrl"
:page="1"
:scale="1.5"
></pdf>
</template>
<script>
import pdf from 'vue-pdf'
export default {
components: { pdf },
data() {
return {
pdfUrl: 'https://example.com/document.pdf'
}
}
}
</script>
- Using events to track loading progress:
<template>
<pdf
:src="pdfUrl"
@loading="loadingHandler"
@progress="progressHandler"
></pdf>
</template>
<script>
import pdf from 'vue-pdf'
export default {
components: { pdf },
methods: {
loadingHandler(loading) {
console.log(loading ? 'Loading started' : 'Loading finished')
},
progressHandler(progress) {
console.log(`Loading progress: ${progress * 100}%`)
}
}
}
</script>
Getting Started
-
Install the package:
npm install vue-pdf
-
Import and use the component in your Vue file:
<template> <pdf src="path/to/document.pdf"></pdf> </template> <script> import pdf from 'vue-pdf' export default { components: { pdf } } </script>
-
If using with Nuxt.js, create a plugin file (e.g.,
plugins/vue-pdf.js
):import Vue from 'vue' import pdf from 'vue-pdf' Vue.component('pdf', pdf)
Then add the plugin to your
nuxt.config.js
:plugins: [ { src: '~/plugins/vue-pdf', mode: 'client' } ]
Competitor Comparisons
PDF Reader in JavaScript
Pros of pdf.js
- More comprehensive PDF rendering and manipulation capabilities
- Wider browser compatibility and standalone usage
- Larger community and more frequent updates
Cons of pdf.js
- Steeper learning curve and more complex implementation
- Larger file size and potentially higher resource usage
- Not specifically designed for Vue.js integration
Code Comparison
pdf.js:
pdfjsLib.getDocument('path/to/document.pdf').promise.then(function(pdf) {
pdf.getPage(1).then(function(page) {
var scale = 1.5;
var viewport = page.getViewport({ scale: scale });
// Render page
});
});
vue-pdf:
<template>
<pdf src="path/to/document.pdf" :page="1"></pdf>
</template>
<script>
import pdf from 'vue-pdf'
export default {
components: { pdf }
}
</script>
Summary
pdf.js offers a more robust and versatile solution for PDF rendering and manipulation, with broader browser support and a larger community. However, it comes with a steeper learning curve and higher complexity. vue-pdf, on the other hand, provides a simpler, Vue-specific implementation that's easier to integrate into Vue.js projects but may have more limited functionality compared to pdf.js.
Convert PDF to markdown quickly with high accuracy
Pros of marker
- Focuses on PDF annotation and highlighting, providing more specialized functionality
- Offers AI-powered text extraction and analysis features
- Supports multiple file formats beyond just PDFs
Cons of marker
- Less integration-focused compared to vue-pdf's Vue.js compatibility
- May have a steeper learning curve due to its more advanced features
- Potentially heavier resource usage due to AI components
Code Comparison
marker:
from marker import Document
doc = Document("example.pdf")
highlights = doc.highlight_text("important concept")
vue-pdf:
<template>
<pdf src="example.pdf"></pdf>
</template>
<script>
import pdf from 'vue-pdf'
export default { components: { pdf } }
</script>
Summary
marker is a more specialized tool for PDF annotation and analysis, offering AI-powered features and support for multiple file formats. It may be more suitable for complex document processing tasks. vue-pdf, on the other hand, is designed specifically for integrating PDF viewing capabilities into Vue.js applications, making it a simpler choice for basic PDF rendering in web projects.
Client/server side PDF printing in pure JavaScript
Pros of pdfmake
- Client-side PDF generation, allowing for dynamic content creation without server-side processing
- Extensive documentation and a wide range of features for complex PDF layouts
- Supports both browser and Node.js environments
Cons of pdfmake
- Steeper learning curve due to its own markup language for defining PDF structure
- Larger file size and potentially slower performance for simple PDF viewing tasks
- May require more setup and configuration for basic use cases
Code Comparison
vue-pdf (for viewing a PDF):
<template>
<pdf src="path/to/document.pdf"></pdf>
</template>
<script>
import pdf from 'vue-pdf'
</script>
pdfmake (for generating a PDF):
var docDefinition = {
content: [
'Hello world',
{ text: 'Styled text', bold: true, fontSize: 20 }
]
};
pdfMake.createPdf(docDefinition).download();
vue-pdf is focused on rendering existing PDFs within Vue applications, while pdfmake is designed for creating new PDFs from scratch with a high degree of customization. The choice between them depends on whether you need to view or generate PDFs in your project.
Client-side JavaScript PDF generation for everyone.
Pros of jsPDF
- More versatile: Can create PDFs from scratch, not just render existing ones
- Broader functionality: Supports text, images, shapes, and more
- No framework dependency: Can be used with any JavaScript project
Cons of jsPDF
- Steeper learning curve: Requires more code to achieve basic PDF rendering
- Less optimized for Vue.js: Doesn't provide Vue-specific components or integrations
- Larger file size: More features mean a heavier library
Code Comparison
vue-pdf:
<template>
<pdf src="path/to/document.pdf"></pdf>
</template>
<script>
import pdf from 'vue-pdf'
</script>
jsPDF:
import { jsPDF } from "jspdf";
const doc = new jsPDF();
doc.text("Hello world!", 10, 10);
doc.save("a4.pdf");
Summary
vue-pdf is tailored for Vue.js applications and focuses on rendering existing PDFs, making it simpler for basic PDF display tasks. jsPDF, on the other hand, offers more comprehensive PDF creation capabilities but requires more setup and coding. Choose vue-pdf for quick PDF rendering in Vue apps, and jsPDF for creating custom PDFs from scratch in any JavaScript environment.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
vue-pdf
vue.js pdf viewer is a package for Vue that enables you to display and view PDF's easily via vue components.
Install via NPM/Yarn
npm install vue-pdf
yarn add vue-pdf
Example - basic
<template>
<pdf src="./path/to/static/relativity.pdf"></pdf>
</template>
<script>
import pdf from 'vue-pdf'
export default {
components: {
pdf
}
}
Demo
TBD: fix the demo
Browser support
Same browser support as Vue.js 2
Note
since v2.x, the script is exported as esm.
API
Props
:src String / Object - default: ''
The url of the given pdf. src
may also be a string|TypedArray|DocumentInitParameters|PDFDataRangeTransport
for more details, see PDFJS.getDocument()
.
:page Number - default: 1
The page number to display.
:rotate Number - default: 0
The page rotation in degrees, only multiple of 90 are valid. EG: 90, 180, 270, 360, ...
Events
@password (updatePassword, reason)
updatePassword
: The function to call with the pdf password.reason
: the reason why this function is called'NEED_PASSWORD'
or'INCORRECT_PASSWORD'
@progress Number
Document loading progress. Range [0, 1].
@loaded
Triggers when the document is loaded.
@page-loaded Number
Triggers when a page is loaded.
@num-pages Number
The sum of all pages from the given pdf.
@error Object
Triggers when an error occurs.
@link-clicked Number
Triggers when an internal link is clicked
Public methods
print(dpi, pageList) * experimental *
dpi
: the print resolution of the document (try 100).pageList
: the list (array) of pages to print.
Public static methods
createLoadingTask(src[, options])
src
: see:src
propoptions
: an object of options. This function creates a PDFJS loading task that can be used and reused as:src
property.
The loading task is a promise that resolves with the PDFJS pdf document that exposes thenumPages
property (see example below).
beware: when the component is destroyed, the object returned by createLoadingTask()
become invalid.
Supported options:
- onPassword: Callback that's called when a password protected PDF is being opened.
- onProgress: Callback return loading progress.
- withCredentials: Wheter or not to send cookies in the fetch request.
Examples
Example - current page / page count
<template>
<div>
{{currentPage}} / {{pageCount}}
<pdf
src="https://cdn.mozilla.net/pdfjs/tracemonkey.pdf"
@num-pages="pageCount = $event"
@page-loaded="currentPage = $event"
></pdf>
</div>
</template>
<script>
import pdf from 'vue-pdf'
export default {
components: {
pdf
},
data() {
return {
currentPage: 0,
pageCount: 0,
}
}
}
</script>
Example - display multiple pages of the same pdf document
<template>
<div>
<pdf
v-for="i in numPages"
:key="i"
:src="src"
:page="i"
style="display: inline-block; width: 25%"
></pdf>
</div>
</template>
<script>
import pdf from 'vue-pdf'
var loadingTask = pdf.createLoadingTask('https://cdn.mozilla.net/pdfjs/tracemonkey.pdf');
export default {
components: {
pdf
},
data() {
return {
src: loadingTask,
numPages: undefined,
}
},
mounted() {
this.src.promise.then(pdf => {
this.numPages = pdf.numPages;
});
}
}
</script>
Example - print all pages
<template>
<button @click="$refs.myPdfComponent.print()">print</button>
<pdf ref="myPdfComponent" src="https://cdn.mozilla.net/pdfjs/tracemonkey.pdf"></pdf>
</template>
Example - print multiple pages
<template>
<button @click="$refs.myPdfComponent.print(100, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])">print</button>
<pdf ref="myPdfComponent" src="https://cdn.mozilla.net/pdfjs/tracemonkey.pdf"></pdf>
</template>
Example - get text content
<template>
<div>
<button
@click="logContent"
>
log content
</button>
<pdf
ref="myPdfComponent"
src="https://cdn.mozilla.net/pdfjs/tracemonkey.pdf"
></pdf>
</div>
</template>
<script>
import pdf from 'vue-pdf'
export default {
components: {
pdf
},
methods: {
logContent() {
this.$refs.myPdfComponent.pdf.forEachPage(function(page) {
return page.getTextContent()
.then(function(content) {
var text = content.items.map(item => item.str);
console.log(text);
})
});
}
}
}
</script>
Example - complete
<template>
<div>
<input type="checkbox" v-model="show">
<select v-model="src" style="width: 30em">
<option v-for="item in pdfList" :value="item" v-text="item"></option>
</select>
<input v-model.number="page" type="number" style="width: 5em"> /{{numPages}}
<button @click="rotate += 90">⟳</button>
<button @click="rotate -= 90">⟲</button>
<button @click="$refs.pdf.print()">print</button>
<div style="width: 50%">
<div v-if="loadedRatio > 0 && loadedRatio < 1" style="background-color: green; color: white; text-align: center" :style="{ width: loadedRatio * 100 + '%' }">{{ Math.floor(loadedRatio * 100) }}%</div>
<pdf v-if="show" ref="pdf" style="border: 1px solid red" :src="src" :page="page" :rotate="rotate" @password="password" @progress="loadedRatio = $event" @error="error" @num-pages="numPages = $event" @link-clicked="page = $event"></pdf>
</div>
</div>
</template>
<script>
import pdf from 'vue-pdf'
export default {
components: {
pdf: pdf
},
data () {
return {
show: true,
pdfList: [
'',
'https://cdn.mozilla.net/pdfjs/tracemonkey.pdf',
'https://cdn.rawgit.com/mozilla/pdf.js/c6e8ca86/test/pdfs/freeculture.pdf',
'https://cdn.rawgit.com/mozilla/pdf.js/c6e8ca86/test/pdfs/annotation-link-text-popup.pdf',
'https://cdn.rawgit.com/mozilla/pdf.js/c6e8ca86/test/pdfs/calrgb.pdf',
'https://cdn.rawgit.com/sayanee/angularjs-pdf/68066e85/example/pdf/relativity.protected.pdf',
'data:application/pdf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gL1BhZ2VzIDEgMCBSCj4+CmVuZG9iagp4cmVmCjAgOAowMDAwMDAwMDAwIDY1NTM1IGYgCjAwMDAwMDA1ODAgMDAwMDAgbiAKMDAwMDAwMDI1MiAwMDAwMCBuIAowMDAwMDAwMDE1IDAwMDAwIG4gCjAwMDAwMDAyMzAgMDAwMDAgbiAKMDAwMDAwMDM2NiAwMDAwMCBuIAowMDAwMDAwNjQ1IDAwMDAwIG4gCjAwMDAwMDA3NzIgMDAwMDAgbiAKdHJhaWxlcgo8PCAvU2l6ZSA4CiAgIC9Sb290IDcgMCBSCiAgIC9JbmZvIDYgMCBSCj4+CnN0YXJ0eHJlZgo4MjQKJSVFT0YK',
],
src:'',
loadedRatio: 0,
page: 1,
numPages: 0,
rotate: 0,
}
},
methods: {
password: function(updatePassword, reason) {
updatePassword(prompt('password is "test"'));
},
error: function(err) {
console.log(err);
}
}
}
</script>
Credits
Top Related Projects
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot