DEV Community

sunshey
sunshey

Posted on

How to Delete PDF Pages in the Browser with Vue 3 and pdf-lib

Deleting pages from PDF seems simple, but there are important considerations around index management and data safety. Here's how to build a browser-based PDF page deletion tool with Vue 3 and pdf-lib.

The challenge: Index management

When deleting multiple pages, the indices shift. If you delete page 2 first, what was page 3 becomes page 2. This can lead to deleting the wrong pages if not handled correctly.

The stack

  • Vue 3 with Composition API
  • pdf-lib for PDF manipulation
  • Vite for bundling

The core implementation

<script setup lang="ts">
import { ref } from 'vue'
import { PDFDocument } from 'pdf-lib'

const file = ref<File | null>(null)
const pagesToDelete = ref<number[]>([])
const deleting = ref(false)
const result = ref<Uint8Array | null>(null)

async function deletePages() {
  if (!file.value || pagesToDelete.value.length === 0) return
  deleting.value = true

  const arrayBuffer = await file.value.arrayBuffer()
  const pdf = await PDFDocument.load(arrayBuffer)
  const totalPages = pdf.getPageCount()

  // Validate and sort indices (delete from back to front)
  const validIndices = pagesToDelete.value
    .filter(index => index >= 0 && index < totalPages)
    .sort((a, b) => b - a) // Descending order

  for (const index of validIndices) {
    pdf.removePage(index)
  }

  result.value = await pdf.save()
  deleting.value = false
}
</script>
Enter fullscreen mode Exit fullscreen mode

Key implementation details

1. Index validation

Ensure indices are within bounds:

const validIndices = pagesToDelete.value
  .filter(index => index >= 0 && index < totalPages)
Enter fullscreen mode Exit fullscreen mode

2. Delete from back to front

Prevent index shift issues:

.sort((a, b) => b - a) // Descending order
Enter fullscreen mode Exit fullscreen mode

3. User interface

Provide a visual page selector:

<div class="page-grid">
  <div
    v-for="(page, index) in totalPages"
    :key="index"
    :class="['page-item', { deleted: pagesToDelete.includes(index) }]"
    @click="togglePage(index)"
  >
    {{ index + 1 }}
  </div>
</div>
Enter fullscreen mode Exit fullscreen mode

4. Batch operations

Support selecting multiple pages:

function togglePage(index: number) {
  const pos = pagesToDelete.value.indexOf(index)
  if (pos >= 0) {
    pagesToDelete.value.splice(pos, 1)
  } else {
    pagesToDelete.value.push(index)
  }
}
Enter fullscreen mode Exit fullscreen mode

Limitations

No undo

Once pages are deleted, they cannot be recovered.

Solution: Always keep a backup of the original PDF.

Large files

Very large PDFs may cause memory issues.

Solution: Process in smaller batches or use Web Workers.

Summary

Building a browser-based PDF page deletion tool involves:

  1. Loading the PDF with pdf-lib
  2. Validating page indices
  3. Sorting for back-to-front deletion
  4. Removing pages and saving

Try it at en.sotool.top/delete-pages.

Top comments (0)