Go to Page

Remove pages from a PDF

Remove a selection of pages from a PDF with the pages parameter. At least one page always remains, so the selection cannot cover the whole document.

Remove one or more pages from a PDF document. Select the pages to drop with the pages parameter; because at least one page must remain, the selection can’t cover every page. The API is stateless: your document is processed in-region and never stored.

Endpoint

POST
/v1/remove_pages

Available in every region. See Regions & data residency for routing and data residency.

Region URL
Global https://api.pdfblocks.com/v1/remove_pages
United States https://us.api.pdfblocks.com/v1/remove_pages
US HIPAA https://hipaa.api.pdfblocks.com/v1/remove_pages
European Union https://eu.api.pdfblocks.com/v1/remove_pages
United Kingdom https://uk.api.pdfblocks.com/v1/remove_pages
Canada https://ca.api.pdfblocks.com/v1/remove_pages
Australia https://au.api.pdfblocks.com/v1/remove_pages
Japan https://jp.api.pdfblocks.com/v1/remove_pages
India https://in.api.pdfblocks.com/v1/remove_pages
Brazil https://br.api.pdfblocks.com/v1/remove_pages

Authentication

Authenticate every request with your secret API key in the X-API-Key header, over HTTPS. Create and manage keys from the dashboard. See Authentication for details.

Request

The endpoint accepts a multipart/form-data request body.

filefilerequired

The input PDF document.

pagesstringrequired

The pages to remove, written as a page range such as 2,4..6. The selection cannot cover every page: at least one page must remain. Maximum 1000 characters.

Selecting pages

The pages parameter takes a comma-separated list of 1-based page numbers and ranges. It is treated as a set: order and duplicates are ignored, and the pages left behind keep their original document order.

Pattern Removes
1 The first page only
1..3,5 Pages 1, 2, 3, and 5
2.. Page 2 through the last page
..-2 The first page through the second-to-last
-1 The last page

See Selecting pages for the complete reference.

The selection is a set, and it must leave at least one page behind. A selection that covers every page is rejected with a 400.

Examples

Remove page 2 and pages 4–6 from a PDF:

cURLbash
curl https://api.pdfblocks.com/v1/remove_pages \
  -H 'X-API-Key: your_api_key' \
  -F file=@input.pdf \
  -F pages='2,4..6' \
  -o trimmed.pdf
Pythonpython
# pip install requests
import requests

with open('input.pdf', 'rb') as file:
    response = requests.post(
        'https://api.pdfblocks.com/v1/remove_pages',
        headers={'X-API-Key': 'your_api_key'},
        files={'file': file},
        data={'pages': '2,4..6'},
    )

response.raise_for_status()
with open('trimmed.pdf', 'wb') as output:
    output.write(response.content)
Node.jsjavascript
// Node.js 18+
import { readFile, writeFile } from 'node:fs/promises';

const body = new FormData();
body.set('file', new Blob([await readFile('input.pdf')]), 'input.pdf');
body.set('pages', '2,4..6');

const response = await fetch('https://api.pdfblocks.com/v1/remove_pages', {
  method: 'POST',
  headers: { 'X-API-Key': 'your_api_key' },
  body,
});

if (!response.ok) throw new Error(`Request failed: ${response.status}`);
await writeFile('trimmed.pdf', Buffer.from(await response.arrayBuffer()));
PHPphp
<?php
$ch = curl_init('https://api.pdfblocks.com/v1/remove_pages');
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => ['X-API-Key: your_api_key'],
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => [
        'file' => new CURLFile('input.pdf', 'application/pdf'),
        'pages' => '2,4..6',
    ],
]);

$pdf = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) === 200) {
    file_put_contents('trimmed.pdf', $pdf);
}
Rubyruby
# gem install http
require 'http'

response = HTTP
  .headers('X-API-Key' => 'your_api_key')
  .post('https://api.pdfblocks.com/v1/remove_pages', form: {
    file: HTTP::FormData::File.new('input.pdf'),
    pages: '2,4..6',
  })

File.write('trimmed.pdf', response.body) if response.status.success?
Gogo
package main

import (
	"bytes"
	"io"
	"mime/multipart"
	"net/http"
	"os"
)

