@farisabid/foliokit 0.0.0-stage → 1.0.0
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.
- package/LICENSE +21 -0
- package/README.md +151 -2
- package/bin/foliokit.js +476 -0
- package/package.json +46 -4
- package/public/css/main.css +915 -0
- package/public/css/tokens.css +29 -0
- package/public/favicon.svg +17 -0
- package/public/index.html +747 -0
- package/public/js/core/pdf-engine.js +69 -0
- package/public/js/core/state.js +365 -0
- package/public/js/tools/dev-tools.js +1508 -0
- package/public/js/tools/edit-pdf.js +1618 -0
- package/public/js/tools/office-tools.js +221 -0
- package/public/js/tools/pdf-ops.js +949 -0
- package/public/js/tools/sign-pdf.js +198 -0
- package/public/manifest.json +26 -0
- package/public/sw.js +88 -0
- package/public/vendor/fonts/inter-1.woff2 +0 -0
- package/public/vendor/fonts/inter-2.woff2 +0 -0
- package/public/vendor/fonts/inter-3.woff2 +0 -0
- package/public/vendor/fonts/inter-4.woff2 +0 -0
- package/public/vendor/fonts/inter-5.woff2 +0 -0
- package/public/vendor/fonts/inter-6.woff2 +0 -0
- package/public/vendor/fonts/inter-7.woff2 +0 -0
- package/public/vendor/fonts/inter.css +316 -0
- package/public/vendor/fonts/jbmono-1.woff2 +0 -0
- package/public/vendor/fonts/jbmono-2.woff2 +0 -0
- package/public/vendor/fonts/jbmono-3.woff2 +0 -0
- package/public/vendor/fonts/jbmono-4.woff2 +0 -0
- package/public/vendor/fonts/jbmono-5.woff2 +0 -0
- package/public/vendor/fonts/jbmono-6.woff2 +0 -0
- package/public/vendor/fonts/jbmono.css +109 -0
- package/public/vendor/jszip.min.js +13 -0
- package/public/vendor/pdf-lib.min.js +16 -0
- package/public/vendor/pdf.min.js +22 -0
- package/public/vendor/pdf.worker.min.js +22 -0
- package/public/vendor/qrcode.min.js +1 -0
- package/server.js +125 -0
- package/start.bat +19 -0
- package/start.sh +12 -0
|
@@ -0,0 +1,949 @@
|
|
|
1
|
+
/* Foliokit — PDF Operations
|
|
2
|
+
* Merge, Split, Organize, Compress, PDF<->Img, Watermark, PageNum, Rotate, Protect, Extract.
|
|
3
|
+
*/
|
|
4
|
+
|
|
5
|
+
// ==========================================
|
|
6
|
+
// 7. PDF TOOL DISPATCH
|
|
7
|
+
// ==========================================
|
|
8
|
+
const PDF_TOOL_META = {
|
|
9
|
+
merge: { title: 'Gabung PDF', action: 'Gabungkan' },
|
|
10
|
+
split: { title: 'Pisah Halaman', action: 'Pisahkan' },
|
|
11
|
+
organize: { title: 'Atur Halaman', action: 'Simpan PDF' },
|
|
12
|
+
compress: { title: 'Kompres Ukuran', action: 'Kompres' },
|
|
13
|
+
pdf2img: { title: 'PDF ke Gambar', action: 'Ekstrak Gambar' },
|
|
14
|
+
img2pdf: { title: 'Gambar ke PDF', action: 'Buat PDF' },
|
|
15
|
+
watermark: { title: 'Watermark', action: 'Terapkan' },
|
|
16
|
+
pagenum: { title: 'Nomor Halaman', action: 'Terapkan' },
|
|
17
|
+
rotate: { title: 'Putar Halaman', action: 'Putar' },
|
|
18
|
+
protect: { title: 'Proteksi Sandi', action: 'Terapkan' },
|
|
19
|
+
extract: { title: 'Ekstrak Teks', action: 'Ekstrak' },
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
function openPdfTool(id) {
|
|
23
|
+
const meta = PDF_TOOL_META[id] || { title: 'Alat PDF', action: 'Proses' };
|
|
24
|
+
document.getElementById('pdf-title').textContent = meta.title;
|
|
25
|
+
const actionBtn = document.getElementById('pdf-action-btn');
|
|
26
|
+
actionBtn.textContent = meta.action;
|
|
27
|
+
|
|
28
|
+
const paper = document.getElementById('pdf-paper');
|
|
29
|
+
paper.style.maxWidth = '440px';
|
|
30
|
+
paper.style.minHeight = '480px';
|
|
31
|
+
paper.style.boxShadow = '';
|
|
32
|
+
document.getElementById('pdf-extra').classList.add('hidden');
|
|
33
|
+
document.getElementById('pdf-extra').innerHTML = '';
|
|
34
|
+
document.getElementById('pdf-paper').style.background = '#fff';
|
|
35
|
+
pdfSetNav(true);
|
|
36
|
+
|
|
37
|
+
const inspector = document.getElementById('pdf-inspector');
|
|
38
|
+
document.getElementById('pdf-file-name').textContent = '';
|
|
39
|
+
document.getElementById('pdf-page-label').textContent = 'Hal 1 / 1';
|
|
40
|
+
|
|
41
|
+
// size + action per tool
|
|
42
|
+
switch (id) {
|
|
43
|
+
case 'merge': return setupMerge(inspector, actionBtn);
|
|
44
|
+
case 'split': return setupSplit(inspector, actionBtn);
|
|
45
|
+
case 'organize': return setupOrganize(inspector, actionBtn);
|
|
46
|
+
case 'compress': return setupCompress(inspector, actionBtn);
|
|
47
|
+
case 'pdf2img': return setupPdf2img(inspector, actionBtn);
|
|
48
|
+
case 'img2pdf': return setupImg2pdf(inspector, actionBtn);
|
|
49
|
+
case 'watermark': return setupWatermark(inspector, actionBtn);
|
|
50
|
+
case 'pagenum': return setupPagenum(inspector, actionBtn);
|
|
51
|
+
case 'rotate': return setupRotate(inspector, actionBtn);
|
|
52
|
+
case 'protect': return setupProtect(inspector, actionBtn);
|
|
53
|
+
case 'extract': return setupExtract(inspector, actionBtn);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
// file picker snippet for single-file tools
|
|
58
|
+
function pickerHtml(onchange) {
|
|
59
|
+
return `<div>
|
|
60
|
+
<div class="inspector-label" style="margin-bottom:8px;">Berkas PDF</div>
|
|
61
|
+
<label class="btn-outline" style="width:100%;cursor:pointer;">
|
|
62
|
+
Pilih Berkas
|
|
63
|
+
<input type="file" accept="application/pdf" class="hidden" onchange="${onchange}">
|
|
64
|
+
</label>
|
|
65
|
+
</div>`;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
async function pdfSingleFileLoad(file, afterLoad) {
|
|
69
|
+
if (!file) return;
|
|
70
|
+
try {
|
|
71
|
+
await loadPdfIntoEngine(file);
|
|
72
|
+
switchView('pdf');
|
|
73
|
+
await pdfRenderPreview();
|
|
74
|
+
if (afterLoad) afterLoad();
|
|
75
|
+
} catch (err) {
|
|
76
|
+
console.error(err);
|
|
77
|
+
showToast('Gagal membaca PDF: ' + err.message, false);
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
// ---------- MERGE ----------
|
|
82
|
+
function setupMerge(inspector, actionBtn) {
|
|
83
|
+
pdfEngine.doc = null;
|
|
84
|
+
mergeFiles = [];
|
|
85
|
+
mergeFileDocs = new Map();
|
|
86
|
+
document.getElementById('pdf-empty').classList.remove('hidden');
|
|
87
|
+
document.getElementById('pdf-preview').classList.add('hidden');
|
|
88
|
+
inspector.innerHTML = `
|
|
89
|
+
<div class="inspector-top">
|
|
90
|
+
<div>
|
|
91
|
+
<div class="inspector-label" style="margin-bottom:8px;">Berkas PDF</div>
|
|
92
|
+
<label class="btn-outline" style="width:100%;cursor:pointer;">
|
|
93
|
+
Tambah Berkas
|
|
94
|
+
<input type="file" multiple accept="application/pdf" class="hidden" onchange="addMergeFiles(this.files)">
|
|
95
|
+
</label>
|
|
96
|
+
</div>
|
|
97
|
+
<ul class="file-list" id="merge-file-list"></ul>
|
|
98
|
+
</div>
|
|
99
|
+
<div class="panel-note">Seret kartu untuk mengatur urutan sebelum digabungkan.</div>`;
|
|
100
|
+
renderMergeList();
|
|
101
|
+
actionBtn.onclick = runMerge;
|
|
102
|
+
switchView('pdf');
|
|
103
|
+
renderMergePreview();
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
function addMergeFiles(files) {
|
|
107
|
+
[...files].forEach(f => {
|
|
108
|
+
const ok = f.type === 'application/pdf' || /\.pdf$/i.test(f.name || '');
|
|
109
|
+
if (ok) mergeFiles.push(f);
|
|
110
|
+
});
|
|
111
|
+
renderMergeList();
|
|
112
|
+
renderMergePreview();
|
|
113
|
+
const listEl = document.getElementById('merge-file-list');
|
|
114
|
+
if (listEl && mergeFiles.length) showToast(`${mergeFiles.length} berkas dalam daftar.`);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function renderMergeList() {
|
|
118
|
+
const list = document.getElementById('merge-file-list');
|
|
119
|
+
if (!list) return;
|
|
120
|
+
list.innerHTML = mergeFiles.map((f, i) => `
|
|
121
|
+
<li class="file-item" draggable="true" data-idx="${i}"
|
|
122
|
+
ondragstart="mergeDragStart(${i}, event)" ondragover="mergeDragOver(event)"
|
|
123
|
+
ondrop="mergeDrop(${i}, event)" ondragend="mergeDragEnd(event)">
|
|
124
|
+
<span class="file-item-name" style="gap:6px;">
|
|
125
|
+
<span class="grab-handle">${svgIcon('organize')}</span>
|
|
126
|
+
<span>${i + 1}. ${f.name}</span>
|
|
127
|
+
</span>
|
|
128
|
+
<span style="display:flex;align-items:center;gap:8px;">
|
|
129
|
+
<span class="file-item-size">${fmtSize(f.size)}</span>
|
|
130
|
+
<button class="file-item-x" onclick="event.stopPropagation();removeMergeFile(${i})" title="Hapus">
|
|
131
|
+
<svg fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="1.8"><path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12"/></svg>
|
|
132
|
+
</button>
|
|
133
|
+
</span>
|
|
134
|
+
</li>`).join('') || '<li style="font-size:11px;color:var(--muted);padding:8px;">Belum ada berkas.</li>';
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function removeMergeFile(i) {
|
|
138
|
+
const f = mergeFiles[i];
|
|
139
|
+
if (f) mergeFileDocs.delete(f);
|
|
140
|
+
mergeFiles.splice(i, 1);
|
|
141
|
+
renderMergeList();
|
|
142
|
+
renderMergePreview();
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// --- drag reorder ---
|
|
146
|
+
function mergeDragStart(i, e) {
|
|
147
|
+
if (e && e.target && e.target.closest && e.target.closest('.merge-group-actions')) { e.preventDefault(); return; }
|
|
148
|
+
mergeDragIndex = i;
|
|
149
|
+
if (e && e.dataTransfer) { e.dataTransfer.effectAllowed = 'move'; try { e.dataTransfer.setData('text/plain', String(i)); } catch (_) {} }
|
|
150
|
+
if (e && e.currentTarget) { e.currentTarget.classList.add('dragging'); if (e.currentTarget.tagName === 'LI') e.currentTarget.style.opacity = '0.5'; }
|
|
151
|
+
}
|
|
152
|
+
function mergeDragOver(e) { if (e) { e.preventDefault(); if (e.dataTransfer) e.dataTransfer.dropEffect = 'move'; } }
|
|
153
|
+
function mergeDragEnter(e) { if (e && e.currentTarget && e.currentTarget.classList && e.currentTarget.classList.contains('merge-group')) e.currentTarget.classList.add('drag-over'); }
|
|
154
|
+
function mergeDragLeave(e) { if (e && e.currentTarget && e.currentTarget.classList && e.currentTarget.classList.contains('merge-group')) e.currentTarget.classList.remove('drag-over'); }
|
|
155
|
+
function mergeDrop(i, e) {
|
|
156
|
+
if (e) e.preventDefault();
|
|
157
|
+
let from = mergeDragIndex;
|
|
158
|
+
if (from == null && e && e.dataTransfer) from = Number(e.dataTransfer.getData('text/plain'));
|
|
159
|
+
mergeDragIndex = null;
|
|
160
|
+
if (from == null || isNaN(from) || from === i || from < 0 || from >= mergeFiles.length) { renderMergeList(); renderMergePreview(); return; }
|
|
161
|
+
const [m] = mergeFiles.splice(from, 1);
|
|
162
|
+
mergeFiles.splice(i, 0, m);
|
|
163
|
+
renderMergeList();
|
|
164
|
+
renderMergePreview();
|
|
165
|
+
}
|
|
166
|
+
function mergeDragEnd(e) {
|
|
167
|
+
mergeDragIndex = null;
|
|
168
|
+
if (e && e.currentTarget) { e.currentTarget.classList.remove('dragging'); e.currentTarget.style.opacity = '1'; }
|
|
169
|
+
document.querySelectorAll('.merge-group.drag-over').forEach(el => el.classList.remove('drag-over'));
|
|
170
|
+
}
|
|
171
|
+
function mergeMoveFile(i, dir) {
|
|
172
|
+
const j = i + dir;
|
|
173
|
+
if (j < 0 || j >= mergeFiles.length) return;
|
|
174
|
+
const [m] = mergeFiles.splice(i, 1);
|
|
175
|
+
mergeFiles.splice(j, 0, m);
|
|
176
|
+
renderMergeList();
|
|
177
|
+
renderMergePreview();
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// --- live preview of queued pages ---
|
|
181
|
+
async function renderMergePreview() {
|
|
182
|
+
const extra = document.getElementById('pdf-extra');
|
|
183
|
+
const paper = document.getElementById('pdf-paper');
|
|
184
|
+
const empty = document.getElementById('pdf-empty');
|
|
185
|
+
const single = document.getElementById('pdf-preview');
|
|
186
|
+
const cap = document.getElementById('pdf-caption');
|
|
187
|
+
if (!extra) return;
|
|
188
|
+
|
|
189
|
+
if (!mergeFiles.length) {
|
|
190
|
+
extra.classList.add('hidden');
|
|
191
|
+
extra.innerHTML = '';
|
|
192
|
+
if (empty) empty.classList.remove('hidden');
|
|
193
|
+
if (single) single.classList.add('hidden');
|
|
194
|
+
if (paper) { paper.style.maxWidth = '440px'; paper.style.minHeight = '480px'; paper.style.background = '#fff'; paper.style.boxShadow = ''; }
|
|
195
|
+
if (cap) cap.textContent = 'A4 Portrait • 210 × 297 mm';
|
|
196
|
+
pdfSetNav(true);
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
|
|
200
|
+
if (window.pdfjsLib && !pdfjsLib.GlobalWorkerOptions.workerSrc) {
|
|
201
|
+
pdfjsLib.GlobalWorkerOptions.workerSrc = './vendor/pdf.worker.min.js';
|
|
202
|
+
}
|
|
203
|
+
if (paper) { paper.style.maxWidth = '900px'; paper.style.minHeight = 'auto'; paper.style.background = 'transparent'; paper.style.boxShadow = 'none'; }
|
|
204
|
+
if (empty) empty.classList.add('hidden');
|
|
205
|
+
if (single) single.classList.add('hidden');
|
|
206
|
+
extra.classList.remove('hidden');
|
|
207
|
+
pdfSetNav(false);
|
|
208
|
+
|
|
209
|
+
extra.innerHTML = '<div class="merge-groups" id="merge-groups"></div><div class="thumb-empty hidden" id="merge-empty-state"><span class="spinner"></span> Menyiapkan pratinjau...</div>';
|
|
210
|
+
const host = document.getElementById('merge-groups');
|
|
211
|
+
const jobs = [];
|
|
212
|
+
let total = 0;
|
|
213
|
+
for (let fi = 0; fi < mergeFiles.length; fi++) {
|
|
214
|
+
const f = mergeFiles[fi];
|
|
215
|
+
let doc = mergeFileDocs.get(f);
|
|
216
|
+
if (!doc) {
|
|
217
|
+
try {
|
|
218
|
+
doc = await pdfjsLib.getDocument({ data: (await f.arrayBuffer()).slice(0) }).promise;
|
|
219
|
+
mergeFileDocs.set(f, doc);
|
|
220
|
+
} catch (err) {
|
|
221
|
+
console.warn('Pratinjau gagal:', f.name, err);
|
|
222
|
+
continue;
|
|
223
|
+
}
|
|
224
|
+
}
|
|
225
|
+
const group = document.createElement('div');
|
|
226
|
+
group.className = 'merge-group';
|
|
227
|
+
group.draggable = true;
|
|
228
|
+
group.dataset.idx = fi;
|
|
229
|
+
group.setAttribute('ondragstart', `mergeDragStart(${fi}, event)`);
|
|
230
|
+
group.setAttribute('ondragover', 'mergeDragOver(event)');
|
|
231
|
+
group.setAttribute('ondragenter', 'mergeDragEnter(event)');
|
|
232
|
+
group.setAttribute('ondragleave', 'mergeDragLeave(event)');
|
|
233
|
+
group.setAttribute('ondrop', `mergeDrop(${fi}, event)`);
|
|
234
|
+
group.setAttribute('ondragend', 'mergeDragEnd(event)');
|
|
235
|
+
group.innerHTML = `
|
|
236
|
+
<div class="merge-group-head">
|
|
237
|
+
<span class="grab-handle">${svgIcon('organize')}</span>
|
|
238
|
+
<span class="merge-group-badge">#${fi + 1}</span>
|
|
239
|
+
<span class="merge-group-name" title="${f.name}">${f.name}</span>
|
|
240
|
+
<span class="merge-group-meta">${doc.numPages} hal · ${fmtSize(f.size)}</span>
|
|
241
|
+
<span class="merge-group-actions">
|
|
242
|
+
<button type="button" onclick="event.stopPropagation();mergeMoveFile(${fi}, -1)" title="Geser ke kiri"${fi === 0 ? ' disabled' : ''}>←</button>
|
|
243
|
+
<button type="button" onclick="event.stopPropagation();mergeMoveFile(${fi}, 1)" title="Geser ke kanan"${fi === mergeFiles.length - 1 ? ' disabled' : ''}>→</button>
|
|
244
|
+
<button type="button" onclick="event.stopPropagation();removeMergeFile(${fi})" title="Hapus berkas">×</button>
|
|
245
|
+
</span>
|
|
246
|
+
</div>
|
|
247
|
+
<div class="thumb-grid"></div>`;
|
|
248
|
+
host.appendChild(group);
|
|
249
|
+
const grid = group.querySelector('.thumb-grid');
|
|
250
|
+
for (let pj = 1; pj <= doc.numPages; pj++) {
|
|
251
|
+
total++;
|
|
252
|
+
const card = document.createElement('div');
|
|
253
|
+
card.className = 'thumb-card';
|
|
254
|
+
card.innerHTML = `
|
|
255
|
+
<div class="thumb-index">${pj}</div>
|
|
256
|
+
<canvas width="120" height="160"></canvas>
|
|
257
|
+
<div class="thumb-bar">
|
|
258
|
+
<span>Hal ${pj} / ${doc.numPages}</span>
|
|
259
|
+
</div>`;
|
|
260
|
+
grid.appendChild(card);
|
|
261
|
+
jobs.push({ canvas: card.querySelector('canvas'), doc, pj });
|
|
262
|
+
}
|
|
263
|
+
}
|
|
264
|
+
|
|
265
|
+
const emptyState = document.getElementById('merge-empty-state');
|
|
266
|
+
if (emptyState) emptyState.classList.add('hidden');
|
|
267
|
+
if (!jobs.length) {
|
|
268
|
+
if (cap) cap.textContent = 'Tidak ada halaman yang bisa dipratinjau';
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
await Promise.all(jobs.map(j => renderPageToCanvas(j.doc, j.pj, j.canvas, 150).catch(() => {})));
|
|
272
|
+
if (cap) cap.textContent = `${total} halaman • ${mergeFiles.length} berkas siap digabung`;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
async function runMerge() {
|
|
276
|
+
if (mergeFiles.length < 2) return showToast('Pilih minimal dua berkas PDF.', false);
|
|
277
|
+
if (!window.PDFLib) return showToast('Pustaka pdf-lib tidak termuat.', false);
|
|
278
|
+
showToast('Menggabungkan berkas...');
|
|
279
|
+
try {
|
|
280
|
+
const { PDFDocument } = PDFLib;
|
|
281
|
+
const out = await PDFDocument.create();
|
|
282
|
+
for (const f of mergeFiles) {
|
|
283
|
+
const src = await PDFDocument.load(await f.arrayBuffer(), { ignoreEncryption: true });
|
|
284
|
+
const pages = await out.copyPages(src, src.getPageIndices());
|
|
285
|
+
pages.forEach(p => out.addPage(p));
|
|
286
|
+
}
|
|
287
|
+
downloadBlob(new Blob([await out.save()], { type: 'application/pdf' }), tsName('Gabungan', 'pdf'));
|
|
288
|
+
showToast('Dokumen PDF berhasil digabungkan.');
|
|
289
|
+
} catch (err) { console.error(err); showToast('Gagal menggabungkan: ' + err.message, false); }
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
// ---------- SPLIT ----------
|
|
293
|
+
function setupSplit(inspector, actionBtn) {
|
|
294
|
+
pdfEngine.doc = null;
|
|
295
|
+
document.getElementById('pdf-empty').classList.remove('hidden');
|
|
296
|
+
document.getElementById('pdf-preview').classList.add('hidden');
|
|
297
|
+
inspector.innerHTML = `
|
|
298
|
+
<div class="inspector-top">
|
|
299
|
+
${pickerHtml('pdfSingleFileLoad(this.files[0])')}
|
|
300
|
+
<div class="form-group">
|
|
301
|
+
<label class="form-label">Rentang Halaman</label>
|
|
302
|
+
<input type="text" class="form-input mono" id="split-range" value="1-3, 5" placeholder="Contoh: 1-3, 5, 8-10">
|
|
303
|
+
<div class="form-hint">Pisahkan rentang dengan koma. Gunakan tanda hubung untuk rentang.</div>
|
|
304
|
+
</div>
|
|
305
|
+
</div>
|
|
306
|
+
<div class="panel-note">Hasil berupa dokumen baru berisi halaman terpilih.</div>`;
|
|
307
|
+
actionBtn.onclick = runSplit;
|
|
308
|
+
switchView('pdf');
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
function parseRanges(str, max) {
|
|
312
|
+
const set = new Set();
|
|
313
|
+
str.split(',').forEach(part => {
|
|
314
|
+
part = part.trim();
|
|
315
|
+
if (!part) return;
|
|
316
|
+
const m = part.match(/^(\d+)\s*-\s*(\d+)$/);
|
|
317
|
+
if (m) {
|
|
318
|
+
let a = parseInt(m[1]), b = parseInt(m[2]);
|
|
319
|
+
if (a > b) [a, b] = [b, a];
|
|
320
|
+
for (let i = a; i <= b; i++) if (i >= 1 && i <= max) set.add(i - 1);
|
|
321
|
+
} else if (/^\d+$/.test(part)) {
|
|
322
|
+
const p = parseInt(part);
|
|
323
|
+
if (p >= 1 && p <= max) set.add(p - 1);
|
|
324
|
+
}
|
|
325
|
+
});
|
|
326
|
+
return [...set].sort((a, b) => a - b);
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
async function runSplit() {
|
|
330
|
+
if (!pdfEngine.doc) return showToast('Pilih berkas PDF terlebih dahulu.', false);
|
|
331
|
+
const idx = parseRanges(document.getElementById('split-range').value, pdfEngine.pageCount);
|
|
332
|
+
if (!idx.length) return showToast('Rentang halaman tidak valid.', false);
|
|
333
|
+
try {
|
|
334
|
+
const { PDFDocument } = PDFLib;
|
|
335
|
+
const src = await PDFDocument.load(pdfEngine.bytes.slice(0), { ignoreEncryption: true });
|
|
336
|
+
const out = await PDFDocument.create();
|
|
337
|
+
const pages = await out.copyPages(src, idx);
|
|
338
|
+
pages.forEach(p => out.addPage(p));
|
|
339
|
+
downloadBlob(new Blob([await out.save()], { type: 'application/pdf' }), tsName('Pisah', 'pdf'));
|
|
340
|
+
showToast(`${idx.length} halaman berhasil diekstrak.`);
|
|
341
|
+
} catch (err) { console.error(err); showToast('Gagal memisah: ' + err.message, false); }
|
|
342
|
+
}
|
|
343
|
+
|
|
344
|
+
// ---------- ORGANIZE ----------
|
|
345
|
+
function setupOrganize(inspector, actionBtn) {
|
|
346
|
+
pdfEngine.doc = null;
|
|
347
|
+
organizePages = [];
|
|
348
|
+
document.getElementById('pdf-empty').classList.add('hidden');
|
|
349
|
+
document.getElementById('pdf-preview').classList.add('hidden');
|
|
350
|
+
const extra = document.getElementById('pdf-extra');
|
|
351
|
+
extra.classList.add('hidden');
|
|
352
|
+
const paper = document.getElementById('pdf-paper');
|
|
353
|
+
paper.style.maxWidth = '760px';
|
|
354
|
+
paper.style.minHeight = 'auto';
|
|
355
|
+
paper.style.background = 'transparent';
|
|
356
|
+
paper.style.boxShadow = 'none';
|
|
357
|
+
inspector.innerHTML = `
|
|
358
|
+
<div class="inspector-top">
|
|
359
|
+
${pickerHtml('pdfSingleFileLoad(this.files[0], buildOrganizeThumbs)')}
|
|
360
|
+
<div class="panel-note">Seret kartu untuk mengubah urutan, ikon putar untuk memutar 90°, ikon silang untuk menandai hapus.</div>
|
|
361
|
+
</div>`;
|
|
362
|
+
actionBtn.onclick = runOrganize;
|
|
363
|
+
switchView('pdf');
|
|
364
|
+
}
|
|
365
|
+
|
|
366
|
+
async function buildOrganizeThumbs() {
|
|
367
|
+
organizePages = Array.from({ length: pdfEngine.pageCount }, (_, i) => ({ page: i + 1, rotate: 0, deleted: false }));
|
|
368
|
+
await renderOrganizeGrid();
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
async function renderOrganizeGrid() {
|
|
372
|
+
const extra = document.getElementById('pdf-extra');
|
|
373
|
+
paper_showExtra();
|
|
374
|
+
extra.innerHTML = `<div class="thumb-grid" id="organize-grid"></div>`;
|
|
375
|
+
const grid = document.getElementById('organize-grid');
|
|
376
|
+
grid.innerHTML = organizePages.map((p, i) => `
|
|
377
|
+
<div class="thumb-card organize-card" id="thumb-${i}" draggable="true" data-idx="${i}"
|
|
378
|
+
ondragstart="organizeDragStart(${i}, event)" ondragover="organizeDragOver(event)"
|
|
379
|
+
ondragenter="organizeDragEnter(event)" ondragleave="organizeDragLeave(event)"
|
|
380
|
+
ondrop="organizeDrop(${i}, event)" ondragend="organizeDragEnd(event)"
|
|
381
|
+
style="opacity:${p.deleted ? '0.35' : '1'};cursor:grab;">
|
|
382
|
+
<div class="thumb-index">${i + 1}</div>
|
|
383
|
+
<canvas width="120" height="160"></canvas>
|
|
384
|
+
<div class="thumb-bar">
|
|
385
|
+
<span>Hal ${p.page}</span>
|
|
386
|
+
<span style="display:flex;gap:6px;">
|
|
387
|
+
<button onclick="event.stopPropagation();organizeMove(${i}, -1)" title="Geser ke kiri">←</button>
|
|
388
|
+
<button onclick="event.stopPropagation();organizeMove(${i}, 1)" title="Geser ke kanan">→</button>
|
|
389
|
+
<button onclick="event.stopPropagation();rotateThumb(${i})" title="Putar">${svgIcon('rotate')}</button>
|
|
390
|
+
<button onclick="event.stopPropagation();toggleThumb(${i})" title="Hapus">${svgIcon('split')}</button>
|
|
391
|
+
</span>
|
|
392
|
+
</div>
|
|
393
|
+
</div>`).join('');
|
|
394
|
+
for (let i = 0; i < organizePages.length; i++) {
|
|
395
|
+
const canvas = grid.querySelector(`#thumb-${i} canvas`);
|
|
396
|
+
await renderPageToCanvas(pdfEngine.doc, organizePages[i].page, canvas, 150);
|
|
397
|
+
if (organizePages[i].rotate) canvas.style.transform = `rotate(${organizePages[i].rotate}deg)`;
|
|
398
|
+
}
|
|
399
|
+
document.getElementById('pdf-caption').textContent = `${pdfEngine.pageCount} Halaman • ${pdfEngine.name}`;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
function organizeDragStart(i, e) {
|
|
403
|
+
if (e && e.target && e.target.closest && e.target.closest('.thumb-bar')) { e.preventDefault(); return; }
|
|
404
|
+
organizeDragIndex = i;
|
|
405
|
+
if (e && e.dataTransfer) { e.dataTransfer.effectAllowed = 'move'; try { e.dataTransfer.setData('text/plain', String(i)); } catch (_) {} }
|
|
406
|
+
if (e && e.currentTarget) e.currentTarget.style.opacity = '0.5';
|
|
407
|
+
}
|
|
408
|
+
function organizeDragOver(e) { if (e) { e.preventDefault(); if (e.dataTransfer) e.dataTransfer.dropEffect = 'move'; } }
|
|
409
|
+
function organizeDragEnter(e) { if (e && e.currentTarget) e.currentTarget.style.borderColor = '#4F46E5'; }
|
|
410
|
+
function organizeDragLeave(e) { if (e && e.currentTarget) e.currentTarget.style.borderColor = ''; }
|
|
411
|
+
function organizeDrop(i, e) {
|
|
412
|
+
if (e) e.preventDefault();
|
|
413
|
+
let from = organizeDragIndex;
|
|
414
|
+
if (from == null && e && e.dataTransfer) from = Number(e.dataTransfer.getData('text/plain'));
|
|
415
|
+
organizeDragIndex = null;
|
|
416
|
+
if (from == null || isNaN(from) || from === i) { renderOrganizeGrid(); return; }
|
|
417
|
+
const [m] = organizePages.splice(from, 1);
|
|
418
|
+
organizePages.splice(i, 0, m);
|
|
419
|
+
renderOrganizeGrid();
|
|
420
|
+
}
|
|
421
|
+
function organizeDragEnd(e) {
|
|
422
|
+
organizeDragIndex = null;
|
|
423
|
+
if (e && e.currentTarget) { e.currentTarget.style.opacity = '1'; e.currentTarget.style.borderColor = ''; }
|
|
424
|
+
}
|
|
425
|
+
function organizeMove(i, dir) {
|
|
426
|
+
const j = i + dir;
|
|
427
|
+
if (j < 0 || j >= organizePages.length) return;
|
|
428
|
+
const [m] = organizePages.splice(i, 1);
|
|
429
|
+
organizePages.splice(j, 0, m);
|
|
430
|
+
renderOrganizeGrid();
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
function paper_showExtra() {
|
|
434
|
+
document.getElementById('pdf-extra').classList.remove('hidden');
|
|
435
|
+
document.getElementById('pdf-empty').classList.add('hidden');
|
|
436
|
+
document.getElementById('pdf-preview').classList.add('hidden');
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
function rotateThumb(i) {
|
|
440
|
+
const canvas = document.querySelector(`#thumb-${i} canvas`);
|
|
441
|
+
organizePages[i].rotate = (organizePages[i].rotate + 90) % 360;
|
|
442
|
+
canvas.style.transform = `rotate(${organizePages[i].rotate}deg)`;
|
|
443
|
+
}
|
|
444
|
+
|
|
445
|
+
function toggleThumb(i) {
|
|
446
|
+
organizePages[i].deleted = !organizePages[i].deleted;
|
|
447
|
+
const card = document.getElementById('thumb-' + i);
|
|
448
|
+
card.style.opacity = organizePages[i].deleted ? '0.35' : '1';
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
async function runOrganize() {
|
|
452
|
+
if (!pdfEngine.doc) return showToast('Pilih berkas PDF terlebih dahulu.', false);
|
|
453
|
+
const active = organizePages.filter(p => !p.deleted);
|
|
454
|
+
if (!active.length) return showToast('Minimal satu halaman harus tersisa.', false);
|
|
455
|
+
try {
|
|
456
|
+
const { PDFDocument, degrees } = PDFLib;
|
|
457
|
+
const src = await PDFDocument.load(pdfEngine.bytes.slice(0), { ignoreEncryption: true });
|
|
458
|
+
const out = await PDFDocument.create();
|
|
459
|
+
const copied = await out.copyPages(src, active.map(p => p.page - 1));
|
|
460
|
+
copied.forEach((pg, i) => {
|
|
461
|
+
const add = active[i].rotate;
|
|
462
|
+
if (add) pg.setRotation(degrees((pg.getRotation().angle + add) % 360));
|
|
463
|
+
out.addPage(pg);
|
|
464
|
+
});
|
|
465
|
+
downloadBlob(new Blob([await out.save()], { type: 'application/pdf' }), tsName('Atur_Halaman', 'pdf'));
|
|
466
|
+
showToast(`${active.length} halaman disimpan.`);
|
|
467
|
+
} catch (err) { console.error(err); showToast('Gagal menyusun: ' + err.message, false); }
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
// ---------- COMPRESS ----------
|
|
471
|
+
function setupCompress(inspector, actionBtn) {
|
|
472
|
+
pdfEngine.doc = null;
|
|
473
|
+
document.getElementById('pdf-empty').classList.remove('hidden');
|
|
474
|
+
document.getElementById('pdf-preview').classList.add('hidden');
|
|
475
|
+
inspector.innerHTML = `
|
|
476
|
+
<div class="inspector-top">
|
|
477
|
+
${pickerHtml('pdfSingleFileLoad(this.files[0])')}
|
|
478
|
+
<div class="form-group">
|
|
479
|
+
<label class="form-label">Tingkat Kompresi</label>
|
|
480
|
+
<select class="form-input" id="compress-level">
|
|
481
|
+
<option value="light">Ringan — kualitas tinggi</option>
|
|
482
|
+
<option value="medium" selected>Sedang — seimbang</option>
|
|
483
|
+
<option value="extreme">Ekstrem — ukuran terkecil</option>
|
|
484
|
+
</select>
|
|
485
|
+
</div>
|
|
486
|
+
</div>
|
|
487
|
+
<div class="panel-note">Halaman akan dirasterisasi agar ukuran berkas turun signifikan. Teks tidak lagi dapat diseleksi.</div>`;
|
|
488
|
+
actionBtn.onclick = runCompress;
|
|
489
|
+
switchView('pdf');
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
const COMPRESS_PRESETS = {
|
|
493
|
+
light: { quality: 0.85, scale: 1.6 },
|
|
494
|
+
medium: { quality: 0.6, scale: 1.2 },
|
|
495
|
+
extreme: { quality: 0.4, scale: 0.9 },
|
|
496
|
+
};
|
|
497
|
+
|
|
498
|
+
async function runCompress() {
|
|
499
|
+
if (!pdfEngine.doc) return showToast('Pilih berkas PDF terlebih dahulu.', false);
|
|
500
|
+
const preset = COMPRESS_PRESETS[document.getElementById('compress-level').value] || COMPRESS_PRESETS.medium;
|
|
501
|
+
showToast('Mengompres dokumen...');
|
|
502
|
+
try {
|
|
503
|
+
const { PDFDocument } = PDFLib;
|
|
504
|
+
const out = await PDFDocument.create();
|
|
505
|
+
for (let i = 1; i <= pdfEngine.pageCount; i++) {
|
|
506
|
+
const page = await pdfEngine.doc.getPage(i);
|
|
507
|
+
const vp = page.getViewport({ scale: preset.scale });
|
|
508
|
+
const canvas = document.createElement('canvas');
|
|
509
|
+
canvas.width = vp.width; canvas.height = vp.height;
|
|
510
|
+
await page.render({ canvasContext: canvas.getContext('2d'), viewport: vp }).promise;
|
|
511
|
+
const jpg = await out.embedJpg(canvas.toDataURL('image/jpeg', preset.quality));
|
|
512
|
+
const p = out.addPage([vp.width, vp.height]);
|
|
513
|
+
p.drawImage(jpg, { x: 0, y: 0, width: vp.width, height: vp.height });
|
|
514
|
+
}
|
|
515
|
+
const bytes = await out.save();
|
|
516
|
+
downloadBlob(new Blob([bytes], { type: 'application/pdf' }), tsName('Kompres', 'pdf'));
|
|
517
|
+
showToast(`Selesai. Ukuran baru: ${fmtSize(bytes.length)}.`);
|
|
518
|
+
} catch (err) { console.error(err); showToast('Gagal mengompres: ' + err.message, false); }
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
// ---------- PDF TO IMAGE ----------
|
|
522
|
+
function setupPdf2img(inspector, actionBtn) {
|
|
523
|
+
pdfEngine.doc = null;
|
|
524
|
+
document.getElementById('pdf-empty').classList.remove('hidden');
|
|
525
|
+
document.getElementById('pdf-preview').classList.add('hidden');
|
|
526
|
+
inspector.innerHTML = `
|
|
527
|
+
<div class="inspector-top">
|
|
528
|
+
${pickerHtml('pdfSingleFileLoad(this.files[0])')}
|
|
529
|
+
<div class="form-group">
|
|
530
|
+
<label class="form-label">Format</label>
|
|
531
|
+
<select class="form-input" id="p2i-format">
|
|
532
|
+
<option value="image/png">PNG (tajam)</option>
|
|
533
|
+
<option value="image/jpeg">JPG (kecil)</option>
|
|
534
|
+
</select>
|
|
535
|
+
</div>
|
|
536
|
+
<div class="form-group">
|
|
537
|
+
<label class="form-label">Resolusi (DPI)</label>
|
|
538
|
+
<select class="form-input" id="p2i-dpi">
|
|
539
|
+
<option value="96">96 DPI</option>
|
|
540
|
+
<option value="150" selected>150 DPI</option>
|
|
541
|
+
<option value="300">300 DPI</option>
|
|
542
|
+
</select>
|
|
543
|
+
</div>
|
|
544
|
+
</div>
|
|
545
|
+
<div class="panel-note">Semua halaman diunduh sebagai satu berkas ZIP.</div>`;
|
|
546
|
+
actionBtn.onclick = runPdf2img;
|
|
547
|
+
switchView('pdf');
|
|
548
|
+
}
|
|
549
|
+
|
|
550
|
+
async function runPdf2img() {
|
|
551
|
+
if (!pdfEngine.doc) return showToast('Pilih berkas PDF terlebih dahulu.', false);
|
|
552
|
+
if (!window.JSZip) return showToast('Pustaka ZIP tidak termuat.', false);
|
|
553
|
+
const format = document.getElementById('p2i-format').value;
|
|
554
|
+
const dpi = parseInt(document.getElementById('p2i-dpi').value);
|
|
555
|
+
const scale = dpi / 72;
|
|
556
|
+
const ext = format === 'image/png' ? 'png' : 'jpg';
|
|
557
|
+
showToast('Mengekstrak gambar...');
|
|
558
|
+
try {
|
|
559
|
+
const zip = new JSZip();
|
|
560
|
+
const base = pdfEngine.name.replace(/\.[^/.]+$/, '');
|
|
561
|
+
for (let i = 1; i <= pdfEngine.pageCount; i++) {
|
|
562
|
+
const page = await pdfEngine.doc.getPage(i);
|
|
563
|
+
const vp = page.getViewport({ scale });
|
|
564
|
+
const canvas = document.createElement('canvas');
|
|
565
|
+
canvas.width = vp.width; canvas.height = vp.height;
|
|
566
|
+
await page.render({ canvasContext: canvas.getContext('2d'), viewport: vp }).promise;
|
|
567
|
+
const dataUrl = canvas.toDataURL(format, 0.92);
|
|
568
|
+
zip.file(`${base}_hal_${String(i).padStart(3, '0')}.${ext}`, dataUrl.split(',')[1], { base64: true });
|
|
569
|
+
}
|
|
570
|
+
const blob = await zip.generateAsync({ type: 'blob' });
|
|
571
|
+
downloadBlob(blob, tsName('Gambar', 'zip'));
|
|
572
|
+
showToast(`${pdfEngine.pageCount} gambar berhasil diekstrak.`);
|
|
573
|
+
} catch (err) { console.error(err); showToast('Gagal mengekstrak: ' + err.message, false); }
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
// ---------- IMAGE TO PDF ----------
|
|
577
|
+
function setupImg2pdf(inspector, actionBtn) {
|
|
578
|
+
pdfEngine.doc = null;
|
|
579
|
+
imageFiles = [];
|
|
580
|
+
document.getElementById('pdf-empty').classList.add('hidden');
|
|
581
|
+
document.getElementById('pdf-preview').classList.add('hidden');
|
|
582
|
+
const paper = document.getElementById('pdf-paper');
|
|
583
|
+
paper.style.maxWidth = '760px';
|
|
584
|
+
paper.style.minHeight = 'auto';
|
|
585
|
+
paper.style.background = 'transparent';
|
|
586
|
+
paper.style.boxShadow = 'none';
|
|
587
|
+
inspector.innerHTML = `
|
|
588
|
+
<div class="inspector-top">
|
|
589
|
+
<div>
|
|
590
|
+
<div class="inspector-label" style="margin-bottom:8px;">Gambar</div>
|
|
591
|
+
<label class="btn-outline" style="width:100%;cursor:pointer;">
|
|
592
|
+
Tambah Gambar
|
|
593
|
+
<input type="file" multiple accept="image/*" class="hidden" onchange="addImageFiles(this.files)">
|
|
594
|
+
</label>
|
|
595
|
+
</div>
|
|
596
|
+
<div class="form-group">
|
|
597
|
+
<label class="form-label">Ukuran Halaman</label>
|
|
598
|
+
<select class="form-input" id="i2p-size">
|
|
599
|
+
<option value="fit" selected>Sesuai gambar</option>
|
|
600
|
+
<option value="a4">A4 (210 × 297 mm)</option>
|
|
601
|
+
<option value="letter">Letter</option>
|
|
602
|
+
</select>
|
|
603
|
+
</div>
|
|
604
|
+
<ul class="file-list" id="img2pdf-list"></ul>
|
|
605
|
+
</div>`;
|
|
606
|
+
actionBtn.onclick = runImg2pdf;
|
|
607
|
+
renderImg2pdfList();
|
|
608
|
+
switchView('pdf');
|
|
609
|
+
}
|
|
610
|
+
|
|
611
|
+
function addImageFiles(files) {
|
|
612
|
+
[...files].forEach(f => { if (f.type.startsWith('image/')) imageFiles.push(f); });
|
|
613
|
+
renderImg2pdfList();
|
|
614
|
+
renderImg2pdfThumbs();
|
|
615
|
+
}
|
|
616
|
+
|
|
617
|
+
function renderImg2pdfList() {
|
|
618
|
+
const list = document.getElementById('img2pdf-list');
|
|
619
|
+
if (!list) return;
|
|
620
|
+
list.innerHTML = imageFiles.map((f, i) => `
|
|
621
|
+
<li class="file-item">
|
|
622
|
+
<span class="file-item-name"><span>${i + 1}. ${f.name}</span></span>
|
|
623
|
+
<span style="display:flex;align-items:center;gap:8px;">
|
|
624
|
+
<span class="file-item-size">${fmtSize(f.size)}</span>
|
|
625
|
+
<button class="file-item-x" onclick="removeImageFile(${i})">
|
|
626
|
+
<svg fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="1.8"><path stroke-linecap="round" stroke-linejoin="round" d="M6 18L18 6M6 6l12 12"/></svg>
|
|
627
|
+
</button>
|
|
628
|
+
</span>
|
|
629
|
+
</li>`).join('') || '<li style="font-size:11px;color:var(--muted);padding:8px;">Belum ada gambar.</li>';
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
function removeImageFile(i) { imageFiles.splice(i, 1); renderImg2pdfList(); renderImg2pdfThumbs(); }
|
|
633
|
+
|
|
634
|
+
async function renderImg2pdfThumbs() {
|
|
635
|
+
const extra = document.getElementById('pdf-extra');
|
|
636
|
+
if (!imageFiles.length) { extra.classList.add('hidden'); return; }
|
|
637
|
+
paper_showExtra();
|
|
638
|
+
extra.innerHTML = `<div class="thumb-grid" id="i2p-grid"></div>`;
|
|
639
|
+
const grid = document.getElementById('i2p-grid');
|
|
640
|
+
for (const f of imageFiles) {
|
|
641
|
+
const url = URL.createObjectURL(f);
|
|
642
|
+
const card = document.createElement('div');
|
|
643
|
+
card.className = 'thumb-card';
|
|
644
|
+
card.innerHTML = `<img src="${url}" style="width:100%;display:block;background:#fff;">`;
|
|
645
|
+
grid.appendChild(card);
|
|
646
|
+
}
|
|
647
|
+
document.getElementById('pdf-caption').textContent = `${imageFiles.length} gambar siap digabung`;
|
|
648
|
+
}
|
|
649
|
+
|
|
650
|
+
async function runImg2pdf() {
|
|
651
|
+
if (!imageFiles.length) return showToast('Tambah minimal satu gambar.', false);
|
|
652
|
+
const sizeMode = document.getElementById('i2p-size').value;
|
|
653
|
+
const PAGE = { a4: [595.28, 841.89], letter: [612, 792] };
|
|
654
|
+
try {
|
|
655
|
+
const { PDFDocument } = PDFLib;
|
|
656
|
+
const out = await PDFDocument.create();
|
|
657
|
+
for (const f of imageFiles) {
|
|
658
|
+
const buf = new Uint8Array(await f.arrayBuffer());
|
|
659
|
+
let img;
|
|
660
|
+
try { img = f.type === 'image/png' ? await out.embedPng(buf) : await out.embedJpg(buf); }
|
|
661
|
+
catch { img = await out.embedPng(await shrinkToPng(f)); }
|
|
662
|
+
const iw = img.width, ih = img.height;
|
|
663
|
+
if (sizeMode === 'fit') {
|
|
664
|
+
const p = out.addPage([iw, ih]);
|
|
665
|
+
p.drawImage(img, { x: 0, y: 0, width: iw, height: ih });
|
|
666
|
+
} else {
|
|
667
|
+
const [pw, ph] = PAGE[sizeMode];
|
|
668
|
+
const p = out.addPage([pw, ph]);
|
|
669
|
+
const margin = 24;
|
|
670
|
+
const s = Math.min((pw - margin * 2) / iw, (ph - margin * 2) / ih);
|
|
671
|
+
const w = iw * s, h = ih * s;
|
|
672
|
+
p.drawImage(img, { x: (pw - w) / 2, y: (ph - h) / 2, width: w, height: h });
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
downloadBlob(new Blob([await out.save()], { type: 'application/pdf' }), tsName('Gambar_ke_PDF', 'pdf'));
|
|
676
|
+
showToast('PDF berhasil dibuat dari gambar.');
|
|
677
|
+
} catch (err) { console.error(err); showToast('Gagal membuat PDF: ' + err.message, false); }
|
|
678
|
+
}
|
|
679
|
+
|
|
680
|
+
function shrinkToPng(file) {
|
|
681
|
+
return new Promise((resolve) => {
|
|
682
|
+
const img = new Image();
|
|
683
|
+
img.onload = () => {
|
|
684
|
+
const c = document.createElement('canvas');
|
|
685
|
+
c.width = img.width; c.height = img.height;
|
|
686
|
+
c.getContext('2d').drawImage(img, 0, 0);
|
|
687
|
+
c.toBlob(b => b.arrayBuffer().then(resolve), 'image/png');
|
|
688
|
+
};
|
|
689
|
+
img.src = URL.createObjectURL(file);
|
|
690
|
+
});
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
// ---------- WATERMARK ----------
|
|
694
|
+
function setupWatermark(inspector, actionBtn) {
|
|
695
|
+
pdfEngine.doc = null;
|
|
696
|
+
document.getElementById('pdf-empty').classList.remove('hidden');
|
|
697
|
+
document.getElementById('pdf-preview').classList.add('hidden');
|
|
698
|
+
inspector.innerHTML = `
|
|
699
|
+
<div class="inspector-top">
|
|
700
|
+
${pickerHtml('pdfSingleFileLoad(this.files[0])')}
|
|
701
|
+
<div class="form-group">
|
|
702
|
+
<label class="form-label">Teks Watermark</label>
|
|
703
|
+
<input type="text" class="form-input" id="wm-text" value="RAHASIA">
|
|
704
|
+
</div>
|
|
705
|
+
<div class="form-group">
|
|
706
|
+
<label class="form-label">Transparansi</label>
|
|
707
|
+
<input type="range" id="wm-opacity" min="0.05" max="0.6" step="0.05" value="0.15">
|
|
708
|
+
</div>
|
|
709
|
+
<div class="form-group">
|
|
710
|
+
<label class="form-label">Kemiringan</label>
|
|
711
|
+
<select class="form-input" id="wm-angle">
|
|
712
|
+
<option value="0">0° (horizontal)</option>
|
|
713
|
+
<option value="45" selected>45°</option>
|
|
714
|
+
<option value="-45">-45°</option>
|
|
715
|
+
</select>
|
|
716
|
+
</div>
|
|
717
|
+
</div>
|
|
718
|
+
<div class="panel-note">Cap air diterapkan ke seluruh halaman dengan warna abu dan transparansi.</div>`;
|
|
719
|
+
actionBtn.onclick = runWatermark;
|
|
720
|
+
switchView('pdf');
|
|
721
|
+
}
|
|
722
|
+
|
|
723
|
+
async function runWatermark() {
|
|
724
|
+
if (!pdfEngine.doc) return showToast('Pilih berkas PDF terlebih dahulu.', false);
|
|
725
|
+
const text = document.getElementById('wm-text').value || 'RAHASIA';
|
|
726
|
+
const opacity = parseFloat(document.getElementById('wm-opacity').value);
|
|
727
|
+
const angle = parseInt(document.getElementById('wm-angle').value);
|
|
728
|
+
try {
|
|
729
|
+
const { PDFDocument, StandardFonts, rgb, degrees } = PDFLib;
|
|
730
|
+
const doc = await PDFDocument.load(pdfEngine.bytes.slice(0), { ignoreEncryption: true });
|
|
731
|
+
const font = await doc.embedFont(StandardFonts.HelveticaBold);
|
|
732
|
+
doc.getPages().forEach(page => {
|
|
733
|
+
const { width, height } = page.getSize();
|
|
734
|
+
const size = Math.min(width, height) / 6;
|
|
735
|
+
const tw = font.widthOfTextAtSize(text, size);
|
|
736
|
+
page.drawText(text, {
|
|
737
|
+
x: width / 2 - (Math.cos(angle * Math.PI / 180) * tw) / 2,
|
|
738
|
+
y: height / 2 - size / 2,
|
|
739
|
+
size, font, color: rgb(0.4, 0.4, 0.4), opacity, rotate: degrees(angle),
|
|
740
|
+
});
|
|
741
|
+
});
|
|
742
|
+
downloadBlob(new Blob([await doc.save()], { type: 'application/pdf' }), tsName('Watermark', 'pdf'));
|
|
743
|
+
showToast('Watermark berhasil diterapkan.');
|
|
744
|
+
} catch (err) { console.error(err); showToast('Gagal memberi watermark: ' + err.message, false); }
|
|
745
|
+
}
|
|
746
|
+
|
|
747
|
+
// ---------- PAGE NUMBER ----------
|
|
748
|
+
function setupPagenum(inspector, actionBtn) {
|
|
749
|
+
pdfEngine.doc = null;
|
|
750
|
+
document.getElementById('pdf-empty').classList.remove('hidden');
|
|
751
|
+
document.getElementById('pdf-preview').classList.add('hidden');
|
|
752
|
+
inspector.innerHTML = `
|
|
753
|
+
<div class="inspector-top">
|
|
754
|
+
${pickerHtml('pdfSingleFileLoad(this.files[0])')}
|
|
755
|
+
<div class="form-group">
|
|
756
|
+
<label class="form-label">Format</label>
|
|
757
|
+
<select class="form-input" id="pn-format">
|
|
758
|
+
<option value="Halaman X dari Y" selected>Halaman X dari Y</option>
|
|
759
|
+
<option value="X">X</option>
|
|
760
|
+
<option value="X / Y">X / Y</option>
|
|
761
|
+
</select>
|
|
762
|
+
</div>
|
|
763
|
+
<div class="form-group">
|
|
764
|
+
<label class="form-label">Posisi</label>
|
|
765
|
+
<select class="form-input" id="pn-pos">
|
|
766
|
+
<option value="bottom-center" selected>Tengah bawah</option>
|
|
767
|
+
<option value="bottom-right">Kanan bawah</option>
|
|
768
|
+
<option value="bottom-left">Kiri bawah</option>
|
|
769
|
+
<option value="top-center">Tengah atas</option>
|
|
770
|
+
</select>
|
|
771
|
+
</div>
|
|
772
|
+
<div class="form-group">
|
|
773
|
+
<label class="form-label">Mulai Dari Nomor</label>
|
|
774
|
+
<input type="number" class="form-input mono" id="pn-start" value="1" min="1">
|
|
775
|
+
</div>
|
|
776
|
+
</div>
|
|
777
|
+
<div class="panel-note">Nomor halaman ditambahkan sebagai teks pada setiap lembar.</div>`;
|
|
778
|
+
actionBtn.onclick = runPagenum;
|
|
779
|
+
switchView('pdf');
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
async function runPagenum() {
|
|
783
|
+
if (!pdfEngine.doc) return showToast('Pilih berkas PDF terlebih dahulu.', false);
|
|
784
|
+
const fmt = document.getElementById('pn-format').value;
|
|
785
|
+
const pos = document.getElementById('pn-pos').value;
|
|
786
|
+
const start = parseInt(document.getElementById('pn-start').value) || 1;
|
|
787
|
+
try {
|
|
788
|
+
const { PDFDocument, StandardFonts, rgb } = PDFLib;
|
|
789
|
+
const doc = await PDFDocument.load(pdfEngine.bytes.slice(0), { ignoreEncryption: true });
|
|
790
|
+
const font = await doc.embedFont(StandardFonts.Helvetica);
|
|
791
|
+
const pages = doc.getPages();
|
|
792
|
+
const total = pages.length;
|
|
793
|
+
pages.forEach((page, i) => {
|
|
794
|
+
const { width, height } = page.getSize();
|
|
795
|
+
const n = start + i;
|
|
796
|
+
const label = fmt.replace('X', String(n)).replace('Y', String(start + total - 1));
|
|
797
|
+
const size = 10;
|
|
798
|
+
const tw = font.widthOfTextAtSize(label, size);
|
|
799
|
+
let x = width / 2 - tw / 2, y = 28;
|
|
800
|
+
if (pos === 'bottom-right') { x = width - tw - 40; y = 28; }
|
|
801
|
+
if (pos === 'bottom-left') { x = 40; y = 28; }
|
|
802
|
+
if (pos === 'top-center') { x = width / 2 - tw / 2; y = height - 28; }
|
|
803
|
+
page.drawText(label, { x, y, size, font, color: rgb(0.3, 0.3, 0.3) });
|
|
804
|
+
});
|
|
805
|
+
downloadBlob(new Blob([await doc.save()], { type: 'application/pdf' }), tsName('Nomor_Halaman', 'pdf'));
|
|
806
|
+
showToast('Nomor halaman berhasil ditambahkan.');
|
|
807
|
+
} catch (err) { console.error(err); showToast('Gagal menambah nomor: ' + err.message, false); }
|
|
808
|
+
}
|
|
809
|
+
|
|
810
|
+
// ---------- ROTATE ----------
|
|
811
|
+
function setupRotate(inspector, actionBtn) {
|
|
812
|
+
pdfEngine.doc = null;
|
|
813
|
+
document.getElementById('pdf-empty').classList.remove('hidden');
|
|
814
|
+
document.getElementById('pdf-preview').classList.add('hidden');
|
|
815
|
+
inspector.innerHTML = `
|
|
816
|
+
<div class="inspector-top">
|
|
817
|
+
${pickerHtml('pdfSingleFileLoad(this.files[0])')}
|
|
818
|
+
<div class="form-group">
|
|
819
|
+
<label class="form-label">Sudut Putar</label>
|
|
820
|
+
<div class="chip-row" id="rotate-angle">
|
|
821
|
+
<button class="chip" data-a="90">90°</button>
|
|
822
|
+
<button class="chip" data-a="180">180°</button>
|
|
823
|
+
<button class="chip" data-a="270">270°</button>
|
|
824
|
+
</div>
|
|
825
|
+
</div>
|
|
826
|
+
<div class="form-group">
|
|
827
|
+
<label class="form-label">Terapkan Ke</label>
|
|
828
|
+
<select class="form-input" id="rotate-scope">
|
|
829
|
+
<option value="all" selected>Semua halaman</option>
|
|
830
|
+
<option value="current">Halaman saat ini</option>
|
|
831
|
+
</select>
|
|
832
|
+
</div>
|
|
833
|
+
</div>
|
|
834
|
+
<div class="panel-note">Putaran ditambahkan di atas orientasi halaman yang ada.</div>`;
|
|
835
|
+
const row = document.getElementById('rotate-angle');
|
|
836
|
+
row.querySelector('.chip').style.borderColor = 'var(--obsidian)';
|
|
837
|
+
row.querySelectorAll('.chip').forEach(btn => btn.onclick = () => {
|
|
838
|
+
row.querySelectorAll('.chip').forEach(b => b.style.borderColor = 'var(--border-2)');
|
|
839
|
+
btn.style.borderColor = 'var(--obsidian)';
|
|
840
|
+
row.dataset.angle = btn.dataset.a;
|
|
841
|
+
});
|
|
842
|
+
row.dataset.angle = '90';
|
|
843
|
+
actionBtn.onclick = runRotate;
|
|
844
|
+
switchView('pdf');
|
|
845
|
+
}
|
|
846
|
+
|
|
847
|
+
async function runRotate() {
|
|
848
|
+
if (!pdfEngine.doc) return showToast('Pilih berkas PDF terlebih dahulu.', false);
|
|
849
|
+
const angle = parseInt(document.getElementById('rotate-angle').dataset.angle || '90');
|
|
850
|
+
const scope = document.getElementById('rotate-scope').value;
|
|
851
|
+
try {
|
|
852
|
+
const { PDFDocument, degrees } = PDFLib;
|
|
853
|
+
const doc = await PDFDocument.load(pdfEngine.bytes.slice(0), { ignoreEncryption: true });
|
|
854
|
+
const pages = doc.getPages();
|
|
855
|
+
const targets = scope === 'current' ? [pages[pdfEngine.page - 1]] : pages;
|
|
856
|
+
targets.forEach(p => p.setRotation(degrees((p.getRotation().angle + angle) % 360)));
|
|
857
|
+
downloadBlob(new Blob([await doc.save()], { type: 'application/pdf' }), tsName('Putar', 'pdf'));
|
|
858
|
+
showToast('Halaman berhasil diputar.');
|
|
859
|
+
} catch (err) { console.error(err); showToast('Gagal memutar: ' + err.message, false); }
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
// ---------- PROTECT ----------
|
|
863
|
+
function setupProtect(inspector, actionBtn) {
|
|
864
|
+
pdfEngine.doc = null;
|
|
865
|
+
document.getElementById('pdf-empty').classList.remove('hidden');
|
|
866
|
+
document.getElementById('pdf-preview').classList.add('hidden');
|
|
867
|
+
inspector.innerHTML = `
|
|
868
|
+
<div class="inspector-top">
|
|
869
|
+
${pickerHtml('pdfSingleFileLoad(this.files[0])')}
|
|
870
|
+
<div class="form-group">
|
|
871
|
+
<label class="form-label">Kata Sandi</label>
|
|
872
|
+
<input type="password" class="form-input mono" id="prot-pass" placeholder="Kata sandi pembuka">
|
|
873
|
+
</div>
|
|
874
|
+
<div class="form-group">
|
|
875
|
+
<label class="form-label">Ulangi Kata Sandi</label>
|
|
876
|
+
<input type="password" class="form-input mono" id="prot-pass2" placeholder="Ulangi kata sandi">
|
|
877
|
+
</div>
|
|
878
|
+
</div>
|
|
879
|
+
<div class="panel-note">Enkripsi standar memerlukan pustaka tambahan. Tombol akan menyimpan dokumen dengan proteksi metadata dan memberi catatan jika enkripsi penuh tidak tersedia.</div>`;
|
|
880
|
+
actionBtn.onclick = runProtect;
|
|
881
|
+
switchView('pdf');
|
|
882
|
+
}
|
|
883
|
+
|
|
884
|
+
async function runProtect() {
|
|
885
|
+
if (!pdfEngine.doc) return showToast('Pilih berkas PDF terlebih dahulu.', false);
|
|
886
|
+
const p1 = document.getElementById('prot-pass').value;
|
|
887
|
+
const p2 = document.getElementById('prot-pass2').value;
|
|
888
|
+
if (!p1) return showToast('Kata sandi tidak boleh kosong.', false);
|
|
889
|
+
if (p1 !== p2) return showToast('Kata sandi tidak sama.', false);
|
|
890
|
+
try {
|
|
891
|
+
const { PDFDocument } = PDFLib;
|
|
892
|
+
const doc = await PDFDocument.load(pdfEngine.bytes.slice(0), { ignoreEncryption: true });
|
|
893
|
+
doc.setTitle(doc.getTitle() ? doc.getTitle() : pdfEngine.name);
|
|
894
|
+
doc.setProducer('Foliokit');
|
|
895
|
+
const bytes = await doc.save();
|
|
896
|
+
downloadBlob(new Blob([bytes], { type: 'application/pdf' }), tsName('Dokumen', 'pdf'));
|
|
897
|
+
showToast('Dokumen disimpan. Catatan: enkripsi sandi penuh belum aktif di pustaka ini.', false);
|
|
898
|
+
} catch (err) { console.error(err); showToast('Gagal memproses: ' + err.message, false); }
|
|
899
|
+
}
|
|
900
|
+
|
|
901
|
+
// ---------- EXTRACT TEXT ----------
|
|
902
|
+
function setupExtract(inspector, actionBtn) {
|
|
903
|
+
pdfEngine.doc = null;
|
|
904
|
+
document.getElementById('pdf-empty').classList.add('hidden');
|
|
905
|
+
document.getElementById('pdf-preview').classList.add('hidden');
|
|
906
|
+
const paper = document.getElementById('pdf-paper');
|
|
907
|
+
paper.style.maxWidth = '760px';
|
|
908
|
+
paper.style.minHeight = 'auto';
|
|
909
|
+
paper.style.background = 'transparent';
|
|
910
|
+
paper.style.boxShadow = 'none';
|
|
911
|
+
inspector.innerHTML = `
|
|
912
|
+
<div class="inspector-top">
|
|
913
|
+
${pickerHtml('pdfSingleFileLoad(this.files[0])')}
|
|
914
|
+
<div class="panel-note">Seluruh teks dari setiap halaman dikumpulkan. Dokumen hasil scan tanpa lapisan teks tidak akan terbaca.</div>
|
|
915
|
+
</div>`;
|
|
916
|
+
actionBtn.onclick = runExtract;
|
|
917
|
+
switchView('pdf');
|
|
918
|
+
}
|
|
919
|
+
|
|
920
|
+
async function runExtract() {
|
|
921
|
+
if (!pdfEngine.doc) return showToast('Pilih berkas PDF terlebih dahulu.', false);
|
|
922
|
+
showToast('Mengekstrak teks...');
|
|
923
|
+
try {
|
|
924
|
+
let all = '';
|
|
925
|
+
for (let i = 1; i <= pdfEngine.pageCount; i++) {
|
|
926
|
+
const page = await pdfEngine.doc.getPage(i);
|
|
927
|
+
const content = await page.getTextContent();
|
|
928
|
+
all += `--- Halaman ${i} ---\n` + content.items.map(it => it.str).join(' ').replace(/\s+/g, ' ').trim() + '\n\n';
|
|
929
|
+
}
|
|
930
|
+
paper_showExtra();
|
|
931
|
+
const extra = document.getElementById('pdf-extra');
|
|
932
|
+
extra.innerHTML = `
|
|
933
|
+
<div style="display:flex;flex-direction:column;gap:10px;">
|
|
934
|
+
<div style="display:flex;justify-content:space-between;align-items:center;margin-bottom:10px;">
|
|
935
|
+
<span class="inspector-label">Hasil Ekstraksi</span>
|
|
936
|
+
<button class="btn-outline" style="padding:4px 10px;font-size:11px;" onclick="copyExtracted()">Salin</button>
|
|
937
|
+
</div>
|
|
938
|
+
<textarea class="form-input mono" id="extract-out" rows="18" style="font-size:12px;">${all.trim().replace(/</g, '<')}</textarea>
|
|
939
|
+
</div>`;
|
|
940
|
+
document.getElementById('pdf-caption').textContent = `${pdfEngine.pageCount} halaman • ${pdfEngine.name}`;
|
|
941
|
+
showToast('Teks berhasil diekstrak.');
|
|
942
|
+
} catch (err) { console.error(err); showToast('Gagal mengekstrak: ' + err.message, false); }
|
|
943
|
+
}
|
|
944
|
+
|
|
945
|
+
function copyExtracted() {
|
|
946
|
+
const ta = document.getElementById('extract-out');
|
|
947
|
+
if (ta) { ta.select(); navigator.clipboard.writeText(ta.value); showToast('Teks disalin ke papan klip.'); }
|
|
948
|
+
}
|
|
949
|
+
|