Automatically Deleting Sensitive Pages from PDFs

You are about to send a contract to a client, but the last page contains internal Terms & Conditions meant only for your legal team. Or perhaps you have a financial report where the final pages contain confidential notes that should not leave your organization.

Manually opening each PDF and deleting pages is tedious, especially when dealing with dozens or hundreds of documents. Let us automate this with the aPDF.io Delete Pages API.

The Quick Solution

Need to remove the last page from a PDF before sharing? Here is the simplest approach:
const data = new FormData();
data.append('file', 'https://your-server.com/contract.pdf');
data.append('pages', 'z'); // Delete the last page

fetch('https://apdf.io/api/pdf/page/delete', {
    headers: {
        'Authorization': 'Bearer YOUR_API_TOKEN',
        'Accept': 'application/json',
        'Content-Type': 'application/json'
    },
    method: 'POST',
    body: data
})
.then(response => response.json())
.then(result => {
    console.log('Cleaned PDF:', result.file);
});

That is it. The API returns a new PDF with the last page removed, ready to share.

Real-World Scenarios

Why would you need to remove pages from PDFs programmatically? Here are common situations:

  • Contracts: Remove internal T&C pages before sending to clients
  • Financial Reports: Strip appendices with internal cost breakdowns
  • HR Documents: Remove salary bands from job descriptions shared externally
  • Legal Filings: Delete cover pages or exhibits not relevant to specific recipients
  • Marketing Materials: Remove pricing pages when sharing product specs

Understanding the Pages Parameter

The pages parameter lets you specify exactly which pages to delete:

  • z - Delete only the last page
  • 1 - Delete the first page (e.g., a cover page)
  • 1,z - Delete the first and last pages
  • r3-r1 - Delete the last three pages (r1=last, r2=second-to-last, etc.)
  • 5-z - Delete from page 5 to the end
  • 1-3,10,z - Delete pages 1-3, page 10, and the last page

The z notation is particularly useful when you do not know the total page count but always need to remove the last page.

Step 1: Get Your API Token

  1. Sign up at aPDF.io (it is free).
  2. Copy your API Token from the dashboard.

Complete Node.js Implementation

Here is a production-ready script that removes the last page from a PDF:

const API_TOKEN = 'YOUR_API_TOKEN_HERE';
const API_URL = 'https://apdf.io/api/pdf/page/delete';

async function removeSensitivePages(pdfUrl, pagesToDelete) {
    try {
        console.log('Removing pages from PDF...');
        console.log('Pages to delete:', pagesToDelete);

        const data = new FormData();
        data.append('file', pdfUrl);
        data.append('pages', pagesToDelete);

        const response = await fetch(API_URL, {
            method: 'POST',
            headers: {
                'Authorization': `Bearer ${API_TOKEN}`,
                'Accept': 'application/json'
            },
            body: data
        });

        if (!response.ok) {
            const error = await response.text();
            throw new Error(`API error ${response.status}: ${error}`);
        }

        const result = await response.json();

        console.log('Success! Cleaned PDF ready:');
        console.log('  URL:', result.file);
        console.log('  Pages remaining:', result.pages);
        console.log('  File size:', result.size, 'bytes');

        return result;

    } catch (error) {
        console.error('Error:', error.message);
        throw error;
    }
}

// Example: Remove last page (Terms & Conditions)
const pdfUrl = 'https://your-server.com/contract-with-internal-terms.pdf';
removeSensitivePages(pdfUrl, 'z');

Batch Processing Multiple Documents

When you have multiple PDFs that all need the same pages removed (e.g., all contracts have internal T&C on the last page), you can process them in batch:

const API_TOKEN = 'YOUR_API_TOKEN_HERE';
const API_URL = 'https://apdf.io/api/pdf/page/delete';

async function cleanPdf(pdfUrl, pagesToDelete) {
    const data = new FormData();
    data.append('file', pdfUrl);
    data.append('pages', pagesToDelete);

    const response = await fetch(API_URL, {
        method: 'POST',
        headers: {
            'Authorization': `Bearer ${API_TOKEN}`,
            'Accept': 'application/json'
        },
        body: data
    });

    if (!response.ok) {
        throw new Error(`Failed to process: ${pdfUrl}`);
    }

    return response.json();
}

async function batchRemoveLastPage(pdfUrls) {
    console.log(`Processing ${pdfUrls.length} documents...\n`);

    const results = [];

    for (const url of pdfUrls) {
        try {
            const result = await cleanPdf(url, 'z');
            results.push({
                original: url,
                cleaned: result.file,
                pages: result.pages
            });
            console.log(`✓ Processed: ${url}`);
        } catch (error) {
            console.error(`✗ Failed: ${url}`);
        }
    }

    console.log(`\nDone! ${results.length} documents cleaned.`);
    return results;
}

// Process multiple contracts
const contracts = [
    'https://your-server.com/contract-client-a.pdf',
    'https://your-server.com/contract-client-b.pdf',
    'https://your-server.com/contract-client-c.pdf'
];

batchRemoveLastPage(contracts);

API Response

When the API successfully removes the specified pages, you get a response like this:

{
  "file": "https://apdf-files.s3.eu-central-1.amazonaws.com/abc123.pdf",
  "expiration": "2025-01-15T14:30:00.000Z",
  "pages": 4,
  "size": 156234
}

Note: The download URL is valid for 1 hour. If you need to email the cleaned PDF to someone, either download it first or send the email promptly.

Conclusion

Removing sensitive pages from PDFs before sharing is now a one-liner. Whether it is stripping Terms & Conditions from contracts or removing internal notes from reports, the aPDF.io Delete Pages API handles it automatically.

No more manual PDF editing. No more accidentally sharing confidential information.

Next Steps

Once you have removed sensitive pages, you might want to:
  • Add Password Protection: Use the Security endpoint to encrypt the cleaned PDF before sharing.
  • Extract Specific Pages: Need to keep only certain pages instead of deleting? Use the Extract Pages endpoint.
Ready to build?
Get your free API token here
Most APIs charge you per document. aPDF.io is built to be a developer-friendly, free alternative that handles the heavy lifting without the monthly subscription.