Building a PDF Page Deletion Tool with RShiny

Hello friends! I needed to delete a few pages from a PDF, so I created a simple solution using RShiny. This interactive web app allows users to upload a PDF, select pages for removal, and download the modified version effortlessly. In this blog post, we’ll explore how to build a PDF page deletion tool using RShiny and the pdftools and qpdf packages.

Overview of the RShiny App

This RShiny app provides users with a user-friendly interface to upload a PDF file, select pages they want to delete, and download the modified PDF file. The app is designed to handle large files (up to 100MB) and ensures that users can only delete pages while retaining at least one page.

Key Features:

  • Upload PDF File: Users can upload a PDF file via a simple file input.
  • Display Pages: The app dynamically detects and lists all pages in the uploaded PDF.
  • Select Pages for Deletion: Users can select pages they want to remove using checkboxes.
  • Process and Modify PDF: The selected pages are deleted, and the remaining pages are saved into a new file.
  • Download Modified PDF: The user can download the updated file.

Code Walkthrough

UI Components

The UI is designed using RShiny’s fluidPage() function, which includes:

  • A fileInput() widget for uploading PDF files.
  • A dynamically generated checkboxGroupInput() for selecting pages to delete.
  • An actionButton() to trigger the page deletion process.
  • A downloadButton() to allow users to download the modified PDF.

Server Logic

The server function handles the core functionalities:

  1. Reading the PDF: When a file is uploaded, the app extracts the total number of pages using pdf_info().
  2. Rendering UI for Page Selection: The app dynamically generates checkboxes corresponding to the pages available in the uploaded PDF.
  3. Processing the File: When the user selects pages and clicks the “Delete Pages” button, the app filters out the selected pages and creates a modified version using qpdf::pdf_subset().
  4. Handling Downloads: The modified PDF file is made available for download.

Why Use RShiny for This?

RShiny is an excellent choice for developing lightweight, interactive applications in R. This tool demonstrates how R can be leveraged beyond data analysis and visualization to solve practical document-processing problems. The combination of pdftools and qpdf provides a powerful way to manipulate PDFs in a structured manner.

Conclusion

This RShiny application makes PDF page deletion easy and efficient. By integrating file handling, dynamic UI elements, and processing logic, this tool showcases the capabilities of RShiny in building practical web-based applications. If you work with PDFs frequently and need a simple way to modify them, try building and customizing this app to fit your needs!

What’s Next?

  • Adding the ability to rearrange pages.
  • Implementing password protection for PDFs.
  • Expanding features to merge multiple PDFs.

Would you like to see additional functionalities in this app? Let us know in the comments!

R Shiny App

GitHub repo for code

Leave a Reply

Your email address will not be published. Required fields are marked *