-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdf.js
More file actions
216 lines (183 loc) · 5.66 KB
/
Copy pathpdf.js
File metadata and controls
216 lines (183 loc) · 5.66 KB
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
import puppeteer from 'puppeteer';
(async () => {
console.log("Launching browser...");
const browser = await puppeteer.launch({
headless: "new",
args: ['--no-sandbox', '--disable-setuid-sandbox']
});
const page = await browser.newPage();
// Set viewport
await page.setViewport({ width: 1280, height: 1024 });
console.log("Navigating to page...");
await page.goto('http://localhost:3000', { waitUntil: 'networkidle0', timeout: 60000 });
console.log("Scrolling to trigger lazy loaded images...");
await page.evaluate(async () => {
await new Promise((resolve) => {
let totalHeight = 0;
const distance = 100;
const timer = setInterval(() => {
const scrollHeight = document.body.scrollHeight;
window.scrollBy(0, distance);
totalHeight += distance;
if (totalHeight >= scrollHeight) {
clearInterval(timer);
resolve();
}
}, 50);
});
window.scrollTo(0, 0);
});
await new Promise(r => setTimeout(r, 2000));
console.log("Injecting PDF print styles for Dark Mode...");
const documentStyles = `
/* === RESET & VISIBILITY === */
* {
-webkit-print-color-adjust: exact !important;
print-color-adjust: exact !important;
content-visibility: visible !important;
contain-intrinsic-size: auto !important;
}
/* Hide Navigation/UI */
header, aside, button, .mobile-index-trigger, .scroll-to-top, .theme-toggle {
display: none !important;
}
.fixed { display: none !important; }
/* === FULL BLACK BACKGROUND (NO MARGINS) === */
@page {
margin: 0;
size: A4;
background-color: #050505;
}
body {
background-color: #050505 !important;
color: #e5e5e5 !important;
margin: 0 !important;
/* Simulate margins via padding inside the body */
padding: 2cm 1.5cm 2cm 1.5cm !important;
min-height: 100vh;
}
main {
padding: 0 !important;
max-width: 100% !important;
margin: 0 !important;
}
section {
margin-bottom: 40px !important;
page-break-inside: auto;
}
/* === FIXED HEADER/FOOTER VIA CSS (Since we removed PDF margins) === */
/* This will repeat on every page in Chrome/Puppeteer print */
#print-header, #print-footer {
position: fixed;
left: 0;
right: 0;
text-align: center;
color: #666; /* Subtle gray text */
font-family: sans-serif;
font-size: 9px;
background-color: #050505; /* Match bg to cover content if shifting */
padding: 5px 0;
z-index: 9999;
}
#print-header {
top: 0.5cm;
border-bottom: 1px solid #222;
width: 90%;
margin: 0 auto;
display: flex;
justify-content: space-between;
}
#print-footer {
bottom: 0.5cm;
}
/* Layout Logic */
@media print {
.lg\\:flex-row {
flex-direction: row !important;
}
.lg\\:w-1\\/3 {
width: 33.333333% !important;
}
.lg\\:w-5\\/12 {
width: 41.666667% !important;
}
.flex-1 {
flex: 1 1 0% !important;
}
img {
break-inside: avoid;
page-break-inside: avoid;
}
}
/* === REMOVE "MARKING" (GOLD/YELLOW COLORS) === */
/* Force simple white/gray styling for everything */
/* Kill Gradients on text */
.text-transparent, .bg-clip-text {
background: none !important;
-webkit-text-fill-color: initial !important;
color: #ffffff !important; /* Pure white for titles */
}
/* Kill Gold Colors */
.text-premium-gold {
color: #ffffff !important; /* Make pills white */
}
/* Kill Gold Borders */
.border-premium-gold, .border-premium-gold\\/30 {
border-color: #444 !important; /* Dark gray border */
}
/* Pills specific cleanup */
.text-xs.font-mono.uppercase {
color: #bbb !important; /* Soft gray for pill text */
border-color: #444 !important;
}
/* H2 Titles specifically */
h2 {
color: #ffffff !important;
background: none !important;
}
/* Links */
a { color: #60a5fa !important; text-decoration: none !important; }
/* Tables */
table {
border-color: #333 !important;
}
th, td {
border: 1px solid #333 !important;
color: #e5e5e5 !important;
}
th {
background-color: #111 !important;
color: #fff !important;
}
`;
// === DARK MODE GENERATION ===
console.log("Switching to Dark Mode...");
// Force adding 'dark' class
await page.evaluate(() => {
document.documentElement.classList.add('dark');
// Inject custom header/footer divs into the body for CSS fixed printing
const header = document.createElement('div');
header.id = 'print-header';
header.innerHTML = '<span>Economia Aziendale</span><span>Author: Singh Rishapveer</span>';
document.body.prepend(header);
const footer = document.createElement('div');
footer.id = 'print-footer';
footer.innerHTML = 'Singh Rishapveer'; // Page numbers are hard with pure CSS fixed, sticking to Author name
document.body.append(footer);
});
await page.evaluate((styles) => {
const styleEl = document.createElement('style');
styleEl.innerHTML = styles;
document.head.appendChild(styleEl);
}, documentStyles);
console.log("Generating website_dark.pdf...");
await page.pdf({
path: 'website_dark.pdf',
// MARGIN 0 IS KEY FOR FULL BACKGROUND
margin: { top: 0, right: 0, bottom: 0, left: 0 },
format: 'A4',
printBackground: true,
});
await browser.close();
console.log("Done.");
})();