vg-print-vue2 1.0.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) 2021 gmcSimple
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.en.md ADDED
@@ -0,0 +1,500 @@
1
+
2
+ An out-of-the-box print designer component library (Vue 2). Currently focused on the single component `FullDesigner`, while exposing underlying `hiprint` capabilities and client direct connection (`hiwebSocket`) configuration, facilitating rapid integration of template design, preview, browser printing, PDF/image export, and direct printing functions.
3
+
4
+ You can install and experience the features directly. Features are constantly being improved, please update in time. Feedback and suggestions are welcome.
5
+ No domain restrictions, available offline. Provides one-on-one technical support.
6
+ Search `vg-print-vue2` on npm.
7
+ Contact me: QQ 984239686 or use the WeChat group on the homepage to solve problems.
8
+
9
+ ## Silent Printing Client Tool Installer
10
+
11
+ ![Silent Printing Client Tool](https://gitee.com/gao111/electron-vg-print/raw/master/electron-hiprint.png)
12
+
13
+ More info and download:
14
+ ```text
15
+ https://gitee.com/gao111/electron-vg-print
16
+ ```
17
+ Client tool download:
18
+ ```text
19
+ https://download.csdn.net/download/github_38400706/92538739
20
+ ```
21
+
22
+ ## Features
23
+
24
+ - Full-featured page component `FullDesigner`, ready to use, no need to assemble sub-components
25
+ - Supports preview, browser printing, PDF/image export, direct printing (client required)
26
+ - Configure `hiwebSocket` `host`, `token` and auto-connection via props
27
+ - Exposes instance methods: preview, print, export, connection control, etc., for external calls
28
+ - Integrates and exports `hiprint` for advanced customization at the template object level
29
+
30
+ ## Installation
31
+
32
+ ```bash
33
+ npm i vg-print-vue2
34
+ ```
35
+
36
+ ## Header Component
37
+
38
+ - Used for the top area of the page, with built-in common operation buttons and printer settings; supports customization via `props` and `slots`.
39
+ - Embedded in `FullDesigner` by default; can also be used separately.
40
+
41
+ ### Props
42
+
43
+ - `logoHtml: string` Custom HTML fragment for the left Logo area
44
+ - `title: string` Left title text
45
+ - `printCount: number` Number of copies, default `1` (supports `v-model:printCount`)
46
+ - `printerName: string` Currently selected printer name (supports `v-model:printerName`)
47
+ - `printerList: Array<{ name: string; label: string }>` Printer list
48
+ - `waitShowPrinter: boolean` Show loading state for "Browser Print" button
49
+ - `languages: Array<{ label: string; value: string }>` Language switch dropdown options
50
+
51
+ ### Events (Emits)
52
+
53
+ - `preview` Open preview
54
+ - `print-view` Browser print
55
+ - `to-pdf` Export PDF
56
+ - `to-image` Export Image
57
+ - `print` Direct print (client required)
58
+ - `clear-paper` Clear (with secondary confirmation)
59
+ - `save` Save (primary key of split button)
60
+ - `edit-template` Open "Edit Template Data" dialog
61
+ - `edit-data` Open "Edit Print Data" dialog
62
+ - `lang-change` Language switch (value is language code: `cn`, `en`, `de`, `es`, `fr`, `it`, `ja`, `ru`, `cn_tw`)
63
+ - `feedback` Feedback/Help
64
+
65
+ ### Slots
66
+
67
+ - `headerLeft` Left area (default: logo + title)
68
+ - `headerCenter` Middle custom area (default empty)
69
+ - `headerRight` Right operation area (default: printer and a group of buttons)
70
+
71
+ ### Important Note on Styles
72
+
73
+ If you find page styles missing during external calls, please import the print style.
74
+ Copy `node_modules/@vg-print/hiprint/dist/css/print-lock.css` to your development resource directory (e.g., `public` directory in Vue).
75
+ If your site is `https://www.abcd.com/index.html`, ensure `https://www.abcd.com/print-lock.css` is accessible.
76
+ Add `print-lock.css` to your project's `index.html` with `media="print"`.
77
+
78
+ ```html
79
+ <link rel="stylesheet" type="text/css" media="print" href="/print-lock.css" />
80
+ ```
81
+
82
+ ### Usage Example: With FullDesigner
83
+
84
+ ```vue
85
+ <template>
86
+ <FullDesigner
87
+ ref="designer"
88
+ :initial-template="tpl"
89
+ :initial-print-data="rows"
90
+ default-lang="cn"
91
+ :hi-host="host"
92
+ :hi-token="token"
93
+ :hi-auto-connect="true"
94
+ @save="onSave"
95
+ >
96
+ <!-- Top area (Header built-in; can be customized via slot) -->
97
+ <template #headerLeft>
98
+ <div style="display:flex;align-items:center;color:#fff;">
99
+ <img src="/logo.svg" style="height:24px;margin-right:8px;" />
100
+ <span>Custom Title</span>
101
+ </div>
102
+ </template>
103
+ <template #headerCenter>
104
+ <el-tag type="success">Middle Custom Area</el-tag>
105
+ </template>
106
+ <!-- No need to provide headerRight if keeping default buttons; provide if replacing completely -->
107
+ <template #headerRight>
108
+ <el-button size="small" @click="designer.value.preView()">Preview</el-button>
109
+ <el-button size="small" @click="designer.value.printView()">Browser Print</el-button>
110
+ <el-button type="primary" size="small" @click="designer.value.toPdf()">Export PDF</el-button>
111
+ <el-button size="small" @click="designer.value.toImage()">Export Image</el-button>
112
+ <el-button size="small" @click="designer.value.print()">Direct Print</el-button>
113
+ <el-button type="warning" size="small" @click="onClear()">Clear</el-button>
114
+ <el-button type="success" size="small" @click="onSaveDirect()">Save</el-button>
115
+ </template>
116
+ </FullDesigner>
117
+ </template>
118
+
119
+ <script setup>
120
+ import { ref } from 'vue'
121
+ const designer = ref(null)
122
+ const host = ref('http://127.0.0.1:17521')
123
+ const token = ref('')
124
+ const tpl = { panels: [ /* ... */ ] }
125
+ const rows = [{ title: 'Pathology Report', name: 'DongDong' }]
126
+ const onSave = ({ template, data }) => { /* Save to backend */ }
127
+ const onClear = () => { /* Clear data/elements */ }
128
+ const onSaveDirect = () => designer.value?.save()
129
+ </script>
130
+ ```
131
+
132
+ ### Usage Example: Header Alone
133
+
134
+ ```vue
135
+ <template>
136
+ <Header
137
+ v-model:printCount="count"
138
+ v-model:printerName="printer"
139
+ :printerList="printers"
140
+ :waitShowPrinter="loading"
141
+ :languages="langs"
142
+ title="Print Designer"
143
+ @preview="onPreview"
144
+ @print-view="onPrintView"
145
+ @to-pdf="onToPdf"
146
+ @to-image="onToImage"
147
+ @print="onPrint"
148
+ @clear-paper="onClear"
149
+ @save="onSave"
150
+ @lang-change="onLangChange"
151
+ @feedback="onFeedback"
152
+ />
153
+ </template>
154
+ ```
155
+
156
+ ### Responsive Strategy (Built-in)
157
+
158
+ - Automatically hides the middle area and secondary buttons when the window shrinks, keeping key operations like "Save" and "Feedback"; left logo/title are unaffected.
159
+ - Breakpoints:
160
+ - `≤1600px` Hide middle area
161
+ - `≤1400px` Hide copies, printer selection, preview, browser print, export PDF, export image
162
+ - `≤1200px` Hide direct print, language switch
163
+ - `≤992px` Hide clear
164
+ - `≤768px` Hide title text (keep Logo)
165
+
166
+ > Tip: If you need fully custom buttons, provide the `headerRight` slot.
167
+
168
+ ---
169
+
170
+ ## Preview Component
171
+
172
+ - Independent preview modal component, supports rendering template HTML, selecting printer, exporting PDF/image, direct printing, etc.
173
+ - Can be used separately or with `FullDesigner`.
174
+
175
+ ### Props
176
+
177
+ - `printTemplate: object` Template object (`hiprint.PrintTemplate` or return value of `createTemplate`)
178
+ - `printData: object | array` Print data
179
+ - `printerList: Array<{ name: string; label: string }>` Printer list
180
+ - `selectedPrinter: string` Selected printer name
181
+ - `showPdf: boolean` Show "Export PDF" button, default `true`
182
+ - `showImg: boolean` Show "Export Image" button, default `true`
183
+ - `showPrint2: boolean` Show "Direct Print" button, default `true`
184
+ - `modalShow: boolean` Modal visibility (supports `v-model:modalShow`)
185
+ - `width: string | number` Modal width (default `'80%'`)
186
+ - `defaultLang: string` Component i18n language (used when Standalone); supports `cn`, `en`, `de`, `es`, `fr`, `it`, `ja`, `ru`, `cn_tw`; default `cn`
187
+
188
+ ### Events (Emits)
189
+
190
+ - `update:modalShow` Two-way binding for modal visibility
191
+ - `update:selectedPrinter` Selected printer change
192
+ - `close` Close modal
193
+
194
+ ### Exposed Methods (defineExpose)
195
+
196
+ - `show(template, data, width?)` Show and render preview content; can pass template/data and width
197
+ - `hideModal()` Close modal
198
+ - `init()` Initialize (internal use)
199
+ - `getTarget()` Get preview container DOM
200
+
201
+ ### Usage Example (Standalone)
202
+
203
+ ```vue
204
+ <template>
205
+ <el-button @click="openPreview">Show Preview</el-button>
206
+ <Preview
207
+ ref="previewRef"
208
+ v-model:modalShow="visible"
209
+ default-lang="cn"
210
+ :printerList="printers"
211
+ :selectedPrinter="printer"
212
+ @update:selectedPrinter="printer = $event"
213
+ />
214
+ </template>
215
+
216
+ <script setup>
217
+ import { ref } from 'vue'
218
+ import { Preview, createTemplate } from 'vg-print-vue2'
219
+ import 'vg-print-vue2/style.css'
220
+
221
+ const previewRef = ref(null)
222
+ const visible = ref(false)
223
+ const printers = ref([{ name: 'HP_001', label: 'HP Printer' }])
224
+ const printer = ref('')
225
+
226
+ const tmplJson = { panels: [ /* ... */ ] }
227
+ const data = { title: 'Report', name: 'DongDong' }
228
+
229
+ const openPreview = () => {
230
+ const tpl = createTemplate(tmplJson) // Or pass hiprint.PrintTemplate
231
+ previewRef.value.show(tpl, data, '80%')
232
+ }
233
+ </script>
234
+ ```
235
+
236
+ ### Usage Example (With FullDesigner)
237
+
238
+ ```vue
239
+ <template>
240
+ <FullDesigner ref="designer" default-lang="cn" />
241
+ <Preview ref="previewRef" v-model:modalShow="visible" />
242
+ <el-button @click="designer.value.preView()">Use FullDesigner's Preview</el-button>
243
+ <el-button @click="showCustomPreview">Use Preview Component</el-button>
244
+ </template>
245
+
246
+ <script setup>
247
+ import { ref } from 'vue'
248
+ const designer = ref(null)
249
+ const previewRef = ref(null)
250
+ const visible = ref(false)
251
+
252
+ const showCustomPreview = () => {
253
+ const tpl = designer.value.getTemplate() // Assuming method exposed
254
+ const data = designer.value.getPrintData() // Assuming method exposed
255
+ previewRef.value.show(tpl, data, '80%')
256
+ }
257
+ </script>
258
+ ```
259
+
260
+ ### Note
261
+
262
+ - Please import `vg-print-vue2/style.css` when using the component library.
263
+ - `Preview` automatically renders content based on `printTemplate` and `printData`.
264
+ - If using `Preview` separately without plugin installation, set `default-lang`.
265
+
266
+ Style Import:
267
+
268
+ ```javascript
269
+ // Global style
270
+ import 'vg-print-vue2/style.css'
271
+
272
+ // Print lock style (Recommended)
273
+ // In `index.html`:
274
+ // <link rel="stylesheet" type="text/css" media="print" href="/css/print-lock.css" />
275
+ ```
276
+
277
+ Environment: Vue 2.7, Node >= 16.14.
278
+
279
+ ## Quick Start
280
+
281
+ ```javascript
282
+ // main.ts
283
+ import Vue from 'vue'
284
+ import App from './App.vue'
285
+ import vgPrint from 'vg-print-vue2'
286
+ import 'vg-print-vue2/style.css'
287
+
288
+ Vue.use(vgPrint)
289
+ new Vue({
290
+ render: h => h(App)
291
+ }).$mount('#app')
292
+ ```
293
+
294
+ ```vue
295
+ <template>
296
+ <FullDesigner
297
+ ref="designer"
298
+ :hi-host="'http://127.0.0.1:17521'"
299
+ :hi-token="'your-token'"
300
+ :hi-auto-connect="true"
301
+ />
302
+ <!-- Buttons... -->
303
+ </template>
304
+ <script setup>
305
+ // ... Same as above
306
+ </script>
307
+ ```
308
+
309
+ ## API (FullDesigner)
310
+
311
+ ### Props
312
+
313
+ - `hi-host: string` Client service address
314
+ - `hi-token: string` Client auth token
315
+ - `hi-auto-connect: boolean` Auto connect to client
316
+ - `initial-template: object` Initial template JSON
317
+ - `initial-print-data: object | array` Initial print data
318
+ - `default-lang: string` Default language
319
+
320
+ ### Methods
321
+
322
+ - `preView()` Open preview
323
+ - `printView()` Browser print
324
+ - `print()` Direct print
325
+ - `toPdf()` Export PDF
326
+ - `toPdf2()` Export PDF with paper layout
327
+ - `toImage()` Export Image
328
+ - `toImage2()` Export Image with paper layout
329
+ - `toImage()` Export Image
330
+ - `setHiwebSocket(host, token, cb)` Set client address/token (Respects autoConnect state)
331
+ - `setHost(host, token, cb)` Set client address/token and connect immediately (Standard API)
332
+ - `connect(cb)` Connect to client
333
+ - `disconnect()` Disconnect
334
+ - `disAutoConnect()` Disable auto-connect
335
+
336
+ ### Events
337
+
338
+ - `save` Save template and data
339
+
340
+ ## Advanced Usage (hiprint)
341
+
342
+ ```javascript
343
+ import { hiprint, defaultElementTypeProvider } from 'vg-print-vue2'
344
+ // Global Config
345
+ hiprint.setConfig({ showAdsorbLine: true, showPosition: true, showSizeBox: true })
346
+ // Register Plugins
347
+ hiprint.register({ authKey: '', plugins: [] })
348
+ // Init
349
+ hiprint.init({
350
+ host: 'http://localhost:17521',
351
+ token: '',
352
+ lang: 'en'
353
+ })
354
+ // Create Template
355
+ const tpl = new hiprint.PrintTemplate({ template: {} })
356
+ // Design
357
+ tpl.design('#hiprint-printTemplate', { grid: true })
358
+ // Print
359
+ tpl.print({ name: 'Example' })
360
+ ```
361
+
362
+ ### Auth Key and Default Watermark
363
+
364
+ - **Design State**: No default watermark.
365
+ - **Runtime State**:
366
+ - No Auth: Default watermark or "Trial Version" suffix.
367
+ - With Auth: No default watermark.
368
+
369
+ Register:
370
+ ```javascript
371
+ hiprint.register({ authKey: 'YOUR_KEY' });
372
+ ```
373
+
374
+ ## Lightweight Runtime API
375
+
376
+ Use when you only need "Template + Data" capabilities without the designer UI.
377
+
378
+ ```javascript
379
+ import { createTemplate, printBrowser, exportPdf, exportImage } from 'vg-print-vue2'
380
+
381
+ const tpl = createTemplate(tmplJson)
382
+ printBrowser(tpl, printData)
383
+ exportPdf(tpl, printData, 'filename')
384
+ ```
385
+
386
+ ### Export PDF / Image
387
+
388
+ `exportPdf` / `exportImage` are lightweight wrappers around the template instance methods:
389
+
390
+ - `exportPdf(tpl, data, filename, options)` → `tpl.toPdf(data, filename, options)`
391
+ - `exportPdf2(tpl, data, filename, options)` → `tpl.toPdf2(data, filename, options)`
392
+
393
+ - `exportImage(tpl, data, options)` → `tpl.toImage(data, options)`
394
+ - `exportImage2(tpl, data, options)` → `tpl.toImage2(data, options)`
395
+
396
+ #### Export PDF
397
+
398
+ **Mode A: Normal export (follow template paging)**
399
+
400
+ ```js
401
+ // No paperWidth/paperHeight: export pages exactly like preview pagination
402
+ exportPdf(tpl, printData, 'report', {
403
+ onProgress: (cur, total) => console.log('toPdf', Math.floor((cur / total) * 100))
404
+ })
405
+ ```
406
+
407
+ **Mode B: Layout export (tile small labels into a big paper, e.g. A4)**
408
+
409
+ ```js
410
+ exportPdf(tpl, printData, 'labels', {
411
+ paperWidth: 210, // mm
412
+ paperHeight: 297, // mm
413
+ perPage: 6, // put 6 labels per sheet (rest goes to next sheet)
414
+ leftOffset: -1, // -1: center; >=0: left margin (mm)
415
+ topOffset: -1, // -1: center; >=0: top margin (mm)
416
+ scale: 2, // clarity vs performance (pixelRatio also supported)
417
+ onProgress: (cur, total) => console.log('toPdf', Math.floor((cur / total) * 100))
418
+ })
419
+ ```
420
+
421
+ **Layout rules**
422
+
423
+ - With `paperWidth/paperHeight` (or `usePaperType: true`):
424
+ - `perPage > 0`: fixed “N per sheet”
425
+ - `perPage = 0` or omitted: auto-fill “as many as can fit per sheet”
426
+ - Without paper size:
427
+ - Export follows template pagination; `perPage` does not apply in this mode.
428
+
429
+ #### Export Image
430
+
431
+ **Mode A: Normal export (follow template paging)**
432
+
433
+ ```js
434
+ // Recommended: one image per page (avoid very long images)
435
+ await exportImage(tpl, printData, {
436
+ isDownload: true,
437
+ name: 'page',
438
+ splitPages: true,
439
+ type: 'image/png', // QR/barcode recommended
440
+ pixelRatio: 2,
441
+ toType: 'blob'
442
+ })
443
+ ```
444
+
445
+ **Optional: merge into long images**
446
+
447
+ ```js
448
+ // limit: N pages per output image; limit<=0 merges all pages into one (can be huge)
449
+ await exportImage(tpl, printData, {
450
+ isDownload: true,
451
+ name: 'merged',
452
+ limit: 6,
453
+ type: 'image/jpeg',
454
+ pixelRatio: 2,
455
+ quality: 0.85
456
+ })
457
+ ```
458
+
459
+ **Mode B: Layout export (tile small labels into a big paper, e.g. A4)**
460
+
461
+ ```js
462
+ // In layout mode, `limit` works like “labels per sheet”
463
+ await exportImage(tpl, printData, {
464
+ paperWidth: 210,
465
+ paperHeight: 297,
466
+ limit: 6,
467
+ isDownload: true,
468
+ name: 'A4-layout',
469
+ splitPages: true,
470
+ type: 'image/png',
471
+ pixelRatio: 2
472
+ })
473
+ ```
474
+
475
+ **Layout rules**
476
+
477
+ - With `paperWidth/paperHeight`:
478
+ - `limit > 0`: fixed “N per sheet”
479
+ - `limit = 0` or omitted: auto-fill “as many as can fit per sheet”
480
+ - Without paper size:
481
+ - `splitPages: true`: one image per page (recommended)
482
+ - `splitPages: false` + `limit > 0`: merge N pages per long image
483
+ - `splitPages: false` + `limit <= 0`: merge all pages into one long image (not recommended)
484
+
485
+ #### Common options
486
+
487
+ - `pixelRatio` / `scale`: render scale (higher = clearer but slower)
488
+ - `forcePng` (PDF): force PNG for QR/barcode
489
+ - `maxCanvasPixels`: cap per-page canvas pixels to reduce freezes
490
+ - `debugPerf`: log per-page render time to console
491
+
492
+ ### Notes
493
+
494
+ - Inject `print-lock.css` in `index.html`.
495
+ - Direct printing needs a client.
496
+ - SSR compatible.
497
+
498
+ ## Full Example
499
+
500
+ (See the code block in the Chinese section or above)