-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpdf.html
More file actions
135 lines (124 loc) · 4.53 KB
/
pdf.html
File metadata and controls
135 lines (124 loc) · 4.53 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
---
layout: generator
title: "PDF Generator"
subtitle: "Generate mock PDF with custom configuration"
tab_id: "PDF"
generator_script: "/js/generators/pdf.js"
extra_scripts:
- "https://cdnjs.cloudflare.com/ajax/libs/jspdf/2.5.1/jspdf.umd.min.js"
redirect_from:
- /pdf.html
---
<h2 class="text-2xl font-bold mb-4">PDF Generator</h2>
<form id="form-pdf" class="space-y-4">
<div class="form-control">
<label class="label">
<span class="label-text">Page Format</span>
</label>
<select name="pageFormat" class="select select-bordered w-full">
<option value="a4">A4</option>
<option value="letter">Letter</option>
<option value="a3">A3</option>
</select>
</div>
<div class="form-control">
<label class="label">
<span class="label-text">Number of Pages</span>
</label>
<input
type="number"
name="pages"
value="1"
min="1"
class="input input-bordered w-full"
/>
<label class="label">
<span class="label-text-alt">Disabled if size is specified</span>
</label>
</div>
<div class="divider">OR</div>
<div class="form-control">
<label class="label">
<span class="label-text">Target Size (Optional)</span>
</label>
<div class="flex flex-col sm:flex-row gap-2">
<input
type="number"
name="size"
min="0"
step="0.01"
class="input input-bordered"
placeholder="Leave empty to use pages"
/>
<select name="sizeUnit" class="select select-bordered">
<option value="KB">KB</option>
<option value="MB">MB</option>
</select>
</div>
</div>
<button type="submit" class="btn btn-primary w-full">
Generate & Download
</button>
</form>
<script>
document
.getElementById("form-pdf")
.addEventListener("submit", async (e) => {
e.preventDefault();
const formData = new FormData(e.target);
const config = {
pageFormat: formData.get("pageFormat"),
pages: parseInt(formData.get("pages")) || null,
size: formData.get("size")
? parseFloat(formData.get("size"))
: null,
sizeUnit: formData.get("sizeUnit"),
};
try {
const blob = await generatePDF(config);
logToUI("Preparing file...");
const shareTitle = config.size
? `PDF ${config.pageFormat.toUpperCase()} ${config.size} ${config.sizeUnit}`
: `PDF ${config.pageFormat.toUpperCase()} ${config.pages} pages`;
const filename = config.size
? `PDF_${config.pageFormat.toUpperCase()}_${config.size}_${config.sizeUnit}.pdf`
: `PDF_${config.pageFormat.toUpperCase()}_${config.pages}_pages.pdf`;
const result = await downloadOrShareFile(blob, filename, "pdf", shareTitle);
if (result.method === 'download') {
logToUI("✓ Download complete!");
}
// Track in Google Analytics
gtag("event", "generate_pdf", {
event_category: "file_generation",
event_label: config.pageFormat,
page_format: config.pageFormat,
pages: config.pages || "size_based",
has_size: config.size ? "yes" : "no",
});
} catch (error) {
logToUI(`❌ Error: ${error.message}`);
showError("PDF generation failed: " + error.message);
hideProgress();
}
});
// Mutual exclusion logic
const pdfSizeInput = document.querySelector('input[name="size"]');
const pdfPagesInput = document.querySelector('input[name="pages"]');
pdfSizeInput.addEventListener("input", (e) => {
if (e.target.value) {
pdfPagesInput.disabled = true;
pdfPagesInput.value = "";
} else {
pdfPagesInput.disabled = false;
pdfPagesInput.value = 1;
}
});
pdfPagesInput.addEventListener("input", (e) => {
if (e.target.value) {
pdfSizeInput.disabled = true;
pdfSizeInput.value = "";
} else {
pdfSizeInput.disabled = false;
}
});
</script>