Merge pull request #58 from divy-11/feature/reverse-multi-pdfs

Feature/reverse multi pdfs
This commit is contained in:
Alam authored and GitHub committed 2025-10-20 18:14:58 +05:30
commit ccb1b119c5
6 files changed
+144 -21

No files matched your search

+1 -1
View File
@@ -18,7 +18,6 @@ export const singlePdfLoadTools = [
'add-header-footer',
'invert-colors',
'view-metadata',
'reverse-pages',
'crop',
'redact',
'pdf-to-bmp',
@@ -62,4 +61,5 @@ export const multiFileTools = [
'tiff-to-pdf',
'alternate-merge',
'linearize',
'reverse-pages',
];
+3 -3
View File
@@ -25,8 +25,8 @@ async function handleSinglePdfUpload(toolId, file) {
showLoader('Loading PDF...');
try {
const pdfBytes = await readFileAsArrayBuffer(file);
state.pdfDoc = await PDFLibDocument.load(pdfBytes as ArrayBuffer, {
ignoreEncryption: true,
state.pdfDoc = await PDFLibDocument.load(pdfBytes as ArrayBuffer, {
ignoreEncryption: true
});
hideLoader();
@@ -324,7 +324,7 @@ async function handleSinglePdfUpload(toolId, file) {
}
async function handleMultiFileUpload(toolId) {
if (toolId === 'merge' || toolId === 'alternate-merge') {
if (toolId === 'merge' || toolId === 'alternate-merge' || toolId === 'reverse-pages') {
const pdfFilesUnloaded: File[] = [];
state.files.forEach((file) => {
+23 -14
View File
@@ -3,29 +3,38 @@ import { downloadFile } from '../utils/helpers.js';
import { state } from '../state.js';
import { PDFDocument as PDFLibDocument } from 'pdf-lib';
import JSZip from 'jszip';
export async function reversePages() {
if (!state.pdfDoc) {
const pdfDocs = state.files.filter((file: File) => file.type === 'application/pdf');
if (!pdfDocs.length) {
showAlert('Error', 'PDF not loaded.');
return;
}
showLoader('Reversing page order...');
try {
const newPdf = await PDFLibDocument.create();
const pageCount = state.pdfDoc.getPageCount();
const reversedIndices = Array.from(
{ length: pageCount },
(_, i) => pageCount - 1 - i
);
const zip = new JSZip();
for (let j = 0; j < pdfDocs.length; j++) {
const file = pdfDocs[j];
const arrayBuffer = await file.arrayBuffer();
const pdfDoc = await PDFLibDocument.load(arrayBuffer);
const newPdf = await PDFLibDocument.create();
const pageCount = pdfDoc.getPageCount();
const reversedIndices = Array.from(
{ length: pageCount },
(_, i) => pageCount - 1 - i
);
const copiedPages = await newPdf.copyPages(state.pdfDoc, reversedIndices);
copiedPages.forEach((page: any) => newPdf.addPage(page));
const copiedPages = await newPdf.copyPages(pdfDoc, reversedIndices);
copiedPages.forEach((page: any) => newPdf.addPage(page));
const newPdfBytes = await newPdf.save();
downloadFile(
new Blob([new Uint8Array(newPdfBytes)], { type: 'application/pdf' }),
'reversed.pdf'
);
const newPdfBytes = await newPdf.save();
const originalName = file.name.replace(/\.pdf$/i, '');
const fileName = `${originalName}_reversed.pdf`;
zip.file(fileName, newPdfBytes);
}
const zipBlob = await zip.generateAsync({ type: 'blob' });
downloadFile(zipBlob, 'reversed_pdfs.zip');
} catch (e) {
console.error(e);
showAlert('Error', 'Could not reverse the PDF pages.');
+1 -1
View File
@@ -969,7 +969,7 @@ export const toolTemplates = {
'reverse-pages': () => `
<h2 class="text-2xl font-bold text-white mb-4">Reverse PDF Pages</h2>
<p class="mb-6 text-gray-400">Flip the order of all pages in your document, making the last page the first.</p>
${createFileInputHTML()}
${createFileInputHTML({ multiple: true, accept: 'application/pdf', showControls: true })}
<div id="file-display-area" class="mt-4 space-y-2"></div>
<button id="process-btn" class="hidden btn-gradient w-full mt-6">Reverse & Download</button>
`,