func main() {
	var buf bytes.Buffer
	form := multipart.NewWriter(&buf)

	file, _ := os.Open("input.pdf")
	defer file.Close()
	part, _ := form.CreateFormFile("file", "input.pdf")
	io.Copy(part, file)

	form.WriteField("pages", "2,4..6")
	form.Close()

	req, _ := http.NewRequest("POST", "https://api.pdfblocks.com/v1/remove_pages", &buf)
	req.Header.Set("Content-Type", form.FormDataContentType())
	req.Header.Set("X-API-Key", "your_api_key")

	res, _ := http.DefaultClient.Do(req)
	defer res.Body.Close()

	out, _ := os.Create("trimmed.pdf")
	defer out.Close()
	io.Copy(out, res.Body)
}
C#csharp
using var client = new HttpClient();
client.DefaultRequestHeaders.Add("X-API-Key", "your_api_key");

using var form = new MultipartFormDataContent
{
    { new ByteArrayContent(File.ReadAllBytes("input.pdf")), "file", "input.pdf" },
    { new StringContent("2,4..6"), "pages" },
};

var response = await client.PostAsync(
    "https://api.pdfblocks.com/v1/remove_pages", form);
response.EnsureSuccessStatusCode();
await File.WriteAllBytesAsync(
    "trimmed.pdf", await response.Content.ReadAsByteArrayAsync());

Response

On success, the response is 200 OK with the trimmed PDF as the body:

HTTP/1.1 200 OK
Content-Type: application/pdf
Content-Length: 26417

The remaining pages keep their original order: only the selected pages are dropped. Stream the body straight to a file, as the examples above do; nothing is stored on our side.

Errors

Failed requests return an application/problem+json body. The most common one for this endpoint is a 400, returned when pages is malformed or would remove every page. The errors object names each field:

{
  "type": "https://www.pdfblocks.com/docs/api/v1/error/400",
  "title": "One or more validation errors occurred.",
  "status": 400,
  "errors": {
    "pages": ["At least one page must remain, so the selection cannot cover every page."]
  }
}

A missing or invalid X-API-Key returns a 401. See Errors for every status code and the full response shape.

Recipes

Common variations. Expand one to see it in every language.

Drop the last page
cURLbash
curl https://api.pdfblocks.com/v1/remove_pages \
  -H 'X-API-Key: your_api_key' \
  -F file=@input.pdf \
  -F pages='-1' \
  -o without-last.pdf
Pythonpython
import requests

with open('input.pdf', 'rb') as file:
    response = requests.post(
        'https://api.pdfblocks.com/v1/remove_pages',
        headers={'X-API-Key': 'your_api_key'},
        files={'file': file},
        data={'pages': '-1'},
    )

response.raise_for_status()
with open('without-last.pdf', 'wb') as output:
    output.write(response.content)
Node.jsjavascript
import { readFile, writeFile } from 'node:fs/promises';

const body = new FormData();
body.set('file', new Blob([await readFile('input.pdf')]), 'input.pdf');
body.set('pages', '-1');

const response = await fetch('https://api.pdfblocks.com/v1/remove_pages', {
  method: 'POST',
  headers: { 'X-API-Key': 'your_api_key' },
  body,
});

if (!response.ok) throw new Error(`Request failed: ${response.status}`);
await writeFile('without-last.pdf', Buffer.from(await response.arrayBuffer()));
PHPphp
<?php
$ch = curl_init('https://api.pdfblocks.com/v1/remove_pages');
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => ['X-API-Key: your_api_key'],
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => [
        'file' => new CURLFile('input.pdf', 'application/pdf'),
        'pages' => '-1',
    ],
]);

$pdf = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) === 200) {
    file_put_contents('without-last.pdf', $pdf);
}
Rubyruby
require 'http'

response = HTTP
  .headers('X-API-Key' => 'your_api_key')
  .post('https://api.pdfblocks.com/v1/remove_pages', form: {
    file: HTTP::FormData::File.new('input.pdf'),
    pages: '-1',
  })

File.write('without-last.pdf', response.body) if response.status.success?
Gogo
package main

import (
	"bytes"
	"io"
	"mime/multipart"
	"net/http"
	"os"
)

func main() {
	var buf bytes.Buffer
	form := multipart.NewWriter(&buf)

	file, _ := os.Open("input.pdf")
	defer file.Close()
	part, _ := form.CreateFormFile("file", "input.pdf")
	io.Copy(part, file)

	form.WriteField("pages", "-1")
	form.Close()

	req, _ := http.NewRequest("POST", "https://api.pdfblocks.com/v1/remove_pages", &buf)
	req.Header.Set("Content-Type", form.FormDataContentType())
	req.Header.Set("X-API-Key", "your_api_key")

	res, _ := http.DefaultClient.Do(req)
	defer res.Body.Close()

	out, _ := os.Create("without-last.pdf")
	defer out.Close()
	io.Copy(out, res.Body)
}
C#csharp
using var client = new HttpClient();
client.DefaultRequestHeaders.Add("X-API-Key", "your_api_key");

