@idevs/corelib 0.0.95 → 0.0.97

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.
@@ -2,6 +2,10 @@ import { deepClone, postToService } from '@serenity-is/corelib'
2
2
  import { IdevsExportOptions, IdevsExportRequest } from '../globals'
3
3
  import html2pdf from 'html2pdf.js'
4
4
  import { jsPDF } from 'jspdf'
5
+ import pdfMake from 'pdfmake/build/pdfmake'
6
+ import { vfsFonts } from './custom-fonts'
7
+ import htmlToPdfmake from 'html-to-pdfmake'
8
+ import { Margins, PageOrientation, PageSize, Style, TDocumentDefinitions } from 'pdfmake/interfaces'
5
9
 
6
10
  export function doExportPdf(options: IdevsExportOptions): void {
7
11
  const grid = options.grid
@@ -61,3 +65,115 @@ export function generatePdf(content: string, option?: generatePdfOption): void {
61
65
  })
62
66
  }
63
67
  }
68
+
69
+ export function makePdf(html: string, options?: PageOptions): Promise<string> {
70
+ return new Promise((resolve, reject) => {
71
+ if (!html) {
72
+ reject('No html to make a pdf')
73
+ } else {
74
+ pdfMake.vfs = vfsFonts
75
+ pdfMake.fonts = {
76
+ Roboto: {
77
+ normal: 'Roboto-Regular.ttf',
78
+ bold: 'Roboto-Medium.ttf',
79
+ italics: 'Roboto-Italic.ttf',
80
+ bolditalics: 'Roboto-MediumItalic.ttf',
81
+ },
82
+ THSarabun: {
83
+ normal: 'THSarabun.ttf',
84
+ bold: 'THSarabun Bold.ttf',
85
+ italics: 'THSarabun Italic.ttf',
86
+ bolditalics: 'THSarabun Bold Italic.ttf',
87
+ },
88
+ }
89
+
90
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
91
+ const def: any = htmlToPdfmake(html, { tableAutoSize: true })
92
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
93
+ let main = def.find((el: any) => el['nodeName'] == 'MAIN')
94
+ if (!main) {
95
+ return
96
+ }
97
+ main = removeEmptyTextNodes(main)
98
+
99
+ const defStyle = options?.defaultStyle || {
100
+ font: 'THSarabun',
101
+ }
102
+
103
+ const margin = options?.pageMargins ?? 40
104
+ const pageSize = options?.pageSize ?? 'A4'
105
+ const pageOrientation = options?.pageOrientation ?? 'portrait'
106
+
107
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
108
+ let header: any = def.find((el: any) => el['nodeName'] == 'HEADER')
109
+ header = removeEmptyTextNodes(header)
110
+ const hd = header ? JSON.stringify(header) : undefined
111
+
112
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
113
+ let footer = def.find((el: any) => el['nodeName'] == 'FOOTER')
114
+ if (footer) {
115
+ footer = removeEmptyTextNodes(footer)
116
+ }
117
+ const ft = footer ? JSON.stringify(footer) : undefined
118
+
119
+ const content: TDocumentDefinitions = {
120
+ content: main,
121
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
122
+ header: function (currentPage, pageCount, pageSize) {
123
+ if (!hd) {
124
+ return ''
125
+ }
126
+
127
+ const h = hd.replace('{{pageNo}}', currentPage.toString()).replace('{{totalPages}}', pageCount.toString())
128
+ return JSON.parse(h)
129
+ },
130
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
131
+ footer: function (currentPage, pageCount, pageSize) {
132
+ if (!ft) {
133
+ return ''
134
+ }
135
+ const f = ft.replace('{{pageNo}}', currentPage.toString()).replace('{{totalPages}}', pageCount.toString())
136
+ return JSON.parse(f)
137
+ },
138
+ pageSize: pageSize,
139
+ pageOrientation: pageOrientation,
140
+ pageMargins: margin,
141
+ defaultStyle: defStyle,
142
+ }
143
+
144
+ pdfMake.createPdf(content).open()
145
+
146
+ resolve('Make pdf successfully')
147
+ }
148
+ })
149
+ }
150
+
151
+ export type PageOptions = {
152
+ defaultStyle?: Style | undefined
153
+ pageSize?: PageSize | undefined
154
+ pageOrientation?: PageOrientation | undefined
155
+ pageMargins?: Margins | undefined
156
+ }
157
+
158
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
159
+ const removeEmptyTextNodes = (obj: any): any => {
160
+ // If obj is an array, iterate through each element and filter
161
+ if (Array.isArray(obj)) {
162
+ return obj
163
+ .map(removeEmptyTextNodes) // Recursively apply the function
164
+ .filter(item => !(item && item.text === ' ')) // Remove elements where text is " "
165
+ }
166
+
167
+ // If obj is an object, check each key
168
+ if (typeof obj === 'object' && obj !== null) {
169
+ // eslint-disable-next-line prefer-const
170
+ for (let key in obj) {
171
+ // eslint-disable-next-line no-prototype-builtins
172
+ if (obj.hasOwnProperty(key)) {
173
+ obj[key] = removeEmptyTextNodes(obj[key])
174
+ }
175
+ }
176
+ }
177
+
178
+ return obj
179
+ }