@component-hook/pdf-canvas 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 Tzuyi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md ADDED
@@ -0,0 +1,115 @@
1
+ # @component-hook/pdf-canvas
2
+
3
+ Rendering PDF documents onto a canvas with vue3 component ([DEMO](https://tzuyi0817.github.io/component-hook/#/pdf-canvas))
4
+ Implementation depository: [PDF-signature](https://github.com/tzuyi0817/PDF-signature)
5
+
6
+ <p>
7
+ <a href="https://npm-stat.com/charts.html?package=@component-hook/pdf-canvas">
8
+ <img src="https://img.shields.io/npm/dm/@component-hook/pdf-canvas.svg" alt="npm"/>
9
+ </a>
10
+ <a href="https://www.npmjs.com/package/@component-hook/pdf-canvas">
11
+ <img src="https://img.shields.io/npm/v/@component-hook/pdf-canvas.svg" alt="npm"/>
12
+ </a>
13
+ </p>
14
+
15
+ ## Features
16
+
17
+ - Rendering PDF documents onto a canvas
18
+ - Open encrypted PDF files
19
+ - Drag and drop the images and text onto the canvas
20
+ - Manually add the images and text onto the canvas
21
+ - Supports typescript
22
+
23
+ ## Installation
24
+
25
+ ```bash
26
+ npm install @component-hook/pdf-canvas
27
+
28
+ # or use yarn
29
+ yarn add @component-hook/pdf-canvas
30
+
31
+ # or use pnpm
32
+ pnpm install @component-hook/pdf-canvas
33
+ ```
34
+
35
+ ## Basic Usage
36
+
37
+ ```vue
38
+ <script setup lang="ts">
39
+ import { ref } from 'vue';
40
+ import PdfCanvas, { useFabric, type PDF } from '@component-hook/pdf-canvas';
41
+
42
+ const { loadFile } = useFabric();
43
+ const currentPdf = ref<PDF>();
44
+
45
+ async function uploadFile(event: Event) {
46
+ const target = event.target as HTMLInputElement;
47
+ const { files } = target;
48
+
49
+ if (!files) return;
50
+ const file = files[0];
51
+ const content = await loadFile(file);
52
+
53
+ currentPdf.value = content;
54
+ target.value = '';
55
+ }
56
+ </script>
57
+
58
+ <template>
59
+ <div class="w-fit flex flex-col items-center gap-3">
60
+ <pdf-canvas
61
+ v-if="currentPdf"
62
+ :file="currentPdf"
63
+ />
64
+
65
+ <p
66
+ v-else
67
+ class="font-mono text-sm"
68
+ >
69
+ Please select a PDF file or image.
70
+ </p>
71
+
72
+ <button class="relative">
73
+ <input
74
+ type="file"
75
+ accept="application/pdf, .jpg, .png"
76
+ class="opacity-0 top-0 left-0 absolute w-[94px] h-[36px] cursor-pointer"
77
+ @change="uploadFile"
78
+ />
79
+ select file
80
+ </button>
81
+ </div>
82
+ </template>
83
+ ```
84
+
85
+ ## Attributes
86
+
87
+ | Name | Required | Type | Description | Default |
88
+ | ---------------- | -------- | --------- | ---------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------- |
89
+ | file | true | `PDF` | The data is generated by loadFile of useFabric hook | — |
90
+ | page | false | `number` | The page number you want to display | 1 |
91
+ | canvasId | false | `string` | Canvas dom prefix id selector - `${canvasId}-${page - 1}` | PDF-canvas |
92
+ | fileZoom | false | `number` | Zoom in and out of the displayed canvas | 1 |
93
+ | fileScale | false | `number` | The actual size of the canvas will affect the display clarity | 1 |
94
+ | canvasClass | false | `string` | Canvas dom class selector | — |
95
+ | isDrop | false | `boolean` | Control whether to drag and drop images and text onto the canvas | false |
96
+ | password | false | `string` | Open password for encrypted PDF files | - |
97
+ | dropTextOptions | false | `object` | [fabric.js FabricText options](https://github.com/fabricjs/fabric.js) | [text options](https://github.com/tzuyi0817/component-hook/blob/master/packages/pdf-canvas/configs/index.ts) |
98
+ | dropImageOptions | false | `object` | [fabric.js FabricImage options](https://github.com/fabricjs/fabric.js) | [image options](https://github.com/tzuyi0817/component-hook/blob/master/packages/pdf-canvas/configs/index.ts) |
99
+ | closeSvgOptions | false | `object` | Setup close svg options | [close options](https://github.com/tzuyi0817/component-hook/blob/master/packages/pdf-canvas/configs/index.ts) |
100
+
101
+ ## Exposes
102
+
103
+ | Event | Description | Type |
104
+ | :----------- | :-------------------------------------- | :--------------------------------------------------------------- |
105
+ | addImage | Manually add the images onto the canvas | `Function (src: string, options?: TOptions<ImageProps>) => void` |
106
+ | addText | Manually add the text onto the canvas | `Function (text: string, options?: TOptions<TextProps>) => void` |
107
+ | clearActive | Manually deactivate status | `Function () => void` |
108
+ | deleteCanvas | Delete the current canvas | `Function () => void` |
109
+ | canvasRef | The current canvas dom ref | `object Ref<HTMLCanvasElement>` |
110
+
111
+ ## UseFabric
112
+
113
+ | Event | Description | Type |
114
+ | :------- | :--------------------- | :------------------------------------------------------- |
115
+ | loadFile | Load PDF file or image | `Function (file: File, password?: string): Promise<PDF>` |
@@ -0,0 +1,4 @@
1
+ const e = {};
2
+ export {
3
+ e as default
4
+ };
@@ -0,0 +1,161 @@
1
+ import { Canvas } from 'fabric';
2
+ import { ComponentOptionsMixin } from 'vue';
3
+ import { DefineComponent } from 'vue';
4
+ import { ExtractPropTypes } from 'vue';
5
+ import { FabricObject } from 'fabric';
6
+ import { FabricObjectProps } from 'fabric';
7
+ import { ImageProps } from 'fabric';
8
+ import { ObjectEvents } from 'fabric';
9
+ import { Point } from 'fabric';
10
+ import { PropType } from 'vue';
11
+ import { PublicProps } from 'vue';
12
+ import { Ref } from 'vue';
13
+ import { SerializedObjectProps } from 'fabric';
14
+ import { TEvent } from 'fabric';
15
+ import { TextProps } from 'fabric';
16
+ import { TOptions } from 'fabric';
17
+
18
+ declare type __VLS_NonUndefinedable<T> = T extends undefined ? never : T;
19
+
20
+ declare type __VLS_Prettify<T> = {
21
+ [K in keyof T]: T[K];
22
+ } & {};
23
+
24
+ declare type __VLS_TypePropsToRuntimeProps<T> = {
25
+ [K in keyof T]-?: {} extends Pick<T, K> ? {
26
+ type: PropType<__VLS_NonUndefinedable<T[K]>>;
27
+ } : {
28
+ type: PropType<T[K]>;
29
+ required: true;
30
+ };
31
+ };
32
+
33
+ declare type __VLS_WithDefaults<P, D> = {
34
+ [K in keyof Pick<P, keyof P>]: K extends keyof D ? __VLS_Prettify<P[K] & {
35
+ default: D[K];
36
+ }> : P[K];
37
+ };
38
+
39
+ declare function addImage(src: string, options?: TOptions<ImageProps>): void;
40
+
41
+ declare function addText(text: string, options?: TOptions<TextProps>): void;
42
+
43
+ export declare interface CloseSvgOptions {
44
+ stroke: string;
45
+ hoverStroke: string;
46
+ src: string;
47
+ }
48
+
49
+ export declare interface CreateCloseFabricArgs {
50
+ canvas: Canvas;
51
+ target: FabricObject;
52
+ stroke?: string;
53
+ uuid?: number;
54
+ }
55
+
56
+ declare const _default: DefineComponent<__VLS_WithDefaults<__VLS_TypePropsToRuntimeProps<Props>, {
57
+ canvasId: string;
58
+ class: string;
59
+ canvasClass: string;
60
+ canvasScale: number;
61
+ page: number;
62
+ fileZoom: number;
63
+ fileScale: number;
64
+ isDrop: boolean;
65
+ }>, {
66
+ addImage: typeof addImage;
67
+ addText: typeof addText;
68
+ clearActive: () => void;
69
+ deleteCanvas: () => void;
70
+ canvasRef: Ref<HTMLCanvasElement | null>;
71
+ }, unknown, {}, {}, ComponentOptionsMixin, ComponentOptionsMixin, {}, string, PublicProps, Readonly<ExtractPropTypes<__VLS_WithDefaults<__VLS_TypePropsToRuntimeProps<Props>, {
72
+ canvasId: string;
73
+ class: string;
74
+ canvasClass: string;
75
+ canvasScale: number;
76
+ page: number;
77
+ fileZoom: number;
78
+ fileScale: number;
79
+ isDrop: boolean;
80
+ }>>>, {
81
+ page: number;
82
+ canvasId: string;
83
+ fileZoom: number;
84
+ fileScale: number;
85
+ canvasScale: number;
86
+ canvasClass: string;
87
+ isDrop: boolean;
88
+ }, {}>;
89
+ export default _default;
90
+
91
+ export declare interface PDF {
92
+ PDFId: string;
93
+ name: string;
94
+ updateDate: number;
95
+ PDFBase64: string;
96
+ pages: number;
97
+ canvas?: string[];
98
+ trashDate?: number;
99
+ isUpdate?: boolean;
100
+ }
101
+
102
+ declare interface Props {
103
+ file: PDF;
104
+ page?: number;
105
+ canvasId?: string;
106
+ fileZoom?: number;
107
+ fileScale?: number;
108
+ canvasScale?: number;
109
+ canvasClass?: string;
110
+ isDrop?: boolean;
111
+ password?: string;
112
+ dropTextOptions?: TOptions<TextProps>;
113
+ dropImageOptions?: TOptions<ImageProps>;
114
+ closeSvgOptions?: CloseSvgOptions;
115
+ }
116
+
117
+ export declare interface RenderImageArgs {
118
+ url: string;
119
+ scale?: number;
120
+ }
121
+
122
+ export declare type SelectedEvent = Partial<TEvent> & {
123
+ target: FabricObject<Partial<FabricObjectProps>, SerializedObjectProps, ObjectEvents>;
124
+ };
125
+
126
+ export declare interface SpecifyPageArgs {
127
+ page: number;
128
+ PDFBase64: string;
129
+ scale: number;
130
+ password?: string;
131
+ }
132
+
133
+ export declare type SupportFileType = 'application/pdf' | 'image/png' | 'image/jpeg';
134
+
135
+ export declare interface TCornerPoint {
136
+ tl: Point;
137
+ tr: Point;
138
+ bl: Point;
139
+ br: Point;
140
+ }
141
+
142
+ export declare function useFabric(id?: string): {
143
+ createCanvas: () => Canvas | undefined;
144
+ loadFile: (file: File, password?: string) => Promise<{
145
+ pages: number;
146
+ PDFId: string;
147
+ name: string;
148
+ updateDate: number;
149
+ PDFBase64: string;
150
+ } | undefined>;
151
+ specifyPage: ({ page, PDFBase64, scale, password }: SpecifyPageArgs) => Promise<void>;
152
+ renderImage: ({ url, scale }: RenderImageArgs) => Promise<void>;
153
+ addFabric: (src: string, options?: TOptions<ImageProps>) => Promise<void>;
154
+ addTextFabric: (text: string, options?: TOptions<TextProps>) => void;
155
+ clearActive: () => void;
156
+ deleteCanvas: () => void;
157
+ scaleCloseFabric: (scale: number, isCreate?: boolean) => void;
158
+ setCloseSvgOptions: (options?: CloseSvgOptions) => void;
159
+ };
160
+
161
+ export { }