@pdfme/generator 1.0.0-beta.2

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/.eslintrc.js ADDED
@@ -0,0 +1,31 @@
1
+ module.exports = {
2
+ root: true,
3
+ env: {
4
+ es6: true,
5
+ node: true,
6
+ },
7
+ overrides: [
8
+ {
9
+ files: ['*.ts'], // Your TypeScript files extension
10
+ parserOptions: {
11
+ project: ['./tsconfig.json'], // Specify it only for TypeScript files
12
+ },
13
+ },
14
+ ],
15
+ parser: '@typescript-eslint/parser',
16
+ plugins: ['prettier', '@typescript-eslint'],
17
+ rules: {
18
+ 'no-use-before-define': 'off',
19
+ 'dot-notation': 'warn',
20
+ '@typescript-eslint/no-use-before-define': ['error'],
21
+ '@typescript-eslint/explicit-function-return-type': 'off',
22
+ '@typescript-eslint/explicit-module-boundary-types': 'off',
23
+ },
24
+ extends: ['plugin:@typescript-eslint/recommended', 'prettier', 'plugin:prettier/recommended'],
25
+ parserOptions: {
26
+ parser: 'typescript-eslint-parser',
27
+ ecmaVersion: 2018,
28
+ sourceType: 'module',
29
+ project: 'tsconfig.json',
30
+ },
31
+ };
package/README.md ADDED
@@ -0,0 +1,281 @@
1
+ # PDFME
2
+
3
+ > pdfme is now in beta version. Please report any issues or suggestions from [Help page](https://pdfme.com/help) or [edit this page.](https://github.com/pdfme/pdfme/tree/main/website/docs/getting-started.md)
4
+
5
+ TypeScript base PDF generator and React base UI.
6
+ Open source, developed by the community, and completely free to use under the MIT license!
7
+
8
+ <p align="center">
9
+ <img src="https://raw.githubusercontent.com/pdfme/pdfme/main/website/static/img/logo.svg" width="300"/>
10
+ </p>
11
+
12
+ ## Features
13
+
14
+ | Fast PDF Generator | Easy PDF template design | Simple JSON template |
15
+ | -------------------------------------------------------------------------------------------- | ----------------------------------------------------- | -------------------------------------------------------------- |
16
+ | Works on node and browser. Use templates to generate PDF, Complex operations are not needed. | Anyone can easily create templates with the designer. | Templates are JSON data that is easy to understand and handle. |
17
+
18
+ ---
19
+
20
+ <!--
21
+ This is a copy of website/docs/getting-started.md from below.
22
+ - Replace the image path: /img/image.png -> /website/static/img/image.png
23
+ - Replace the link: / -> https://pdfme.com/
24
+ -->
25
+
26
+ ## Introduction
27
+
28
+ pdfme was created to simplify the design and generation process of a PDF. It is especially useful for the following use cases:
29
+
30
+ - Need to create a designed PDF with short code.
31
+ - Need to integrate PDF editor features into an application.
32
+ - Need to create a large number of PDFs without compromising performance
33
+
34
+ As an example, the author's service [https://labelmake.jp/](https://labelmake.jp/) can create more than 100 varieties of PDFs and generates more than 100,000 PDF files per month.
35
+
36
+ ## Installation
37
+
38
+ The operating requirements should be the node environment `>=14`.
39
+ There are two packages in pdfme, generator and UI.
40
+
41
+ The package for generating PDF can be installed with the following command.
42
+
43
+ ```
44
+ npm i @pdfme/generator
45
+ ```
46
+
47
+ The packages for using PDF designer, forms and viewers can be installed with the following commands.
48
+
49
+ ```
50
+ npm i @pdfme/ui
51
+ ```
52
+
53
+ The following type, function and classes are available in pdfme.
54
+
55
+ `@pdfme/generator`
56
+
57
+ - [generate](https://pdfme.com/docs/getting-started#generator)
58
+ - [Template](https://pdfme.com/docs/getting-started#template)
59
+
60
+ `@pdfme/ui`
61
+
62
+ - [Designer](https://pdfme.com/docs/getting-started#designer)
63
+ - [Form](https://pdfme.com/docs/getting-started#form)
64
+ - [Viewer](https://pdfme.com/docs/getting-started#viewer)
65
+ - [Template](https://pdfme.com/docs/getting-started#template)
66
+
67
+ If your environment uses webpack, import the necessary items as shown below.
68
+
69
+ ```ts
70
+ import { Template, generate } from '@pdfme/generator';
71
+ ```
72
+
73
+ ```ts
74
+ import { Template, Designer, Form, Viewer } from '@pdfme/ui';
75
+ ```
76
+
77
+ **All objects use `Template`, which will be briefly explained in the next section.
78
+ **
79
+
80
+ ## Template
81
+
82
+ The core of pdfme library are Templates.
83
+ Template Type can be imported by both `@pdfme/generator` or `@pdfme/ui`. Templates are used everywhere.
84
+
85
+ A template can be divided into two parts: a fixed part and a variable part.
86
+ We call them basePdf and schema.
87
+ The following image is a good illustration of a template.
88
+
89
+ ![](https://raw.githubusercontent.com/pdfme/pdfme/main/website/static/img/template.png)
90
+
91
+ - **basePdf**: PDF data for the fixed part of the PDF to be generated.
92
+ - **schemas**: Definition data for the variable part of the PDF to be generated.
93
+
94
+ **basePdf** can be given a `string`(base64), `ArrayBuffer`, or `Uint8Array`.
95
+ A blank A4 PDF can be imported with `BLANK_PDF`. You can use it to check how it works.
96
+
97
+ **schemas** currently has the following types of data available
98
+
99
+ - text
100
+ - image
101
+ - Various types of barcodes
102
+
103
+ Let's take a look at some specific data.
104
+ (If you are using TypeScript, you can import the Template type.)
105
+
106
+ ### Sample Template
107
+
108
+ ```ts
109
+ import { Template, BLANK_PDF } from '@pdfme/generator';
110
+ // import { Template, BLANK_PDF } from '@pdfme/ui'; <- Template types and BLANK_PDF can also be imported from @pdfme/ui.
111
+
112
+ const template: Template = {
113
+ basePdf: BLANK_PDF,
114
+ schemas: [
115
+ {
116
+ a: {
117
+ type: 'text',
118
+ position: { x: 0, y: 0 },
119
+ width: 10,
120
+ height: 10,
121
+ },
122
+ b: {
123
+ type: 'text',
124
+ position: { x: 10, y: 10 },
125
+ width: 10,
126
+ height: 10,
127
+ },
128
+ c: {
129
+ type: 'text',
130
+ position: { x: 20, y: 20 },
131
+ width: 10,
132
+ height: 10,
133
+ },
134
+ },
135
+ ],
136
+ };
137
+ ```
138
+
139
+ [For more information, please refer to the API documentation of the Template type here](https://pdfme.com/docs/api/common/#template).
140
+
141
+ You can create a template from [Template Design page](https://pdfme.com/template-design). Or, if you want to integrate the template creation feature into your application, check out the [Designer section](https://pdfme.com/docs/getting-started#designer).
142
+
143
+ ## Generator
144
+
145
+ The PDF generator function, `generate`, takes 2 arguments of `template` and `inputs` for generate a PDF. It works both in Node.js and in the browser.
146
+
147
+ The code to generate a PDF file using the [template created above](https://pdfme.com/docs/getting-started#sample-template) is shown below.
148
+
149
+ ```ts
150
+ import { Template, generate } from '@pdfme/generator';
151
+
152
+ const template: Template = {
153
+ // skip... Check the Template section.
154
+ };
155
+ const inputs = [{ a: 'a1', b: 'b1', c: 'c1' }];
156
+
157
+ generate({ template, inputs }).then((pdf) => {
158
+ console.log(pdf);
159
+
160
+ // Browser
161
+ // const blob = new Blob([pdf.buffer], { type: 'application/pdf' });
162
+ // window.open(URL.createObjectURL(blob));
163
+
164
+ // Node.js
165
+ // fs.writeFileSync(path.join(__dirname, `test.pdf`), pdf);
166
+ });
167
+ ```
168
+
169
+ You can create a PDF file like the below.
170
+
171
+ ![](https://raw.githubusercontent.com/pdfme/pdfme/main/website/static/img/simplePdf.png)
172
+
173
+ Also, each element in the inputs array corresponds to a page in the PDF, you can create a multi-page PDF file by providing multiple elements of inputs.
174
+
175
+ [For more information, please refer to the API documentation of the generate function here](https://pdfme.com/docs/api/generator/#generate).
176
+
177
+ ## UI
178
+
179
+ ### Designer
180
+
181
+ The Designer allows you to edit the Template schemas, making it easy for anyone to create Template json objects.
182
+
183
+ You can design your own template from [Template Design page](https://pdfme.com/template-design), or you can integrate the designer into your application.
184
+
185
+ Let's integrate the designer using the template created above as the default template.
186
+
187
+ ```ts
188
+ import { Template, Designer } from '@pdfme/ui';
189
+
190
+ const domContainer = document.getElementById('container');
191
+ const template: Template = {
192
+ // skip... Check the Template section.
193
+ };
194
+
195
+ const designer = new Designer({ domContainer, template });
196
+ ```
197
+
198
+ The Designer class is instantiated as shown above, and the template designer is displayed in the `domContainer`.
199
+ You can edit the template as shown below. The operation is like Google Slides, etc., so you can use common keyboard shortcuts.
200
+
201
+ ![](https://raw.githubusercontent.com/pdfme/pdfme/main/website/static/img/designer.gif)
202
+
203
+ The designer instance can be manipulated with the following methods.
204
+
205
+ - `saveTemplate`
206
+ - `updateTemplate`
207
+ - `getTemplate`
208
+ - `onChangeTemplate`
209
+ - `onSaveTemplate`
210
+ - `destroy`
211
+
212
+ [For more information, please refer to the API documentation of the Designer class here](https://pdfme.com/docs/api/ui/classes/Designer).
213
+
214
+ ### Form
215
+
216
+ You can use templates to create forms and PDF viewers.
217
+
218
+ The Form creates a UI for the user to enter schemas based on the template.
219
+
220
+ ```ts
221
+ import { Template, Form } from '@pdfme/ui';
222
+
223
+ const domContainer = document.getElementById('container');
224
+ const template: Template = {
225
+ // skip...
226
+ };
227
+ // This is initial data.
228
+ const inputs = [{ a: 'a1', b: 'b1', c: 'c1' }];
229
+
230
+ const form = new Form({ domContainer, template, inputs });
231
+ ```
232
+
233
+ ![](https://raw.githubusercontent.com/pdfme/pdfme/main/website/static/img/form.gif)
234
+
235
+ The form instance has a method `getInputs` to get the user's input.
236
+
237
+ You can generate a PDF file based on the user's input by passing the data you get from `getInputs` as inputs to generate, as shown in the code below.
238
+
239
+ ```ts
240
+ generate({ template, inputs: form.getInputs() }).then((pdf) => {
241
+ const blob = new Blob([pdf.buffer], { type: 'application/pdf' });
242
+ window.open(URL.createObjectURL(blob));
243
+ });
244
+ ```
245
+
246
+ [For more information, please refer to the API documentation of the Form class here](https://pdfme.com/docs/api/ui/classes/Form).
247
+
248
+ ### Viewer
249
+
250
+ Viewing a PDF file in a mobile browser is a pain, because it doesn't display well in an iframe.
251
+
252
+ The Viewer is a byproduct of the Form development process, but it allows you to show your users a preview of the PDF file you will create.
253
+
254
+ Using the Viewer is basically the same as using the Form, except that user cannot edit it.
255
+
256
+ ```ts
257
+ import { Template, Viewer } from '@pdfme/ui';
258
+
259
+ const domContainer = document.getElementById('container');
260
+ const template: Template = {
261
+ // skip...
262
+ };
263
+ const inputs = [{ a: 'a1', b: 'b1', c: 'c1' }];
264
+
265
+ const viewer = new Viewer({ domContainer, template, inputs });
266
+ ```
267
+
268
+ ![](https://raw.githubusercontent.com/pdfme/pdfme/main/website/static/img/viewer.png)
269
+
270
+ [For more information, please refer to the API documentation of the Viewer class here](https://pdfme.com/docs/api/ui/classes/Viewer).
271
+
272
+ ## Special Thanks
273
+
274
+ - [pdf-lib](https://pdf-lib.js.org/): Used in PDF generation.
275
+ - [PDF.js](https://mozilla.github.io/pdf.js/): Used in PDF viewing.
276
+ - [React](https://reactjs.org/): Used in building the UI.
277
+ - [react-moveable](https://daybrush.com/moveable/), [react-selecto](https://github.com/daybrush/selecto), [@scena/react-guides](https://daybrush.com/guides/): Used in Designer UI.
278
+ - [bwip-js](https://github.com/metafloor/bwip-js): Used in barcode generation.
279
+ - [zod](https://github.com/colinhacks/zod): Used in Validation.
280
+
281
+ I definitely could not have created pdfme without these libraries. I am grateful to the developers of these libraries.
@@ -0,0 +1 @@
1
+ declare module '*.ttf';