using var form = new MultipartFormDataContent
{
    { new ByteArrayContent(File.ReadAllBytes("input.pdf")), "file", "input.pdf" },
    { new StringContent("-1"), "pages" },
};

var response = await client.PostAsync(
    "https://api.pdfblocks.com/v1/remove_pages", form);
response.EnsureSuccessStatusCode();
await File.WriteAllBytesAsync(
    "without-last.pdf", await response.Content.ReadAsByteArrayAsync());
Remove the cover page
cURLbash
curl https://api.pdfblocks.com/v1/remove_pages \
  -H 'X-API-Key: your_api_key' \
  -F file=@input.pdf \
  -F pages='1' \
  -o no-cover.pdf
Pythonpython
import requests

with open('input.pdf', 'rb') as file:
    response = requests.post(
        'https://api.pdfblocks.com/v1/remove_pages',
        headers={'X-API-Key': 'your_api_key'},
        files={'file': file},
        data={'pages': '1'},
    )

response.raise_for_status()
with open('no-cover.pdf', 'wb') as output:
    output.write(response.content)
Node.jsjavascript
import { readFile, writeFile } from 'node:fs/promises';

const body = new FormData();
body.set('file', new Blob([await readFile('input.pdf')]), 'input.pdf');
body.set('pages', '1');

const response = await fetch('https://api.pdfblocks.com/v1/remove_pages', {
  method: 'POST',
  headers: { 'X-API-Key': 'your_api_key' },
  body,
});

if (!response.ok) throw new Error(`Request failed: ${response.status}`);
await writeFile('no-cover.pdf', Buffer.from(await response.arrayBuffer()));
PHPphp
<?php
$ch = curl_init('https://api.pdfblocks.com/v1/remove_pages');
curl_setopt_array($ch, [
    CURLOPT_RETURNTRANSFER => true,
    CURLOPT_HTTPHEADER => ['X-API-Key: your_api_key'],
    CURLOPT_POST => true,
    CURLOPT_POSTFIELDS => [
        'file' => new CURLFile('input.pdf', 'application/pdf'),
        'pages' => '1',
    ],
]);

$pdf = curl_exec($ch);
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) === 200) {
    file_put_contents('no-cover.pdf', $pdf);
}
Rubyruby
require 'http'

response = HTTP
  .headers('X-API-Key' => 'your_api_key')
  .post('https://api.pdfblocks.com/v1/remove_pages', form: {
    file: HTTP::FormData::File.new('input.pdf'),
    pages: '1',
  })

File.write('no-cover.pdf', response.body) if response.status.success?
Gogo
package main

import (
	"bytes"
	"io"
	"mime/multipart"
	"net/http"
	"os"
)

func main() {
	var buf bytes.Buffer
	form := multipart.NewWriter(&buf)

	file, _ := os.Open("input.pdf")
	defer file.Close()
	part, _ := form.CreateFormFile("file", "input.pdf")
	io.Copy(part, file)

	form.WriteField("pages", "1")
	form.Close()

	req, _ := http.NewRequest("POST", "https://api.pdfblocks.com/v1/remove_pages", &buf)
	req.Header.Set("Content-Type", form.FormDataContentType())
	req.Header.Set("X-API-Key", "your_api_key")

	res, _ := http.DefaultClient.Do(req)
	defer res.Body.Close()

	out, _ := os.Create("no-cover.pdf")
	defer out.Close()
	io.Copy(out, res.Body)
}
C#csharp
using var client = new HttpClient();
client.DefaultRequestHeaders.Add("X-API-Key", "your_api_key");

using var form = new MultipartFormDataContent
{
    { new ByteArrayContent(File.ReadAllBytes("input.pdf")), "file", "input.pdf" },
    { new StringContent("1"), "pages" },
};

var response = await client.PostAsync(
    "https://api.pdfblocks.com/v1/remove_pages", form);
response.EnsureSuccessStatusCode();
await File.WriteAllBytesAsync(
    "no-cover.pdf", await response.Content.ReadAsByteArrayAsync());