File size: 13,978 Bytes
dc4e6da
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
MEASURE_DIMENSIONS_V1 = """

                            () => {

                                const body = document.body;

                                const html = document.documentElement;



                                // Force layout calculation

                                body.offsetHeight;



                                // Get body's computed style to extract margins

                                const bodyStyle = window.getComputedStyle(body);

                                const marginTop = parseFloat(bodyStyle.marginTop) || 0;

                                const marginBottom = parseFloat(bodyStyle.marginBottom) || 0;

                                const marginLeft = parseFloat(bodyStyle.marginLeft) || 0;

                                const marginRight = parseFloat(bodyStyle.marginRight) || 0;



                                const bodyRect = body.getBoundingClientRect();



                                // Find the furthest extent of content

                                let maxY = bodyRect.bottom;

                                let maxX = bodyRect.right;



                                const allElements = body.querySelectorAll('*');

                                allElements.forEach(el => {

                                    const rect = el.getBoundingClientRect();

                                    const style = window.getComputedStyle(el);



                                    if (style.display === 'none' ||

                                        style.visibility === 'hidden' ||

                                        rect.width === 0 || rect.height === 0) {

                                        return;

                                    }



                                    if (rect.bottom > maxY) maxY = rect.bottom;

                                    if (rect.right > maxX) maxX = rect.right;

                                });



                                // CRITICAL FIX: Include body margins in the total size

                                // The PDF needs to be tall enough to contain the margins too!

                                const totalWidth = Math.ceil(maxX - bodyRect.left + marginRight + 5);

                                const totalHeight = Math.ceil(maxY + marginBottom + 5);



                                return {

                                    width: totalWidth,

                                    height: totalHeight,

                                    debug: {

                                        marginTop,

                                        marginBottom,

                                        marginLeft,

                                        marginRight,

                                        bodyRectTop: bodyRect.top,

                                        bodyRectBottom: bodyRect.bottom,

                                        maxY,

                                        contentHeightWithoutMargin: Math.ceil(maxY - bodyRect.top)

                                    }

                                };

                            }

                            """

MEASURE_DIMENSIONS_V2 = """

() => {

    const body = document.body;

    const html = document.documentElement;



    // Force layout calculation

    body.offsetHeight;

    html.offsetHeight;



    // Get body's computed style to extract margins

    const bodyStyle = window.getComputedStyle(body);

    const marginTop = parseFloat(bodyStyle.marginTop) || 0;

    const marginBottom = parseFloat(bodyStyle.marginBottom) || 0;

    const marginLeft = parseFloat(bodyStyle.marginLeft) || 0;

    const marginRight = parseFloat(bodyStyle.marginRight) || 0;



    // Strategy: Find the bounding box of ALL visible content

    // This works for narrow receipts, wide tables, multi-column, everything



    let minX = Infinity;

    let minY = Infinity;

    let maxX = -Infinity;

    let maxY = -Infinity;



    // Check body itself

    const bodyRect = body.getBoundingClientRect();

    if (bodyRect.width > 0 && bodyRect.height > 0) {

        minX = Math.min(minX, bodyRect.left);

        minY = Math.min(minY, bodyRect.top);

        maxX = Math.max(maxX, bodyRect.right);

        maxY = Math.max(maxY, bodyRect.bottom);

    }



    // Check all elements to find true content bounds

    const allElements = document.querySelectorAll('*');

    allElements.forEach(el => {

        const rect = el.getBoundingClientRect();

        const style = window.getComputedStyle(el);



        // Skip hidden elements

        if (style.display === 'none' ||

            style.visibility === 'hidden' ||

            rect.width === 0 ||

            rect.height === 0) {

            return;

        }



        // Skip script/style tags

        if (el.tagName === 'SCRIPT' || el.tagName === 'STYLE') {

            return;

        }



        minX = Math.min(minX, rect.left);

        minY = Math.min(minY, rect.top);

        maxX = Math.max(maxX, rect.right);

        maxY = Math.max(maxY, rect.bottom);

    });



    // Fallback if no content found

    if (minX === Infinity) {

        minX = 0;

        minY = 0;

        maxX = bodyRect.right;

        maxY = bodyRect.bottom;

    }



    // Calculate total dimensions

    // Width: from leftmost to rightmost content + right margin

    // Height: from topmost to bottommost content + bottom margin

    const buffer = 5; // Small safety buffer



    const totalWidth = Math.ceil(maxX - minX + marginRight + buffer);

    const totalHeight = Math.ceil(maxY - minY + marginBottom + buffer);



    return {

        width: totalWidth,

        height: totalHeight,

        debug: {

            marginTop,

            marginBottom,

            marginLeft,

            marginRight,

            minX,

            minY,

            maxX,

            maxY,

            bodyWidth: bodyRect.width,

            bodyHeight: bodyRect.height

        }

    };

}

"""

