zmdms-utils 0.0.35 → 0.0.37

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.
@@ -72,6 +72,16 @@ function __generator(thisArg, body) {
72
72
  } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
73
73
  if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
74
74
  }
75
+ }
76
+
77
+ function __spreadArray(to, from, pack) {
78
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
79
+ if (ar || !(i in from)) {
80
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
81
+ ar[i] = from[i];
82
+ }
83
+ }
84
+ return to.concat(ar || Array.prototype.slice.call(from));
75
85
  }
76
86
 
77
- export { __assign, __awaiter, __generator, __rest };
87
+ export { __assign, __awaiter, __generator, __rest, __spreadArray };
@@ -0,0 +1,6 @@
1
+ /**
2
+ * dom 转 canvas
3
+ */
4
+ declare function domToCanvas(dom: HTMLElement): Promise<HTMLCanvasElement>;
5
+
6
+ export { domToCanvas };
@@ -0,0 +1,14 @@
1
+ import html2canvas from 'html2canvas';
2
+
3
+ /**
4
+ * dom 转 canvas
5
+ */
6
+ function domToCanvas(dom) {
7
+ return html2canvas(dom, {
8
+ useCORS: true,
9
+ allowTaint: true,
10
+ scale: 2, // 用于渲染的比例。默认为浏览器设备像素比 如果dom区域较大,下载会出现部分灰色区域,需要加scale解决
11
+ });
12
+ }
13
+
14
+ export { domToCanvas };
@@ -0,0 +1,42 @@
1
+ import { RGBAData } from 'jspdf';
2
+ import { IWaterResult } from './water.js';
3
+
4
+ /**
5
+ * 图片 转 pdf
6
+ */
7
+ declare function imgToPdf(config: IImgToPdfConfig): Promise<unknown>;
8
+ interface IImgToPdfConfig {
9
+ /** 图片 base64字符串 */
10
+ image: string | HTMLImageElement | HTMLCanvasElement | Uint8Array | RGBAData;
11
+ width: number;
12
+ height: number;
13
+ /** 是否直接下载pdf */
14
+ isDownload?: boolean;
15
+ /** 生成的pdf左边距 */
16
+ leftPadding?: number;
17
+ /** 导出pdf单位 */
18
+ unit?: "pt" | "mm";
19
+ /**
20
+ * 横向 还是 纵向
21
+ * l: 横向 p:纵向
22
+ */
23
+ orientation?: "p" | "l";
24
+ /**
25
+ * pdf格式 a4 ..
26
+ */
27
+ format?: string | [width: number, height: number];
28
+ /**
29
+ * 生成的pdf是否压缩
30
+ */
31
+ compress?: boolean;
32
+ /**
33
+ * 生成pdf的名称
34
+ */
35
+ pdfName?: string;
36
+ /**
37
+ * 水印相关信息
38
+ */
39
+ waterConfig?: IWaterResult;
40
+ }
41
+
42
+ export { imgToPdf };
package/dist/es/pdf.js ADDED
@@ -0,0 +1,101 @@
1
+ import { jsPDF } from 'jspdf';
2
+
3
+ /**
4
+ * 图片 转 pdf
5
+ */
6
+ function imgToPdf(config) {
7
+ return new Promise(function (resolve, reject) {
8
+ var _a;
9
+ try {
10
+ var image = config.image, width = config.width, height = config.height, _b = config.leftPadding, leftPadding = _b === void 0 ? 20 : _b, _c = config.compress, compress = _c === void 0 ? true : _c, _d = config.orientation, orientation_1 = _d === void 0 ? "p" : _d, _e = config.format, format = _e === void 0 ? "a4" : _e, pdfName = config.pdfName, waterConfig = config.waterConfig, isDownload = config.isDownload;
11
+ // 单位转换
12
+ var _f = parsePdfFormat(format), pdfWidth = _f[0], pdfHeight = _f[1];
13
+ // 如果是横向的话,需要转换下宽高的位置
14
+ if (orientation_1 === "l") {
15
+ _a = [pdfHeight, pdfWidth], pdfWidth = _a[0], pdfHeight = _a[1];
16
+ }
17
+ var unit = config.unit || "pt";
18
+ // 缩放图片 以适应PDF页面宽度
19
+ var canvasWidth = width; // 图片原始宽度
20
+ var canvasHeight = height; // 图片原始高度
21
+ // 创建一个新的jsPDF实例
22
+ var pdf = new jsPDF({
23
+ // 方向 l:横向 p:纵向
24
+ orientation: orientation_1,
25
+ // 单位
26
+ unit: unit,
27
+ format: [pdfWidth, pdfHeight],
28
+ compress: compress,
29
+ });
30
+ // 一页pdf能显示的canvas高度
31
+ var pageHeight = (canvasWidth / pdfWidth) * pdfHeight;
32
+ // 将绘制图片的宽度 设置 成pdf的宽度 (减去偏移)
33
+ var imgWidth = pdfWidth - leftPadding;
34
+ // 等比例换算绘制图片高度
35
+ var imgHeight = (imgWidth / canvasWidth) * canvasHeight;
36
+ // 需要打印的图片总高度,初始状态和图片高度相等
37
+ var totalHeight = canvasHeight;
38
+ if (totalHeight < pageHeight) {
39
+ pdf.addImage(image, "JPEG", leftPadding, 0, imgWidth, imgHeight);
40
+ // 添加水印
41
+ if (waterConfig) {
42
+ addImageWater(pdf, waterConfig.image, {
43
+ width: waterConfig.width,
44
+ height: waterConfig.height,
45
+ });
46
+ }
47
+ }
48
+ else {
49
+ var position = 0;
50
+ while (totalHeight > 0) {
51
+ pdf.addImage(image, "JPEG", leftPadding, position, imgWidth, imgHeight);
52
+ // 添加水印
53
+ if (waterConfig) {
54
+ addImageWater(pdf, waterConfig.image, {
55
+ width: waterConfig.width,
56
+ height: waterConfig.height,
57
+ });
58
+ }
59
+ totalHeight -= pageHeight;
60
+ //避免添加空白页
61
+ position -= pdfHeight;
62
+ if (totalHeight > 0) {
63
+ pdf.addPage();
64
+ }
65
+ }
66
+ }
67
+ if (isDownload) {
68
+ pdf.save("".concat(pdfName, ".pdf"));
69
+ }
70
+ resolve(pdf);
71
+ }
72
+ catch (err) {
73
+ reject(err);
74
+ }
75
+ });
76
+ }
77
+ function parsePdfFormat(format) {
78
+ if (format === "a4") {
79
+ return [595.28, 841.89];
80
+ }
81
+ if (Array.isArray(format)) {
82
+ return format;
83
+ }
84
+ // 默认返回a4的数值
85
+ return [595.28, 841.89];
86
+ }
87
+ /**
88
+ * 给PDF添加图片水印
89
+ */
90
+ function addImageWater(pdf, imgData, options) {
91
+ var width = (options === null || options === void 0 ? void 0 : options.width) || 200;
92
+ var height = (options === null || options === void 0 ? void 0 : options.height) || 80;
93
+ var x = (options === null || options === void 0 ? void 0 : options.x) || 40;
94
+ var y = (options === null || options === void 0 ? void 0 : options.y) || 140;
95
+ for (var i = 0; i < 4; i++) {
96
+ pdf.addImage(imgData, "PNG", x, y * i, width, height, "1-".concat(i));
97
+ pdf.addImage(imgData, "PNG", x + x + width, y * i, width, height, "2-".concat(i));
98
+ }
99
+ }
100
+
101
+ export { addImageWater, imgToPdf };
@@ -0,0 +1,23 @@
1
+ import printJS from 'print-js';
2
+
3
+ /**
4
+ * 打印页面 直接调用window.print方法打印
5
+ */
6
+ declare function windowPrint(config?: IWindowPrint): void;
7
+ interface IWindowPrint {
8
+ hiddenClassNames?: string[];
9
+ }
10
+ /**
11
+ * 打印页面dom 通过库来打印
12
+ */
13
+ declare function htmlPrint(config: printJS.Configuration): void;
14
+ /**
15
+ * 打印图片
16
+ */
17
+ declare function imgPrint(config: printJS.Configuration): void;
18
+ /**
19
+ * 打印pdf
20
+ */
21
+ declare function pdfPrint(config: printJS.Configuration): void;
22
+
23
+ export { IWindowPrint, htmlPrint, imgPrint, pdfPrint, windowPrint };
@@ -0,0 +1,43 @@
1
+ import { __spreadArray, __assign } from './_virtual/_tslib.js';
2
+ import printJS from 'print-js';
3
+
4
+ /**
5
+ * 打印页面 直接调用window.print方法打印
6
+ */
7
+ function windowPrint(config) {
8
+ if (config === null || config === void 0 ? void 0 : config.hiddenClassNames) {
9
+ var hiddenClassNames = config.hiddenClassNames;
10
+ var printStyleDom = document.getElementById("print--window__00100");
11
+ if (printStyleDom) {
12
+ printStyleDom.remove();
13
+ }
14
+ var styleDom = document.createElement("style");
15
+ styleDom.setAttribute("type", "text/css");
16
+ styleDom.setAttribute("id", "print--window__00100");
17
+ styleDom.innerHTML = "@media print{ ".concat(__spreadArray(__spreadArray([], (hiddenClassNames || []), true), [
18
+ ".non-printable",
19
+ ], false).join(","), " { display: none; } }");
20
+ document.head.appendChild(styleDom);
21
+ }
22
+ window.print();
23
+ }
24
+ /**
25
+ * 打印页面dom 通过库来打印
26
+ */
27
+ function htmlPrint(config) {
28
+ printJS(__assign({ type: "html" }, config));
29
+ }
30
+ /**
31
+ * 打印图片
32
+ */
33
+ function imgPrint(config) {
34
+ printJS(__assign({ type: "image" }, config));
35
+ }
36
+ /**
37
+ * 打印pdf
38
+ */
39
+ function pdfPrint(config) {
40
+ printJS(__assign({ type: "pdf" }, config));
41
+ }
42
+
43
+ export { htmlPrint, imgPrint, pdfPrint, windowPrint };
@@ -0,0 +1,50 @@
1
+ /**
2
+ * 创建水印
3
+ */
4
+ declare function createWater(config: IWaterConfig): {
5
+ imgBase64: string;
6
+ canvasWidth: number;
7
+ canvasHeight: number;
8
+ };
9
+ /**
10
+ * 默认配置
11
+ */
12
+ declare function mergeWaterConfig(waterConfig?: IWaterConfig): {
13
+ width: number;
14
+ height: number;
15
+ rotate: number;
16
+ zIndex: number;
17
+ content: string | string[];
18
+ font: {
19
+ color: string | CanvasGradient | CanvasPattern;
20
+ fontSize: number;
21
+ fontWeight: number | "normal" | "light" | "weight";
22
+ fontStyle: "none" | "normal" | "italic" | "oblique";
23
+ fontFamily: string;
24
+ textAlign: CanvasTextAlign;
25
+ };
26
+ image: string | undefined;
27
+ };
28
+ interface IWaterConfig {
29
+ width?: number;
30
+ height?: number;
31
+ rotate?: number;
32
+ zIndex?: number;
33
+ image?: string;
34
+ content?: string | string[];
35
+ font?: {
36
+ color?: CanvasFillStrokeStyles["fillStyle"];
37
+ fontSize?: number;
38
+ fontWeight?: "normal" | "light" | "weight" | number;
39
+ fontStyle?: "none" | "normal" | "italic" | "oblique";
40
+ fontFamily?: string;
41
+ textAlign?: CanvasTextAlign;
42
+ };
43
+ }
44
+ interface IWaterResult {
45
+ image: string;
46
+ width: number;
47
+ height: number;
48
+ }
49
+
50
+ export { IWaterConfig, IWaterResult, createWater, mergeWaterConfig };
@@ -0,0 +1,66 @@
1
+ /**
2
+ * 创建水印
3
+ */
4
+ function createWater(config) {
5
+ var _a = mergeWaterConfig(config), width = _a.width, height = _a.height, rotate = _a.rotate, content = _a.content, font = _a.font;
6
+ // 创建canvas元素
7
+ var canvas = document.createElement("canvas");
8
+ canvas.width = width * 2;
9
+ canvas.height = height * 2;
10
+ var ctx = canvas.getContext("2d");
11
+ if (ctx) {
12
+ // 设置字体样式
13
+ ctx.font = "".concat(font.fontStyle, " ").concat(font.fontWeight, " ").concat(font.fontSize, "px ").concat(font.fontFamily);
14
+ ctx.fillStyle = font.color;
15
+ ctx.textAlign = font.textAlign;
16
+ // 绘制水印文本的函数
17
+ var drawText = function (textLines, x, y) {
18
+ ctx.save();
19
+ ctx.translate(x, y);
20
+ ctx.rotate((Math.PI / 180) * rotate);
21
+ if (Array.isArray(textLines)) {
22
+ // 多行文本
23
+ textLines.forEach(function (line, index) {
24
+ ctx.fillText(line, 0, index * font.fontSize);
25
+ });
26
+ }
27
+ else {
28
+ // 单行文本
29
+ ctx.fillText(textLines, 0, 0);
30
+ }
31
+ ctx.restore();
32
+ };
33
+ // 绘制两个重叠的水印文本
34
+ drawText(content, width / 2, height / 2); // 中心对齐第一个文本
35
+ drawText(content, (width * 3) / 2, (height * 3) / 2); // 中心对齐第二个文本
36
+ }
37
+ return {
38
+ imgBase64: canvas.toDataURL("image/png"),
39
+ canvasWidth: canvas.width,
40
+ canvasHeight: canvas.height,
41
+ };
42
+ }
43
+ /**
44
+ * 默认配置
45
+ */
46
+ function mergeWaterConfig(waterConfig) {
47
+ var _a, _b, _c, _d, _e, _f;
48
+ return {
49
+ width: (waterConfig === null || waterConfig === void 0 ? void 0 : waterConfig.width) || 200,
50
+ height: (waterConfig === null || waterConfig === void 0 ? void 0 : waterConfig.height) || 60,
51
+ rotate: (waterConfig === null || waterConfig === void 0 ? void 0 : waterConfig.rotate) || -22,
52
+ zIndex: (waterConfig === null || waterConfig === void 0 ? void 0 : waterConfig.zIndex) || 10,
53
+ content: (waterConfig === null || waterConfig === void 0 ? void 0 : waterConfig.content) || "交投集团",
54
+ font: {
55
+ color: ((_a = waterConfig === null || waterConfig === void 0 ? void 0 : waterConfig.font) === null || _a === void 0 ? void 0 : _a.color) || "rgba(0, 0, 0, 0.1)",
56
+ fontSize: ((_b = waterConfig === null || waterConfig === void 0 ? void 0 : waterConfig.font) === null || _b === void 0 ? void 0 : _b.fontSize) || 18,
57
+ fontWeight: ((_c = waterConfig === null || waterConfig === void 0 ? void 0 : waterConfig.font) === null || _c === void 0 ? void 0 : _c.fontWeight) || "normal",
58
+ fontStyle: ((_d = waterConfig === null || waterConfig === void 0 ? void 0 : waterConfig.font) === null || _d === void 0 ? void 0 : _d.fontStyle) || "normal",
59
+ fontFamily: ((_e = waterConfig === null || waterConfig === void 0 ? void 0 : waterConfig.font) === null || _e === void 0 ? void 0 : _e.fontFamily) || "微软雅黑",
60
+ textAlign: ((_f = waterConfig === null || waterConfig === void 0 ? void 0 : waterConfig.font) === null || _f === void 0 ? void 0 : _f.textAlign) || "center",
61
+ },
62
+ image: waterConfig === null || waterConfig === void 0 ? void 0 : waterConfig.image,
63
+ };
64
+ }
65
+
66
+ export { createWater, mergeWaterConfig };
package/dist/index.d.ts CHANGED
@@ -13,3 +13,7 @@ export { batchDownloadFiles, createDeleteFileLink, createDownloadLink, createOri
13
13
  export { calculateAge, parseTime } from './es/parseTime.js';
14
14
  export { BASIC_KEY, EMITTER_CCP_KEY, EMITTER_PCC_KEY, EMITTER_TYPE, TOKEN_KEY } from './es/constants.js';
15
15
  export { convert, html, match, pinyin } from 'pinyin-pro';
16
+ export { domToCanvas } from './es/canvas.js';
17
+ export { imgToPdf } from './es/pdf.js';
18
+ export { htmlPrint, imgPrint, pdfPrint, windowPrint } from './es/print.js';
19
+ export { createWater, mergeWaterConfig } from './es/water.js';
package/dist/index.js CHANGED
@@ -13,3 +13,7 @@ export { batchDownloadFiles, createDeleteFileLink, createDownloadLink, createOri
13
13
  export { calculateAge, parseTime } from './es/parseTime.js';
14
14
  export { BASIC_KEY, EMITTER_CCP_KEY, EMITTER_PCC_KEY, EMITTER_TYPE, TOKEN_KEY } from './es/constants.js';
15
15
  export { convert, html, match, pinyin } from './es/node_modules/pinyin-pro/dist/index.js';
16
+ export { domToCanvas } from './es/canvas.js';
17
+ export { imgToPdf } from './es/pdf.js';
18
+ export { htmlPrint, imgPrint, pdfPrint, windowPrint } from './es/print.js';
19
+ export { createWater, mergeWaterConfig } from './es/water.js';
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "zmdms-utils",
3
- "version": "0.0.35",
3
+ "version": "0.0.37",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.js",
@@ -16,7 +16,10 @@
16
16
  "axios": "^1.4.0",
17
17
  "crypto-js": "^4.1.1",
18
18
  "dayjs": "^1.11.7",
19
- "qiankun": "^2.10.8"
19
+ "qiankun": "^2.10.8",
20
+ "html2canvas": "^1.4.1",
21
+ "jspdf": "^2.5.1",
22
+ "print-js": "^1.6.0"
20
23
  },
21
24
  "dependencies": {
22
25
  "decimal.js": "^10.4.3",
@@ -31,6 +34,9 @@
31
34
  "axios": "^1.4.0",
32
35
  "crypto-js": "^4.1.1",
33
36
  "dayjs": "^1.11.10",
37
+ "html2canvas": "^1.4.1",
38
+ "jspdf": "^2.5.1",
39
+ "print-js": "^1.6.0",
34
40
  "qiankun": "^2.10.8",
35
41
  "rimraf": "^4.4.1",
36
42
  "rollup": "^3.20.2",
package/src/canvas.ts ADDED
@@ -0,0 +1,12 @@
1
+ import html2canvas from "html2canvas";
2
+
3
+ /**
4
+ * dom 转 canvas
5
+ */
6
+ export function domToCanvas(dom: HTMLElement) {
7
+ return html2canvas(dom, {
8
+ useCORS: true, // 是否尝试使用 CORS 从服务器加载映像 由于打印时,会访问dom上的一些图片等资源,解决跨域问题
9
+ allowTaint: true, // 是否允许跨域图像污染画布 允许跨域
10
+ scale: 2, // 用于渲染的比例。默认为浏览器设备像素比 如果dom区域较大,下载会出现部分灰色区域,需要加scale解决
11
+ });
12
+ }
package/src/index.ts CHANGED
@@ -62,3 +62,15 @@ export {
62
62
  * 中文处理
63
63
  */
64
64
  export { pinyin, match, convert, html } from "./pinyin";
65
+
66
+ // canvas相关
67
+ export { domToCanvas } from "./canvas";
68
+
69
+ // pdf 相关
70
+ export { imgToPdf } from "./pdf";
71
+
72
+ // 打印相关
73
+ export { windowPrint, htmlPrint, imgPrint, pdfPrint } from "./print";
74
+
75
+ // 水印相关
76
+ export { createWater, mergeWaterConfig } from "./water";
package/src/pdf.ts ADDED
@@ -0,0 +1,157 @@
1
+ import { RGBAData, jsPDF } from "jspdf";
2
+ import { IWaterResult } from "./water";
3
+
4
+ /**
5
+ * 图片 转 pdf
6
+ */
7
+ export function imgToPdf(config: IImgToPdfConfig) {
8
+ return new Promise((resolve, reject) => {
9
+ try {
10
+ const {
11
+ image,
12
+ width,
13
+ height,
14
+ leftPadding = 20,
15
+ compress = true,
16
+ orientation = "p",
17
+ format = "a4",
18
+ pdfName,
19
+ waterConfig,
20
+ isDownload,
21
+ } = config;
22
+
23
+ // 单位转换
24
+ let [pdfWidth, pdfHeight] = parsePdfFormat(format);
25
+ // 如果是横向的话,需要转换下宽高的位置
26
+ if (orientation === "l") {
27
+ [pdfWidth, pdfHeight] = [pdfHeight, pdfWidth];
28
+ }
29
+ const unit = config.unit || "pt";
30
+
31
+ // 缩放图片 以适应PDF页面宽度
32
+ const canvasWidth = width; // 图片原始宽度
33
+ const canvasHeight = height; // 图片原始高度
34
+
35
+ // 创建一个新的jsPDF实例
36
+ const pdf = new jsPDF({
37
+ // 方向 l:横向 p:纵向
38
+ orientation,
39
+ // 单位
40
+ unit,
41
+ format: [pdfWidth, pdfHeight],
42
+ compress,
43
+ });
44
+ // 一页pdf能显示的canvas高度
45
+ let pageHeight = (canvasWidth / pdfWidth) * pdfHeight;
46
+ // 将绘制图片的宽度 设置 成pdf的宽度 (减去偏移)
47
+ let imgWidth = pdfWidth - leftPadding;
48
+ // 等比例换算绘制图片高度
49
+ let imgHeight = (imgWidth / canvasWidth) * canvasHeight;
50
+ // 需要打印的图片总高度,初始状态和图片高度相等
51
+ let totalHeight = canvasHeight;
52
+ if (totalHeight < pageHeight) {
53
+ pdf.addImage(image, "JPEG", leftPadding, 0, imgWidth, imgHeight);
54
+ // 添加水印
55
+ if (waterConfig) {
56
+ addImageWater(pdf, waterConfig.image, {
57
+ width: waterConfig.width,
58
+ height: waterConfig.height,
59
+ });
60
+ }
61
+ } else {
62
+ let position = 0;
63
+ while (totalHeight > 0) {
64
+ pdf.addImage(
65
+ image,
66
+ "JPEG",
67
+ leftPadding,
68
+ position,
69
+ imgWidth,
70
+ imgHeight
71
+ );
72
+ // 添加水印
73
+ if (waterConfig) {
74
+ addImageWater(pdf, waterConfig.image, {
75
+ width: waterConfig.width,
76
+ height: waterConfig.height,
77
+ });
78
+ }
79
+ totalHeight -= pageHeight;
80
+ //避免添加空白页
81
+ position -= pdfHeight;
82
+ if (totalHeight > 0) {
83
+ pdf.addPage();
84
+ }
85
+ }
86
+ }
87
+ if (isDownload) {
88
+ pdf.save(`${pdfName}.pdf`);
89
+ }
90
+ resolve(pdf);
91
+ } catch (err) {
92
+ reject(err);
93
+ }
94
+ });
95
+ }
96
+ function parsePdfFormat(format: IImgToPdfConfig["format"]) {
97
+ if (format === "a4") {
98
+ return [595.28, 841.89];
99
+ }
100
+ if (Array.isArray(format)) {
101
+ return format;
102
+ }
103
+ // 默认返回a4的数值
104
+ return [595.28, 841.89];
105
+ }
106
+
107
+ /**
108
+ * 给PDF添加图片水印
109
+ */
110
+ export function addImageWater(
111
+ pdf: any,
112
+ imgData: any,
113
+ options?: { width?: number; height?: number; x?: number; y?: number }
114
+ ) {
115
+ const width = options?.width || 200;
116
+ const height = options?.height || 80;
117
+ const x = options?.x || 40;
118
+ const y = options?.y || 140;
119
+ for (let i = 0; i < 4; i++) {
120
+ pdf.addImage(imgData, "PNG", x, y * i, width, height, `1-${i}`);
121
+ pdf.addImage(imgData, "PNG", x + x + width, y * i, width, height, `2-${i}`);
122
+ }
123
+ }
124
+
125
+ interface IImgToPdfConfig {
126
+ /** 图片 base64字符串 */
127
+ image: string | HTMLImageElement | HTMLCanvasElement | Uint8Array | RGBAData;
128
+ width: number;
129
+ height: number;
130
+ /** 是否直接下载pdf */
131
+ isDownload?: boolean;
132
+ /** 生成的pdf左边距 */
133
+ leftPadding?: number;
134
+ /** 导出pdf单位 */
135
+ unit?: "pt" | "mm";
136
+ /**
137
+ * 横向 还是 纵向
138
+ * l: 横向 p:纵向
139
+ */
140
+ orientation?: "p" | "l";
141
+ /**
142
+ * pdf格式 a4 ..
143
+ */
144
+ format?: string | [width: number, height: number];
145
+ /**
146
+ * 生成的pdf是否压缩
147
+ */
148
+ compress?: boolean;
149
+ /**
150
+ * 生成pdf的名称
151
+ */
152
+ pdfName?: string;
153
+ /**
154
+ * 水印相关信息
155
+ */
156
+ waterConfig?: IWaterResult;
157
+ }
package/src/print.ts ADDED
@@ -0,0 +1,57 @@
1
+ // 打印相关方法
2
+ import printJS from "print-js";
3
+
4
+ /**
5
+ * 打印页面 直接调用window.print方法打印
6
+ */
7
+ export function windowPrint(config?: IWindowPrint) {
8
+ if (config?.hiddenClassNames) {
9
+ const { hiddenClassNames } = config;
10
+ const printStyleDom = document.getElementById("print--window__00100");
11
+ if (printStyleDom) {
12
+ printStyleDom.remove();
13
+ }
14
+ const styleDom = document.createElement("style");
15
+ styleDom.setAttribute("type", "text/css");
16
+ styleDom.setAttribute("id", "print--window__00100");
17
+ styleDom.innerHTML = `@media print{ ${[
18
+ ...(hiddenClassNames || []),
19
+ ".non-printable",
20
+ ].join(",")} { display: none; } }`;
21
+ document.head.appendChild(styleDom);
22
+ }
23
+ window.print();
24
+ }
25
+ export interface IWindowPrint {
26
+ hiddenClassNames?: string[];
27
+ }
28
+
29
+ /**
30
+ * 打印页面dom 通过库来打印
31
+ */
32
+ export function htmlPrint(config: printJS.Configuration) {
33
+ printJS({
34
+ type: "html",
35
+ ...config,
36
+ });
37
+ }
38
+
39
+ /**
40
+ * 打印图片
41
+ */
42
+ export function imgPrint(config: printJS.Configuration) {
43
+ printJS({
44
+ type: "image",
45
+ ...config,
46
+ });
47
+ }
48
+
49
+ /**
50
+ * 打印pdf
51
+ */
52
+ export function pdfPrint(config: printJS.Configuration) {
53
+ printJS({
54
+ type: "pdf",
55
+ ...config,
56
+ });
57
+ }
package/src/water.ts ADDED
@@ -0,0 +1,101 @@
1
+ /**
2
+ * 创建水印
3
+ */
4
+ export function createWater(config: IWaterConfig) {
5
+ const { width, height, rotate, content, font } = mergeWaterConfig(config);
6
+
7
+ // 创建canvas元素
8
+ const canvas = document.createElement("canvas");
9
+ canvas.width = width * 2;
10
+ canvas.height = height * 2;
11
+ const ctx = canvas.getContext("2d");
12
+ if (ctx) {
13
+ // 设置字体样式
14
+ ctx.font = `${font.fontStyle} ${font.fontWeight} ${font.fontSize}px ${font.fontFamily}`;
15
+ ctx.fillStyle = font.color;
16
+ ctx.textAlign = font.textAlign;
17
+
18
+ // 绘制水印文本的函数
19
+ const drawText = (textLines: string | string[], x: number, y: number) => {
20
+ ctx.save();
21
+ ctx.translate(x, y);
22
+ ctx.rotate((Math.PI / 180) * rotate);
23
+
24
+ if (Array.isArray(textLines)) {
25
+ // 多行文本
26
+ textLines.forEach((line, index) => {
27
+ ctx.fillText(line, 0, index * font.fontSize);
28
+ });
29
+ } else {
30
+ // 单行文本
31
+ ctx.fillText(textLines, 0, 0);
32
+ }
33
+
34
+ ctx.restore();
35
+ };
36
+
37
+ // 绘制两个重叠的水印文本
38
+ drawText(content, width / 2, height / 2); // 中心对齐第一个文本
39
+ drawText(content, (width * 3) / 2, (height * 3) / 2); // 中心对齐第二个文本
40
+ }
41
+
42
+ return {
43
+ imgBase64: canvas.toDataURL("image/png"),
44
+ canvasWidth: canvas.width,
45
+ canvasHeight: canvas.height,
46
+ };
47
+ }
48
+
49
+ /**
50
+ * 默认配置
51
+ */
52
+ export function mergeWaterConfig(waterConfig?: IWaterConfig) {
53
+ return {
54
+ width: waterConfig?.width || 200,
55
+ height: waterConfig?.height || 60,
56
+ rotate: waterConfig?.rotate || -22,
57
+ zIndex: waterConfig?.zIndex || 10,
58
+ content: waterConfig?.content || "交投集团",
59
+ font: {
60
+ color: waterConfig?.font?.color || "rgba(0, 0, 0, 0.1)",
61
+ fontSize: waterConfig?.font?.fontSize || 18,
62
+ fontWeight: waterConfig?.font?.fontWeight || "normal",
63
+ fontStyle: waterConfig?.font?.fontStyle || "normal",
64
+ fontFamily: waterConfig?.font?.fontFamily || "微软雅黑",
65
+ textAlign: waterConfig?.font?.textAlign || "center",
66
+ },
67
+ image: waterConfig?.image,
68
+ };
69
+ }
70
+
71
+ // 生成水印相关配置
72
+ export interface IWaterConfig {
73
+ // 水印宽度 120
74
+ width?: number;
75
+ // 水印高度 64
76
+ height?: number;
77
+ // 旋转角度 -22
78
+ rotate?: number;
79
+ // 层级 10
80
+ zIndex?: number;
81
+ // 图片源
82
+ image?: string;
83
+ // 文本水印内容
84
+ content?: string | string[];
85
+ // 水印字体大小
86
+ font?: {
87
+ color?: CanvasFillStrokeStyles["fillStyle"];
88
+ fontSize?: number;
89
+ fontWeight?: "normal" | "light" | "weight" | number;
90
+ fontStyle?: "none" | "normal" | "italic" | "oblique";
91
+ fontFamily?: string;
92
+ textAlign?: CanvasTextAlign;
93
+ };
94
+ }
95
+
96
+ // 水印相关信息
97
+ export interface IWaterResult {
98
+ image: string;
99
+ width: number;
100
+ height: number;
101
+ }