@component-hook/pdf-canvas
@component-hook/pdf-canvas
Rendering PDF documents onto a canvas with vue3 and react component.
Implementation depository: PDF-signature
Features
- Rendering PDF documents onto a canvas
- Open encrypted PDF files
- Drag and drop the images and text onto the canvas
- Manually add the images and text onto the canvas
- Full
TypeScriptsupport
Documentation
For detailed documentation and usage examples, please visit: Official Docs.
Installation
# Using npm
$ npm install @component-hook/pdf-canvas
# Using yarn
$ yarn add @component-hook/pdf-canvas
# Using pnpm
$ pnpm install @component-hook/pdf-canvas
Base Usage
Vue3
<script setup lang="ts">
import PdfCanvas, { loadFile, type PDF } from '@component-hook/pdf-canvas/vue';
import { ref } from 'vue';
const currentPdf = ref<PDF>();
async function uploadFile(event: Event) {
const target = event.target as HTMLInputElement;
const { files } = target;
if (!files) return;
const file = files[0];
const content = await loadFile(file);
currentPdf.value = content;
target.value = '';
}
</script>
<template>
<div>
<pdf-canvas
v-if="currentPdf"
:file="currentPdf"
/>
<p v-else>Please select a PDF file or image.</p>
<button class="relative">
<input
type="file"
accept="application/pdf, .jpg, .png"
class="absolute top-0 left-0 h-[36px] w-[94px] cursor-pointer opacity-0"
@change="uploadFile"
/>
select file
</button>
</div>
</template>
React
import PdfCanvas, { loadFile, type PDF } from '@component-hook/pdf-canvas/react';
import { useState, type ChangeEvent } from 'react';
export function DrawPdf() {
const [currentPdf, setCurrentPdf] = useState<PDF>();
const uploadFile = async (event: ChangeEvent<HTMLInputElement>) => {
const target = event.target;
const { files } = target;
if (!files || files.length === 0) return;
const file = files[0];
const content = await loadFile(file);
setCurrentPdf(content);
target.value = '';
};
return (
<div>
{currentPdf ? <PdfCanvas file={currentPdf} /> : <p>Please select a PDF file or image.</p>}
<button>
<input
type="file"
accept="application/pdf, .jpg, .png"
onChange={uploadFile}
/>
select file
</button>
</div>
);
}
⚠️ Required Vite Configuration
If you're using Vite in development mode, you must exclude this package from Vite's dependency optimization, because it internally uses a Web Worker which doesn't work well when Vite pre-bundles dependencies.
// vite.config.js or vite.config.ts
import { defineConfig } from 'vite';
export default defineConfig({
// …other Vite config
optimizeDeps: {
exclude: ['@component-hook/pdf-canvas'],
},
});
Why Is This Necessary?
- Vite's dev server does pre-bundling of dependencies (using esbuild) to improve performance.
- However, modules using Web Workers (like this component) may break or fail to initialize correctly during this pre-bundling stage.
Web Worker & Vite Known Issue
- This is a known issue: the Vite team is aware of difficulties when optimizing dependencies that use Web Workers.
- For more background, see this GitHub issue: Vite/Rolldown-Vite #362
- Once Vite improves its Web Worker optimization, it may become safe to remove this exclusion — but for now, it's required to ensure correct behavior in dev mode.
License
This project is licensed under the MIT License.
Related Packages
Free PDF SDK: an embeddable, white-label PDF viewer, annotator and editor for th...
Open-source collaborative browser editors for documents, Markdown, spreadsheets,...
Version History
| Version | Released | Vue | License |
|---|---|---|---|
| 1.7.0 | ^3.5.44 | MIT | |
| 1.6.0 | ^3.5.43 | MIT | |
| 1.5.1 | ^3.5.41 | MIT | |
| 1.5.0 | ^3.5.39 | MIT | |
| 1.4.4 | ^3.5.28 | MIT | |
| 1.4.3 | ^3.5.28 | MIT | |
| 1.4.2 | ^3.5.26 | MIT | |
| 1.4.1 | ^3.5.26 | MIT | |
| 1.4.0 | ^3.5.26 | MIT | |
| 1.3.2 | ^3.5.23 | MIT | |
| 1.3.1 | ^3.5.23 | MIT | |
| 1.3.0 | ^3.4.29 | MIT | |
| 1.2.2 | ^3.4.29 | MIT | |
| 1.2.1 | ^3.4.29 | MIT | |
| 1.2.0 | ^3.4.29 | MIT | |
| 1.1.5 | ^3.4.29 | MIT | |
| 1.1.4 | ^3.4.29 | MIT | |
| 1.1.3 | ^3.4.29 | MIT | |
| 1.1.2 | ^3.4.29 | MIT | |
| 1.1.1 | ^3.4.29 | MIT | |
| 1.1.0 | ^3.4.29 | MIT | |
| 1.0.4 | ^3.4.29 | MIT | |
| 1.0.3 | ^3.4.29 | MIT | |
| 1.0.2 | ^3.4.29 | MIT | |
| 1.0.1 | ^3.4.29 | MIT | |
| 1.0.0 | ^3.4.29 | MIT | |
| 0.2.5 | ^3.4.29 | MIT | |
| 0.2.4 | ^3.4.29 | MIT | |
| 0.2.3 | ^3.4.29 | MIT | |
| 0.2.2 | ^3.4.29 | MIT |
Showing the latest 30 of 38. See every release on npm
Pre-releases (50)
| Version | Released | Vue | License |
|---|---|---|---|
| 1.4.4-alpha.1 | ^3.5.28 | MIT | |
| 1.4.4-alpha.0 | ^3.5.28 | MIT | |
| 1.3.2-alpha.0 | ^3.5.23 | MIT | |
| 1.3.1-alpha.5 | ^3.5.23 | MIT | |
| 1.3.1-alpha.4 | ^3.4.29 | MIT | |
| 1.3.1-alpha.3 | ^3.4.29 | MIT | |
| 1.3.1-alpha.2 | ^3.4.29 | MIT | |
| 1.3.1-alpha.1 | ^3.4.29 | MIT | |
| 1.3.1-alpha.0 | ^3.4.29 | MIT | |
| 1.3.0-alpha.5 | ^3.4.29 | MIT | |
| 1.3.0-alpha.4 | ^3.4.29 | MIT | |
| 1.3.0-alpha.3 | ^3.4.29 | MIT | |
| 1.3.0-alpha.2 | ^3.4.29 | MIT | |
| 1.3.0-alpha.1 | ^3.4.29 | MIT | |
| 1.3.0-alpha.0 | ^3.4.29 | MIT | |
| 1.2.2-alpha.4 | ^3.4.29 | MIT | |
| 1.2.2-alpha.3 | ^3.4.29 | MIT | |
| 1.2.2-alpha.2 | ^3.4.29 | MIT | |
| 1.2.2-alpha.1 | ^3.4.29 | MIT | |
| 1.2.2-alpha.0 | ^3.4.29 | MIT | |
| 1.1.0-alpha.12 | ^3.4.29 | MIT | |
| 1.1.0-alpha.11 | ^3.4.29 | MIT | |
| 1.1.0-alpha.10 | ^3.4.29 | MIT | |
| 1.1.0-alpha.9 | ^3.4.29 | MIT | |
| 1.1.0-alpha.8 | ^3.4.29 | MIT | |
| 1.1.0-alpha.7 | ^3.4.29 | MIT | |
| 1.1.0-alpha.6 | ^3.4.29 | MIT | |
| 1.1.0-alpha.5 | ^3.4.29 | MIT | |
| 1.1.0-alpha.4 | ^3.4.29 | MIT | |
| 1.1.0-alpha.3 | ^3.4.29 | MIT |
Showing the latest 30 of 50. See every release on npm