MEASURE_DIMENSIONS_V3 = """

() => {

    const body = document.body;



    // Force layout

    body.offsetHeight;



    const bodyRect = body.getBoundingClientRect();



    // For receipts/documents with body padding, the body rect already includes everything

    // Just add a small buffer

    const buffer = 5;



    return {

        width: Math.ceil(bodyRect.width + buffer),

        height: Math.ceil(bodyRect.height + buffer)

    };

}

"""


MEASURE_DIMENSIONS_V4 = """

() => {

    const body = document.body;

    const html = document.documentElement;



    // Force layout calculation

    body.offsetHeight;

    html.offsetHeight;



    // Get body's computed style to extract margins

    const bodyStyle = window.getComputedStyle(body);

    const marginTop = parseFloat(bodyStyle.marginTop) || 0;

    const marginBottom = parseFloat(bodyStyle.marginBottom) || 0;

    const marginLeft = parseFloat(bodyStyle.marginLeft) || 0;

    const marginRight = parseFloat(bodyStyle.marginRight) || 0;



    // Get body padding as well

    const paddingTop = parseFloat(bodyStyle.paddingTop) || 0;

    const paddingBottom = parseFloat(bodyStyle.paddingBottom) || 0;

    const paddingLeft = parseFloat(bodyStyle.paddingLeft) || 0;

    const paddingRight = parseFloat(bodyStyle.paddingRight) || 0;



    // Strategy: Find the bounding box of ALL visible content

    let minX = Infinity;

    let minY = Infinity;

    let maxX = -Infinity;

    let maxY = -Infinity;



    // Check body itself

    const bodyRect = body.getBoundingClientRect();

    if (bodyRect.width > 0 && bodyRect.height > 0) {

        minX = Math.min(minX, bodyRect.left);

        minY = Math.min(minY, bodyRect.top);

        maxX = Math.max(maxX, bodyRect.right);

        maxY = Math.max(maxY, bodyRect.bottom);

    }



    // Check all elements to find true content bounds

    const allElements = document.querySelectorAll('*');

    allElements.forEach(el => {

        const rect = el.getBoundingClientRect();

        const style = window.getComputedStyle(el);



        // Skip hidden elements

        if (style.display === 'none' ||

            style.visibility === 'hidden' ||

            rect.width === 0 ||

            rect.height === 0) {

            return;

        }



        // Skip script/style tags

        if (el.tagName === 'SCRIPT' || el.tagName === 'STYLE') {

            return;

        }



        minX = Math.min(minX, rect.left);

        minY = Math.min(minY, rect.top);

        maxX = Math.max(maxX, rect.right);

        maxY = Math.max(maxY, rect.bottom);

    });



    // Fallback if no content found

    if (minX === Infinity) {

        minX = 0;

        minY = 0;

        maxX = bodyRect.right;

        maxY = bodyRect.bottom;

    }



    // Calculate total dimensions

    // CRITICAL FIX: The viewport starts at 0,0 but content might be offset

    // We need the full document size, not just content span



    // For width: take the maximum of either the rightmost content or body width

    // For height: take the maximum of either the bottommost content or body height

    const buffer = 5;



    // Option A: Measure from viewport origin (0,0) to furthest content

    const totalWidth = Math.ceil(maxX + buffer);

    const totalHeight = Math.ceil(maxY + buffer);



    // Option B: Also consider body's full width (in case body is wider than content)

    const bodyFullWidth = bodyRect.width;

    const bodyFullHeight = bodyRect.height;



    // Use whichever is larger

    const finalWidth = Math.max(totalWidth, bodyFullWidth);

    const finalHeight = Math.max(totalHeight, bodyFullHeight);



    return {

        width: finalWidth,

        height: finalHeight,

        debug: {

            marginTop,

            marginBottom,

            marginLeft,

            marginRight,

            paddingTop,

            paddingBottom,

            paddingLeft,

            paddingRight,

            minX,

            minY,

            maxX,

            maxY,

            bodyWidth: bodyRect.width,

            bodyHeight: bodyRect.height,

            bodyLeft: bodyRect.left,

            bodyTop: bodyRect.top,

            totalWidth,

            totalHeight,

            bodyFullWidth,

            bodyFullHeight

        }

    };

}

"""

MEASURE_DIMENSIONS = """

() => {

    const body = document.body;

    const html = document.documentElement;



    // Force layout

    body.offsetHeight;

    html.offsetHeight;



    const bodyStyle = window.getComputedStyle(body);

    const paddingTop = parseFloat(bodyStyle.paddingTop) || 0;

    const paddingBottom = parseFloat(bodyStyle.paddingBottom) || 0;

    const paddingLeft = parseFloat(bodyStyle.paddingLeft) || 0;

    const paddingRight = parseFloat(bodyStyle.paddingRight) || 0;



    // Strategy: Find bounding box of ALL visible content

    let minX = Infinity;

    let minY = Infinity;

    let maxX = -Infinity;

    let maxY = -Infinity;



    const bodyRect = body.getBoundingClientRect();



    // Check all elements (not just body children, in case of deep nesting)

    const allElements = document.querySelectorAll('body *');

    let hasContent = false;



    allElements.forEach(el => {

        // Skip scripts, styles, and hidden elements

        if (el.tagName === 'SCRIPT' || el.tagName === 'STYLE') return;



        const rect = el.getBoundingClientRect();

        const style = window.getComputedStyle(el);



        if (style.display === 'none' || style.visibility === 'hidden' ||

            rect.width === 0 || rect.height === 0) {

            return;

        }



        hasContent = true;

        minX = Math.min(minX, rect.left);

        minY = Math.min(minY, rect.top);

        maxX = Math.max(maxX, rect.right);

        maxY = Math.max(maxY, rect.bottom);

    });



    // Fallback if no content found

    if (!hasContent || minX === Infinity) {

        return {

            width: Math.ceil(bodyRect.width + 5),

            height: Math.ceil(bodyRect.height + 5)

        };

    }



    // Now decide: do we measure from content bounds or from body bounds?



    // Approach 1: Content-based (for narrow receipts)

    // Width = actual content span + left padding + right padding

    const contentWidth = maxX - minX;

    const contentHeight = maxY - minY;

    const contentBasedWidth = contentWidth + paddingLeft + paddingRight;

    const contentBasedHeight = contentHeight + paddingTop + paddingBottom;



    // Approach 2: Body-based (for full-width documents)

    // Width = body's full width

    const bodyBasedWidth = bodyRect.width;

    const bodyBasedHeight = bodyRect.height;



    // Decision logic:

    // If content is significantly narrower than body (e.g., < 70% of body width),

    // it's likely a centered narrow layout like a receipt

    // Otherwise, it's a full-width document



    const contentWidthRatio = contentWidth / bodyRect.width;

    const isNarrowCentered = contentWidthRatio < 0.7;



    let finalWidth, finalHeight;



    if (isNarrowCentered) {

        // Use content-based measurement (receipt-style)

        finalWidth = contentBasedWidth;

        finalHeight = Math.max(contentBasedHeight, bodyBasedHeight); // Use max for height

    } else {

        // Use body-based measurement (full-width document)

        finalWidth = bodyBasedWidth;

        finalHeight = bodyBasedHeight;

    }



    const buffer = 5;



    return {

        width: Math.ceil(finalWidth + buffer),

        height: Math.ceil(finalHeight + buffer),

        debug: {

            isNarrowCentered,

            contentWidthRatio: contentWidthRatio.toFixed(2),

            contentWidth,

            contentHeight,

            bodyWidth: bodyRect.width,

            bodyHeight: bodyRect.height,

            approach: isNarrowCentered ? 'content-based' : 'body-based'

        }

    };

}

"""